DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Different Ways To Rename Database Objects

Different Ways To Rename Database Objects

This article explains different methods to rename database objects, including using SSMS and sp_rename stored procedure.

Nisarg Upadhyay user avatar by
Nisarg Upadhyay
·
Mar. 17, 23 · Tutorial
Like (1)
Save
Tweet
Share
2.24K Views

Join the DZone community and get the full member experience.

Join For Free

This article explains different methods to rename database objects. We can rename database objects by using any of the following methods:

  • Rename database objects using SSMS.
  • Rename database objects using the sp_rename stored procedure.

To understand the concept with more clarity, I have explained the methods with simple examples. For demonstration, I have created a database named SchoolManagement. I have created a database schema using ChatGPT. There is an interesting article written on ChatGPT and how we can use it. You can read it here. The script to create the database and tables are following:

Query To Create a Database

MS SQL
 
Create database SchoolManagement


Script To Create Tables

MS SQL
 
CREATE TABLE [dbo].[student](
   	[student_id] [int] IDENTITY(1,1) NOT NULL,
   	[first_name] [varchar](255) NOT NULL,
   	[last_name] [varchar](255) NOT NULL,
   	[date_of_birth] [date] NOT NULL,
   	[gender] [varchar](10) NOT NULL,
   	[address] [varchar](255) NOT NULL,
   	[email] [varchar](255) NOT NULL,
   	[phone_number] [varchar](255) NOT NULL,
 CONSTRAINT [PK_students_student_id] PRIMARY KEY CLUSTERED ([student_id] ASC))
CREATE TABLE [dbo].[courses](
   	[course_id] [int] IDENTITY(1,1) NOT NULL,
   	[course_name] [varchar](255) NOT NULL,
   	[course_description] [text] NOT NULL,
PRIMARY KEY CLUSTERED ([course_id] ASC))
GO
CREATE TABLE [dbo].[class_subjects](
   	[class_subject_id] [int] IDENTITY(1,1) NOT NULL,
   	[course_id] [int] NOT NULL,
   	[teacher_id] [int] NOT NULL,
   	[class_name] [varchar](255) NOT NULL,
PRIMARY KEY CLUSTERED ([class_subject_id] ASC))
GO
CREATE TABLE [dbo].[student_enrollment](
   	[enrollment_id] [int] IDENTITY(1,1) NOT NULL,
   	[student_id] [int] NOT NULL,
   	[class_subject_id] [int] NOT NULL,
   	[enrollment_date] [date] NOT NULL,
PRIMARY KEY CLUSTERED
([enrollment_id] ASC))
GO
CREATE TABLE [dbo].[grades](
   	[grade_id] [int] IDENTITY(1,1) NOT NULL,
   	[enrollment_id] [int] NOT NULL,
   	[grade] [float] NOT NULL,
PRIMARY KEY CLUSTERED ([grade_id] ASC))
GO
CREATE TABLE [dbo].[teachers](
   	[teacher_id] [int] IDENTITY(1,1) NOT NULL,
   	[teacher_first_name] [varchar](255) NOT NULL,
   	[teacher_last_name] [varchar](255) NOT NULL,
   	[subject] [varchar](255) NOT NULL,
   	[address] [varchar](255) NOT NULL,
   	[email] [varchar](255) NOT NULL,
   	[phone_number] [varchar](255) NOT NULL,
PRIMARY KEY CLUSTERED ([teacher_id] ASC))
GO


Script To Create Stored Procedures and Views

MS SQL
 
/*Script to create a stored procedure*/
CREATE PROCEDURE [dbo].[sp_get_student_name]
AS
BEGIN
SELECT
first_name, last_name, s.date_of_birth,s.gender, s.address,s.email,s.phone_number,se.enrollment_date,c.course_name,c.course_description ,cs.class_name
FROM
student_test s INNER JOIN student_enrollment se ON s.student_id=se.student_id
LEFT JOIN class_subjects cs ON se.class_subject_id=cs.class_subject_id
LEFT JOIN courses c ON c.course_id=cs.course_id
END
/*Script to create a view*/
CREATE VIEW [dbo].[vwStudents]
as
SELECT
first_name, last_name, s.date_of_birth,s.gender, s.address,s.email,s.phone_number,se.enrollment_date,c.course_name,c.course_description ,cs.class_name
FROM
student s INNER JOIN student_enrollment se ON s.student_id=se.student_id
LEFT JOIN class_subjects cs ON se.class_subject_id=cs.class_subject_id
LEFT JOIN courses c ON c.course_id=cs.course_id
GO


The database diagram looks like the following image: 

database diagram

First, let us see how we can rename database objects using SSMS. 

Rename Objects Using SQL Server Management Studio

Renaming the object using SQL Server management studio is very straightforward. Right-click on any database object that you want to rename and click on rename. The object name will be editable, and you can enter the desired new name. Suppose you want to rename the table named grades to student_grades. To do that, right-click on grades tables and select Rename. See the following image: 

Rename Objects Using SQL Server Management Studio

Similarly, you can rename any stored procedure or function. To do that, Expand Programmability --> Expand Stored Procedure --> Hit F2 and specify the desired new name of the stored procedure. In our case, the new name of the stored procedure will be sp_get_all_student_name. See the following screenshot for reference.

sp_get_all_student_name


Now, let us see how to use the sp_rename system stored procedure to rename the database object name.

Rename Objects Using System-Stored Procedures

First, let us understand the basics of the sp_rename stored procedure.

The sp_rename procedure is used to rename the table, table column, index name, and common language run time user-defined type. The syntax of the procedure is the following.

MS SQL
 
Exec sp_rename 'object_name', 'new_object_name', 'db_object_type'


In the syntax,

  1.  object_name: Specify the name of the object name that you want to rename.
    •  If you are renaming the table, then the object_name parameter will be specified like table_name or schema_name.table_name.
    • If you are renaming the table’s column name, then the object_name parameter will be specified like table_name.column_name.
    • If you are renaming the index name of the table, then the object_name parameter will be specified like schema_name.table_name.index_nameor table_name.index_name.
  2. new_object_name: Specify the new name of the object.
  3. db_object_type: Specify the type of object that you want to rename. The valid value of the parameter must be any of the following:
    • COLUMN
    • DATABASE
    • INDEX
    • OBJECT
    • STATISTICS

In this example, we will learn how we can rename the column name and index name.

Change the Index Name

In this example, we will change the index name of table [dbo].[student]. The primary key’s name is system generated, and I am changing it to an appropriate name. The old name is PK__students__2A33069A2E02BF0C, and the new name will be PK_students_student_id. The query to rename the index is the following.

MS SQL
 
USE SchoolManagement
go
EXEC sp_rename @objname = 'student.PK__students__2A33069A2E02BF0C', @newname = 'PK_students_student_id', @objtype = 'INDEX'


Run the below query to confirm that the changes are applied.

MS SQL
 
USE SchoolManagement
go
SELECT object_id AS [Object ID],object_name(object_id) AS [Table Name], name [Index Name], type_desc AS [Index Type] FROM sys.indexes WHERE object_id=OBJECT_ID('student')


Query output:

Query output

Now, let us see how we can rename the column of a table using the sp_rename stored procedure.

Change the Column Name

In this example, we will change the column name of [dbo].[teacher] table. The old name is first_name, and last_name, and the new name of the column will be teacher_first_name and teacher_last_name. To do that, execute the following T-SQL statement.

MS SQL
 
USE SchoolManagement
go
EXEC sp_rename @objname = 'teachers.first_name', @newname = 'teacher_first_name', @objtype = 'COLUMN'
GO
EXEC sp_rename @objname = 'teachers.last_name', @newname = 'teacher_last_name', @objtype = 'COLUMN'
GO
Once the SQL query executes successfully, run the following query to verify that the column name has been changed as expected.
USE SchoolManagement
go
EXEC sp_help 'teachers'


Screenshot:

Table screenshot

Important Notes 

  1. To rename any database object, we require the following set of permission on database objects:
    • If you are renaming a column, index, or table, ALTER permission is required on the object that you are renaming.
    • If you are renaming a database, you require membership in a sysadmin or dbcreator server role.
  2. When you are renaming any stored procedure, triggers, and function using the sp_rename procedure, the object name will not be changed in the sys.sql_modules catalog view. For example, if you are renaming a stored procedure, the name of the stored procedure will be unchanged in the definition column of sys.sql_modules ; therefore, Microsoft suggests dropping and recreating the stored procedure with a new name.
  3. When we rename any index, the associated statistics and query execution plan will be changed automatically. Also, if you rename any Primary Key index, the name of the Primary key constraint will be renamed.
  4. When we rename the database object and the renamed object is referenced by another object, then referencing object will be invalid. For example, if you are renaming a table and the same table is being referenced by a stored procedure, then we must change the definition of the stored procedure; otherwise, you will encounter an “Invalid Object Name” error. These broken objects are known as Invalid objects. You can find them by querying sys.sql_expression_dependencies. Alternatively, you can use dbForge Studio for SQL Server, which provides a facility to identify invalid objects. It also has other interesting features that help DBAs, Developers, and database architects to make their job easy and productive. 

Summary

In this article, we learn different ways to rename any database objects in SQL Server. We learned the usage of sp_rename and SQL Server management studio to rename any database object.  

Database Object (computer science) sql

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Integrate AWS Secrets Manager in Spring Boot Application
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Java REST API Frameworks
  • Asynchronous Messaging Service

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: