What Command do we Use to Rename a db, a Table and a Column?

To Rename db

sp_renamedb ‘oldname’ , ‘newname
If someone is using db it will not accept sp_renmaedb. In that case, first bring db to single user mode using sp_dboptions. Use sp_renamedb to rename the database. Use sp_dboptions to bring the database to multi-user mode.
e.g.
USE MASTER;
GO

EXEC sp_dboption AdventureWorks'Single User'True
GO


EXEC sp_renamedb 'AdventureWorks''AdventureWorks_New'
GO

EXEC sp_dboption AdventureWorks'Single User'False
GO

To Rename Table

We can change the table name using sp_rename as follows:
sp_rename 'oldTableName' 'newTableName'
e.g.
sp_RENAME 'Table_First''Table_Last'GO

To rename Column

The script for renaming any column is as follows:
sp_rename 'TableName.[OldcolumnName]''NewColumnName''Column'
e.g.
sp_RENAME 'Table_First.Name''NameChange' 'COLUMN'GO

No comments:

Post a Comment

What is Normalization?

Database normalization is a data design and organization process applied to data structures based on rules that help building relational dat...