Change Primary Key Column in SQL Server
Last Updated :
23 Sep, 2021
Primary Key refers to the column of a table that uniquely identifies a row in a table. It contains unique values and can not contain NULL values. For the purpose of the demonstration, we will be creating geeks for geeks table in a database called “geeks“.
Step 1: Creating the database
The database is created in SQL Server using the following command
Query:
CREATE DATABASE geeks;
Step 2: Using the Database
Use the below SQL statement to switch the database context to geeks:
Query:
USE geeks;
In SQL Server, the Primary key constraint is defined by the syntax:
Syntax:
CREATE TABLE tablename (
column_name datatype NOT
NULL PRIMARY KEY);
We can also define the primary key after making the table, but make sure the column has the NOT NULL constraint on it. The syntax for declaring primary key after defining the table:
Syntax:
Alter table table_name add primary key (column_name);
To change the Primary key column in the SQL Server, follow these steps:
- Drop already defined primary key.
- Add a new column as the primary key.
Step 1: Table creation
We have the following geeks for geeks table in the geeks database
Query:
CREATE TABLE geeksforgeeks(
ID int PRIMARY KEY,
FIRSTNAME varchar(30),
LASTNAME varchar(30),
CITY varchar(18),
EmpID int NOT NULL
);
Step 2: Insert data into the table
Query:
INSERT INTO geeksforgeeks VALUES
(1,'Romy', 'Kumari', 'Delhi',1900089),
(2,'Avinav', 'Pandey', 'Delhi',1909089),
(3,'Nikhil', 'Kalra', 'Punjab',1000089),
(4,'Mansi', 'Lal', 'Uttarpradesh',1905689),
(5,'Rinkle', 'Arora', 'Haryana',1900989),
(6,'Sakshi', 'Kumari', 'Delhi',1700089),
(7,'Soumya', 'Shriya', 'Bihar',1660089),
(8,'Mitu', 'Kumari', 'Rajasthan',1340089);
Step 3: Check the content of table
Query:
SELECT * FROM geeksforgeeks;

Output:

Check which column is set as Primary key, we can use following command:
Query:
SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ;

Output:

In the above figure, you can see ID column is the primary key. We can drop this primary key by using the CONSTRAINT_NAME mentioned in the image. From here copy CONSTRAINT_NAME to drop the primary key.
Command to drop primary key:
Query:
ALTER TABLE geeksforgeeks DROP CONSTRAINT PK__geeksfor__3214EC275032BA6D;

Add EmpID as new Primary key (always check NOT NULL constraint is specified, otherwise, an error will be thrown)
Query:
ALTER TABLE geeksforgeeks ADD PRIMARY KEY (EmpID);

Now check the primary key of the table using ‘SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ‘ command
Output:

In the above image, you can see EMPID is the primary key.
Similar Reads
Primary key in MS SQL Server
A table has a particular number of columns and each column has n number of rows. At times, there might be a probability of repeated rows in a column. For example, a column named identification number has repeated rows. To avoid the duplication of rows, the concept of a key has been introduced. A key
2 min read
Rename Column in SQL Server
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. Renaming a column in a database is a common task usually required when users want to change the database schema. In this article, we will explore different methods
3 min read
Deleting a Column in SQL Server
Structure Query Language (SQL) is a standard language to manipulate and manage the database. SQL is a very powerful language for managing the database. SQL Server Delete command is one of the SQL commands that is used to remove the data that is not useful or due to which inconsistency occurred in th
5 min read
How to Create a Composite Primary Key in SQL Server?
In this article, We will learn what is a Composite Primary Key and How will create a Composite Primary Key. As We know a Primary key is the Candidate key that is selected to uniquely identify a row in a table. A And Primary Key does not allow NULL value. Composite Primary KeyWhen two or more Columns
3 min read
Foreign key in MS SQL Server
A foreign key in SQL Server plays a crucial role in establishing and enforcing relationships between tables. It is a column or a set of columns in a table that references the primary key or a unique key in another table. By using foreign key constraints the SQL Server keeps data consistent between r
6 min read
SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint in SQL is one of the most important constraints used to ensure data integrity in a database table. A primary key uniquely identifies each record in a table, preventing duplicate or NULL values in the specified column(s). Understanding how to properly implement and use the
5 min read
How to Change a Column Name in SQL?
The ALTER TABLE statement in SQL is a powerful command used to modify the structure of an existing table without affecting its data. It enables changes like adding, dropping, renaming or altering columns in the table. Among these operations, altering a column with the CHANGE or RENAME command is com
3 min read
Rename column SQL Server 2008
Renaming a column in a database is a common task that often arises when users want to change the database schema. In SQL Server 2008, renaming columns can be done efficiently using the sp_rename system-stored procedure, as the ALTER TABLE RENAME COLUMN syntax is not supported in this version. This a
4 min read
SQL Query to Alter Column Size in MySQL
In MySQL, managing the structure of a database is crucial for optimal data organization and integrity. One common task is altering the size of a column to accommodate changing the data requirements. The ALTER TABLE statement and the MODIFY clause provide a straightforward method to achieve this. Her
3 min read
ALTER SCHEMA in SQL Server
The SQL Server ALTER SCHEMA statement is used to transfer an object (table, view, or function) from one schema to another schema in the same database. The ALTER SCHEMA statement is used to transfer an object from a schema to another schema in the same database. SyntaxALTER SCHEMA target_schema_name
2 min read