SQL ALTER VIEW
Changes an existing view: its query on SQL Server and MySQL, its name, schema or options on PostgreSQL and Oracle. SQLite has no ALTER VIEW at all.
- Categoría
- Vistas
- In this category
- 2 of 3
Sintaxis
ALTER VIEW view_name AS SELECT ... -- SQL Server, MySQL: new definition
CREATE OR REPLACE VIEW view_name AS SELECT ... -- PostgreSQL, Oracle
ALTER VIEW view_name RENAME TO new_name -- PostgreSQL
DROP VIEW view_name; CREATE VIEW view_name AS SELECT ... -- SQLite
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | ALTER VIEW ... AS redefines; permissions survive |
| PostgreSQL | Different syntax | ALTER VIEW renames or moves it; the query changes with CREATE OR REPLACE |
| MySQL | Sí | ALTER VIEW ... AS redefines, the same as CREATE OR REPLACE |
| SQLite | No equivalent | no ALTER VIEW: drop and recreate |
| Oracle | Different syntax | CREATE OR REPLACE redefines; ALTER VIEW COMPILE revalidates |
Ejemplos
SQL Server
ALTER VIEW dbo.open_orders
AS
SELECT id, customer_id, placed_at, total
FROM dbo.orders
WHERE status IN ('open', 'processing');
MySQL
ALTER VIEW open_orders AS
SELECT id, customer_id, placed_at, total
FROM orders WHERE status IN ('open', 'processing');
PostgreSQL
CREATE OR REPLACE VIEW open_orders AS
SELECT id, customer_id, placed_at, total
FROM orders WHERE status IN ('open', 'processing');
PostgreSQL
ALTER VIEW open_orders RENAME TO active_orders;
SQLite
BEGIN;
DROP VIEW open_orders;
CREATE VIEW open_orders AS
SELECT id, customer_id, placed_at, total
FROM orders WHERE status IN ('open', 'processing');
COMMIT;
Oracle
ALTER VIEW open_orders COMPILE;
Conviene saberlo
- PostgreSQL splits the job: ALTER VIEW handles RENAME TO, SET SCHEMA, owners and options such as security_invoker, while the query itself only changes through CREATE OR REPLACE VIEW, with the frozen-column-list rules that come with it.
- SQL Server's ALTER VIEW replaces the definition and keeps the view's permissions, which is why it beats drop-and-create for anything with grants attached.
- SQLite has neither ALTER VIEW nor OR REPLACE. DROP VIEW then CREATE VIEW inside one transaction is the standard move; readers see the old definition until the commit lands.
- Oracle's ALTER VIEW does not change the query: it recompiles (ALTER VIEW v COMPILE) and manages view constraints. Redefinition is CREATE OR REPLACE VIEW.
Questions about ALTER VIEW
Why is there no ALTER VIEW AS on PostgreSQL?
PostgreSQL reserves ALTER VIEW for metadata: RENAME TO, SET SCHEMA, owner and options. The query changes through CREATE OR REPLACE VIEW, which keeps permissions and dependencies while swapping the definition under the column-freeze rules.
How do I rename a view?
PostgreSQL: ALTER VIEW v RENAME TO v2. MySQL: RENAME TABLE v TO v2, which works for views too. SQL Server's sp_rename renames the object but leaves stored definitions stale, so drop-and-recreate is the recommended path there. SQLite and Oracle: drop and recreate.
Do permissions survive a view change?
On SQL Server's ALTER VIEW and PostgreSQL's CREATE OR REPLACE VIEW, yes: the object keeps its identity and its grants. Drop-and-recreate loses grants on SQL Server, PostgreSQL and Oracle, so re-apply them, which is the main reason engines grew replace forms. MySQL is the exception: its grants are stored by name in mysql.tables_priv, so they survive DROP VIEW and apply to the recreated view.
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.