Open In App

How to Rename a View in SQL Server?

View is a virtual table based on the result-set of an SQL statement. It is like the subset of the table and basically created to optimize the database experience.

Like real table, this also contains rows and column. The data in a view are the data extracted from one or more real tables in the database.



We can perform all the functions to the view as we do with the tables.

Create VIEW syntax:

CREATE VIEW view_name AS SELECT
column_1, column_2, ...
FROM table_name WHERE condition;

Renaming of view can be done through Object explorer in SQL Server.

Step 1: Create database



Use the following command to create database.

Query:

CREATE TABLE geeks;

Step 2: Use database

Query:

USE geeks;

Step 3: Table definition

We have the following geeksforgeeks table in the database.

Query:

CREATE TABLE geeksforgeeks
(FIRSTNAME varchar(20), 
LASTNAME varchar(20),
GENDER varchar(10), AGE int);

Step 4: Insert values

Following command is used to insert values into the table.

Query:

INSERT INTO geeksforgeeks VALUES
('ROMY','KUMARI','FEMALE', 22),
 ('PUSHKAR', 'JHA', 'MALE', 23),
('SOUMYA', 'SHRIYA', 'FEMALE', 22), 
('NIKHIL', 'KALRA', 'MALE', 23),
('ROHIT', 'KUMAR', 'MALE', 23), 
('ASTHA', 'GUPTA', 'FEMALE',22),
('SAMIKSHA', 'MISHRA', 'FEMALE', 22), 
('MANU', 'PILLAI', 'MALE', 24);

Step 5: View data of the table

Query:

SELECT * FROM geeksforgeeks;

Output:

Step 6: Create View 

Query:

CREATE VIEW FEMALE AS SELECT fIRSTNAME, LASTNAME,AGE
FROM geeksforgeeks WHERE GENDER='FEMALE';

This will have the values whose gender is female.

Step 7: See the content of the view

Content can be viewed with the same query as we use for table.

Query:

SELECT * FROM female;

Output:

Step 8 : Rename view from object explorer

Steps to rename view:

Step 8: See the content of view using new view name

Query:

SELECT * FROM Changed_name;

Output:

Article Tags :