SQL Tutorial - How to rename tables or columns

preview_player
Показать описание
Another fantastic SQL Tutorial brought to you by BeardedDev.

We are all human and that means sometimes we make mistakes when naming objects such as tables and columns in our database.

To rename columns or tables we can use the system stored procedure sp_rename.

In this video we go through examples using sp_rename to rename tables, rename columns and there is even a bonus query on how to rename an index.

SQL Syntax:
CREATE TABLE dbo.CstomerDetals
(
Cust_Id INT IDENTITY(1, 1) NOT NULL
CONSTRAINT PK_Customr_Cust_Id PRIMARY KEY (Cust_Id)
, Title CHAR(5) NULL
, First_Name VARCHAR(50) NOT NULL
, Middle_Name VARCHAR(50) NOT NULL
, Last_Name VARCHAR(50) NOT NULL
, DBO DATE NOT NULL
)

EXEC sp_rename 'dbo.CstomerDetals', 'CustomerDetails'

EXEC sp_rename 'dbo.CustomerDetails.DBO', 'DOB', 'COLUMN'

EXEC sp_rename 'dbo.CustomerDetails.PK_Customr_Cust_Id', 'PK_Customer_Cust_Id', 'INDEX'

Before renaming any objects it is important to identify any objects such as stored procedures that reference the object you are renaming as these will show an error that the name is not recognised when run.
Рекомендации по теме
Комментарии
Автор

Thank you very much, saved me the hassle!

euanespenilla
Автор

Thank you. That is what I was looking for...

otosonet
Автор

THANK YOU FOR THIS HELPFUL VIDEO TUTORIAL

tubinojrantolinn.
Автор

Nice tutorial helped me allot!
thank you :)

suleymanyavas
Автор

very clear thank you so much mate <3 !

al-doori
Автор

I learned a lot from this..thanks a lot....As all queries statements undergo execution why does the rename function requires specifically the exec keyword to be executed? Thanks a lot

hasanmougharbel
Автор

thank you. what about renaming a temp table header ?

mokhot
welcome to shbcf.ru