SQL CREATE VIEW
Stores a named query that you can select from like a table. Most engines take CREATE OR REPLACE VIEW; SQL Server instead has CREATE OR ALTER VIEW, and SQLite has neither.
- Category
- Views
- In this category
- 1 of 3
Syntax
CREATE VIEW view_name [(column_aliases)] AS SELECT ...
CREATE OR REPLACE VIEW view_name AS SELECT ... -- PostgreSQL, MySQL, Oracle
CREATE OR ALTER VIEW view_name AS SELECT ... -- SQL Server 2016 SP1+
Each block is a separate form. The support table and the examples show which engine takes which.
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Different syntax | no OR REPLACE; CREATE OR ALTER VIEW from 2016 SP1 |
| PostgreSQL | Yes | OR REPLACE keeps permissions; existing output columns are frozen |
| MySQL | Yes | OR REPLACE, plus ALGORITHM, DEFINER and WITH CHECK OPTION |
| SQLite | Different syntax | read-only; no OR REPLACE and no ALTER VIEW |
| Oracle | Yes | OR REPLACE; FORCE creates the view even while base objects are missing |
Examples
Any engine
CREATE VIEW open_orders AS SELECT id, customer_id, placed_at, total FROM orders WHERE status = 'open';
PostgreSQL
CREATE OR REPLACE VIEW open_orders AS SELECT id, customer_id, placed_at, total FROM orders WHERE status = 'open';
SQL Server
CREATE OR ALTER VIEW dbo.open_orders AS SELECT id, customer_id, placed_at, total FROM dbo.orders WHERE status = 'open';
MySQL
CREATE OR REPLACE VIEW open_orders AS SELECT id, customer_id, total FROM orders WHERE status = 'open' WITH CHECK OPTION;
Oracle
CREATE OR REPLACE VIEW open_orders AS SELECT id, customer_id, total FROM orders WHERE status = 'open';
Good to know
- SQL Server has no CREATE OR REPLACE VIEW. Use CREATE OR ALTER VIEW (2016 SP1+) or drop-and-create; a plain CREATE fails once the view exists, which breaks re-runnable migration scripts.
- PostgreSQL's OR REPLACE cannot change existing output columns: the names, order and types of the original column list are frozen, and you may only append new ones. Changing a column's type means DROP VIEW and CREATE again.
- SQLite views are read-only stored queries with no OR REPLACE and no ALTER VIEW: redefine by DROP VIEW then CREATE VIEW, ideally in one transaction so no reader sees the view missing.
- MySQL's WITH CHECK OPTION makes writes through the view reject rows the view would not show; without it an UPDATE through a filtered view can move a row out of its own view. PostgreSQL accepts WITH CHECK OPTION on updatable views too.
Questions about CREATE VIEW
How do I change a view's definition?
CREATE OR REPLACE VIEW on PostgreSQL, MySQL and Oracle; CREATE OR ALTER VIEW on SQL Server 2016 SP1+; DROP VIEW then CREATE VIEW on SQLite (and anywhere else, if you prefer one portable habit).
Can I insert or update rows through a view?
On simple single-table views, yes: MySQL, PostgreSQL, SQL Server and Oracle accept writes through views that stay within the base table's rules, subject to WITH CHECK OPTION when declared. SQLite views are read-only unless an INSTEAD OF trigger handles the write. Views over aggregates reject writes everywhere. Join views differ: PostgreSQL rejects them, while MySQL and SQL Server accept a write that changes only one base table. INSTEAD OF triggers on PostgreSQL, SQL Server, Oracle and SQLite can translate the rest; MySQL has none.
Do views make queries faster?
No: a plain view is a stored query, and every use re-runs it. PostgreSQL and Oracle have materialized views that store the result, and SQL Server has indexed views. On MySQL and SQLite, build and refresh a summary table yourself.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.