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.
CREATE TABLE geeks;
Step 2: Use database
Step 3: Table definition
We have the following geeksforgeeks table in the database.
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.
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
SELECT * FROM geeksforgeeks;
Step 6: Create View
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.
SELECT * FROM female;
Step 8 : Rename view from object explorer
Steps to rename view:
- Select View from menu bar.
- Select Object explorer option. Object explorer will be appeared on left side of the screen.
- Select Database folder and select your database (geeks we have used in this article).
- Inside the database, select View option.
- Right click on you viewname and select RENAME option.
- Give new name as per your choice.
- Object explorer:
- View that we have created:
- Rename by right clicking on it:
- We have changed the name of view to ‘Changed_name’
Step 8: See the content of view using new view name
SELECT * FROM Changed_name;
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.