• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
sqlserver tutorial

SQL Server Tutorial

SQL Server Tutorial for Beginners

  • HOME
  • START HERE
  • BASICS
  • ADVANCED
    • Index
    • Views
    • Triggers
    • Stored Procedures
    • User-defined Functions
  • FUNCTIONS
Home / SQL Server Basics / SQL Server RENAME TABLE

SQL Server RENAME TABLE

Learning Objective

The objective of this tutorial is to teach you how to rename an existing table in SQL Server database using Transact SQL and using SQL Server Management Studio(SSMS).

SQL Server RENAME TABLE Introduction

Sometime you may need to change an existing table name in the Server database. SQL Server does not provide any ALTER TABLE statement to rename any table name. But it does provide a stored procedure by which you can modify a table name. Alternatively, you can the SQL Server Management Studio to rename a table.

1) RENAME TABLE using Transact SQL

As mentioned above, SQL Server does not provide any direct ALTER statement to rename a table. However, it does provide a store procedure named sp_rename to modify a table name.

SQL Server RENAME TABLE Syntax

The following shows the syntax of RENAME TABLE in SQL Server.

EXEC sp_rename 'old_table_name', 'new_table_name';

Note that, both old table name and new table name must be enclosed with single quotes.

SQL Server RENAME TABLE Example

First, create a table named emp in company database. By default the table will be created in the dbo schema under company database.

CREATE DATABASE company;

USE company;

CREATE TABLE emp
(
emp_id INT PRIMARY KEY IDENTITY (1,1),
first_name nVARCHAR(50) NOT NULL,
last_name nVARCHAR(50) NOT NULL,
dob DATE NOT NULL,
join_date DATE NOT NULL,
phone nVARCHAR(20),
dept_id INT NOT NULL
);
SQL Server rename table example

Now, if you want to change the name of the table from emp to employee, you need to use sp_rename store procedure to rename the table as follows.

EXEC sp_rename 'emp', 'employee';

SQL Server shows the below message.

Caution: Changing any part of an object name could break scripts and stored procedures.

However, it renamed the table successfully as shown in the below image.

2) RENAME TABLE using SSMS

The second method is for renaming table in SQL Server is by using SQL Server Management Studio.

In this example, we change the name of the employee table to emp again.

For this, first right click on the table name and select Rename option.

SQL Server rename table

Second, give the desire name you want to keep and press enter.

SQL Server rename table example

Now you can see that the table name has been rename to emp from employee.

Summary

In this tutorial, you have learned how to rename an existing table in SQL Server database using both Transact SQL and using SQL Server Management Studio.

Was this tutorial helpful?
YesNo
« Previous: SQL Server CREATE TABLE
SQL Server DROP TABLE :Next »

Primary Sidebar

DATA MANIPULATION

  • SELECT
  • SELECT TOP
  • SELECT DISTINCT
  • OFFSET FETCH
  • ORDER BY
  • GROUP BY
  • BETWEEN
  • LIKE
  • ALIAS
  • HAVING
  • AND
  • OR
  • IN
  • WHERE
  • SELECT INTO
  • INSERT
  • INSERT Multiple rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE
  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE CONSTRAINT
  • NOT NULL CONSTRAINT
  • SUBQUERY
  • CORRELATED SUBQUERY
  • JOINS
  • CROSS JOIN
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL JOIN
  • SELF JOIN
  • UPDATE Join
  • CASE
  • COALESCE
  • NULL
  • NULLIF
  • UNION
  • INTERSECT
  • MERGE
  • EXCEPT
  • EXISTS
  • GROUPING SET
  • PIVOT
  • ROLLUP
  • CUBE

DATA DEFINITION

  • CREATE DATABASE
  • DROP DATABASE
  • CREATE SCHEMA
  • ALTER SCHEMA
  • DROP SCHEMA
  • CREATE TABLE
  • RENAME TABLE
  • DROP TABLE
  • TRUNCATE TABLE
  • IDENTITY column
  • Sequence
  • ALTER TABLE ADD Column
  • ALTER TABLE ALTER Column
  • ALTER TABLE DROP Column

Footer

About

SQLServerTutorial.org provides free tutorials and guide on SQL Server for Developers, Database Administrators, and Solution Architects who want to get started SQL Server quickly.

Recent Posts

  • SQL Server DROP VIEW
  • SQL Server Indexed View
  • Check view definition
  • SQL Server Rename View
  • SQL Server List Views

Quick Links

  • About
  • Contact Us
  • Privacy Policy
  • SQL Server Index
  • SQL Server Views
  • Terms of Use

Copyright © 2023 www.sqlservertutorial.org. All Rights Reserved.