How to Create, Replace, Alter, and Drop a View in MariaDB
A MariaDB view gives a SELECT statement a name so you can query it like a table. The definition stores the choice of columns and filters, and later queries read the current data in the underlying tables. Use CREATE VIEW to define it, ALTER VIEW to change it, and DROP VIEW to remove it.
Create a practice employee table
Connect to a practice server with a MariaDB client. The example creates and selects a fresh database named view_lab. If that name is in use, choose a different one and use it in both CREATE DATABASE and USE. If you already have a scratch database, omit CREATE DATABASE and use its name in the USE statement.
CREATE DATABASE view_lab;
USE view_lab;
CREATE TABLE view_demo_employees (
emp_no INT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
gender CHAR(1) NOT NULL
);
INSERT INTO view_demo_employees (emp_no, first_name, last_name, gender)
VALUES (1, 'Alex', 'Kim', 'M'),
(2, 'Mina', 'Park', 'F'),
(3, 'Daniel', 'Lee', 'M');
The table has three employees. The first and third have gender M and the second has F. Run the following examples in order in the same database, since each one uses the state left by the preceding step.
For a larger practice dataset, follow the guide to installing the employees sample database and run the same SELECT against the employees table.
Create and query the view
Run the SELECT first to check the filter and the two name columns. With MariaDB’s CREATE VIEW reference, putting the same SELECT after AS gives that query the name v_employees_male. Put ORDER BY on the SELECT that reads from the view when the output needs a specific order.
SELECT first_name, last_name
FROM view_demo_employees
WHERE gender = 'M'
ORDER BY first_name, last_name;
CREATE VIEW v_employees_male AS
SELECT first_name, last_name
FROM view_demo_employees
WHERE gender = 'M';
SELECT first_name, last_name
FROM v_employees_male
ORDER BY first_name, last_name;
| first_name | last_name |
|---|---|
| Alex | Kim |
| Daniel | Lee |
Both queries return the two rows in the table. Later queries through the view reflect changes to the rows in the source table that satisfy the filter.
Keep or replace an existing view
Plain CREATE VIEW raises an error if the name already exists. Use CREATE VIEW IF NOT EXISTS when the existing definition should stay in place. The next statement asks for gender F, but the previously created view keeps its existing definition.
CREATE VIEW IF NOT EXISTS v_employees_male AS
SELECT first_name, last_name
FROM view_demo_employees
WHERE gender = 'F';
SHOW WARNINGS;
SELECT first_name, last_name
FROM v_employees_male
ORDER BY first_name, last_name;
SHOW WARNINGS reports note 1050 for the existing name. Alex Kim and Daniel Lee still appear because the saved filter is unchanged.
Use CREATE OR REPLACE VIEW when you want to install the supplied definition whether or not the view already exists. This example keeps the filter and adds gender to the result. OR REPLACE requires DROP privilege for the view in addition to the privileges needed to create it.
CREATE OR REPLACE VIEW v_employees_male AS
SELECT first_name, last_name, gender
FROM view_demo_employees
WHERE gender = 'M';
SELECT first_name, last_name, gender
FROM v_employees_male
ORDER BY first_name, last_name;
Both rows now include M in the gender column. Before replacing a view used by other queries, check that those queries still have the column names and types they expect.
Alter and inspect the view definition
Use MariaDB’s ALTER VIEW reference when the view must already exist. Supply the full SELECT, including the columns and filter to retain. Use SHOW CREATE VIEW to inspect the stored definition.
ALTER VIEW v_employees_male AS
SELECT emp_no, first_name, last_name, gender
FROM view_demo_employees
WHERE gender = 'M';
SHOW CREATE VIEW v_employees_male;
SELECT emp_no, first_name, last_name, gender
FROM v_employees_male
ORDER BY emp_no;
Look for emp_no in the definition. The following query returns employees 1 and 3 with their names and gender. If you are editing an existing database, save the SHOW CREATE VIEW output beforehand so you can restore the earlier definition if needed.
Drop the view and check the source data
Use MariaDB’s DROP VIEW reference to remove the view. IF EXISTS lets the command proceed when the view is already absent. Check for reports or queries that reference the view before deleting one that is in use.
DROP VIEW IF EXISTS v_employees_male;
SELECT TABLE_NAME
FROM information_schema.VIEWS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'v_employees_male';
SELECT COUNT(*) AS employee_count FROM view_demo_employees;
After the drop, the view name is absent from information_schema.VIEWS. The employee_count in the source table remains 3.



