filmov
tv
SQL Tutorial - How to rename tables or columns

Показать описание
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.
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.
Комментарии