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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using AUTHID Parameter in Oracle PL/SQL
  • How To Generate Scripts of Database Objects in SQL Server
  • JSON-Based Serialized LOB Pattern
  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects

Trending

  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Scalability 101: How to Build, Measure, and Improve It
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  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.

By 
Nisarg Upadhyay user avatar
Nisarg Upadhyay
·
Mar. 17, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
3.4K 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.

Related

  • Using AUTHID Parameter in Oracle PL/SQL
  • How To Generate Scripts of Database Objects in SQL Server
  • JSON-Based Serialized LOB Pattern
  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!