Skip to content

SQL - Rename Views

Renaming a database view is a common administrative task. However, unlike other objects, there is no single, standard SQL command for this. The method for renaming a view varies significantly between different database management systems (RDBMS).

Before renaming a view, it is critical to identify all other database objects (like other views, stored procedures, or functions) that depend on it. These dependencies will need to be updated to use the new view name to avoid breaking your application.

PostgreSQL follows the SQL standard for renaming views, providing a straightforward DDL command.

-- Syntax for PostgreSQL
ALTER VIEW old_view_name RENAME TO new_view_name;

In SQL Server, you use the system stored procedure sp_rename to change the name of an object, including a view.

-- Syntax for SQL Server
EXEC sp_rename 'old_view_name', 'new_view_name';

SQL Server will often issue a warning that changing any part of an object’s name can break scripts and stored procedures. This underscores the importance of checking dependencies first.

MySQL uses the RENAME TABLE statement, which works for both tables and views.

-- Syntax for MySQL
RENAME TABLE old_view_name TO new_view_name;

First, let’s create a base table and a view. This view, USA_CUSTOMERS, will show only customers from the USA.

-- Create a base table
CREATE TABLE CUSTOMERS(
ID INT PRIMARY KEY,
NAME VARCHAR(255) NOT NULL,
COUNTRY VARCHAR(255)
);
-- Insert data
INSERT INTO CUSTOMERS (ID, NAME, COUNTRY) VALUES
(1, 'John Smith', 'USA'),
(2, 'Jane Doe', 'USA'),
(3, 'Pierre Martin', 'France');
-- Create the view
CREATE VIEW USA_CUSTOMERS AS
SELECT ID, NAME FROM CUSTOMERS WHERE COUNTRY = 'USA';

Now, let’s say we want to rename USA_CUSTOMERS to AMERICAN_CUSTOMERS. Using PostgreSQL syntax:

ALTER VIEW USA_CUSTOMERS RENAME TO AMERICAN_CUSTOMERS;

After renaming, you can verify the change by querying the new view name. The old name will no longer work.

SELECT * FROM AMERICAN_CUSTOMERS; -- This will succeed.
SELECT * FROM USA_CUSTOMERS; -- This will fail.

An alternative, universal approach that works on any database system is to drop the old view and create a new one with the desired name and definition. This is often considered a safer and more explicit practice.

DROP VIEW USA_CUSTOMERS;
CREATE VIEW AMERICAN_CUSTOMERS AS
SELECT ID, NAME FROM CUSTOMERS WHERE COUNTRY = 'USA';
  • Pros: Works on all platforms. Allows you to change the view’s definition at the same time. It’s a very clear and explicit action.
  • Cons: Any permissions (e.g., GRANT SELECT) granted on the original view will be lost and must be re-applied to the new view. There is a brief moment where the view does not exist, which could affect concurrent queries.

Before renaming a view in a production environment, follow these steps:

  • Backup: Always have a recent backup of your database schema and data.
  • Identify Dependencies: Use your database’s system catalog tables to find all objects that reference the view you plan to rename.
  • Plan Downtime (if needed): For critical views, plan a maintenance window to perform the rename and update all dependencies.
  • Script the Changes: Prepare a script that renames the view and updates all dependent objects. For the drop/re-create method, include the GRANT statements to re-apply permissions.
  • Test: Execute your script in a development or staging environment that mirrors production to ensure everything works as expected.
  • Deploy: Run the script in production during your planned window.