How to Create a Composite Primary Key in SQL Server?
Last Updated :
12 Dec, 2021
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 Key
When two or more Columns are together Identify the unique row in a table Known as Composite Primary Key. A composite key is a key that is the combination of more than one attribute or column of a given table. It may be a candidate key or a primary key.
We will implement with the help of an example for better understanding, first of all, we will create a database Name of the Database will Sample. and inside the database, we will Create a Compo.
Step 1: Creating a Database
For database creation, there is query we will use in SQL Platform.
Query:
Create database sample
Step 2: Use Database
For using the database we will use another query in SQL Platform like Mysql.
Query:
Use Sample
Step 3: Table Creation with composite primary
We will use the below query to create a Composite Key.
Query:
CREATE TABLE COMPO
(
EMP_ID INT,
DEPT_ID INT,
EMPNAME VARCHAR(25),
GENDER VARCHAR(6),
SALARY INT -->
//This statement will create a
//composite Primary Key from
PRIMARY KEY (EMP_ID,DEPT_ID)
with the help of Column EMP_ID and DEPT_ID
);
Step 4: After the creation of the table, we can Justify the view and metadata of the table with the help of the below Query. It will return Schema, column, data type, and size and constraints.
Query:
EXEC sp_help COMPO;
Output:

Step 5: INSERTION DATA IN TABLE.
We will use the below SQL query to insert data in Created Table.
Query:
INSERT INTO COMPO
VALUES (101,001,'RAHUL','MALE',22000),
(102,002,'RAJ','MALE',25000),
(103,003,'PRIYANKA','FEMALE',25500),
(102,003,'VIJAY','MALE',25000),
(101,004,'SHWETA','FEMALE',22000),
(104,003,'SATYA','MALE',23000),
(105,005,'VIVEK','MALE',28000);
Step 6: Verifying Inserted data
After Inserting data in the table We can justify or confirm which data we have to insert correctly or not. With the help of the Below Query.
Query:
SELECT * FROM COMPO
Output:

Step 7: As We know Primary Key has a unique value but in the above table, EMP_ID has a Duplicate value. Because it's alone(EMP_ID) is not a primary Key So that it can contain duplicate values. Similarly, DEPT_ID also has duplicate Value because it is also not a Primary Key. But in the above record, EMP_ID and DEPT_ID are not duplicate. Because it is a Composite Primary key. Here (EMP_ID + DEPT_ID) is uniquely Identifying the row in given above table.
For Finding Unique Value from COMPO, we will Execute like below.
Query:
SELECT EMPNAME,SALARY FROM COMPO WHERE EMP_ID= 102 AND DEPT_ID =6;
Output:
Similar Reads
Change Primary Key Column in SQL Server 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 i
2 min read
How to Index Foreign Key Columns in SQL Server Indexing foreign key columns in SQL Server is a vital optimization strategy for enhancing query performance, particularly when dealing with JOIN operations between related tables. Foreign keys enforce relationships between tables by linking a column (or columns) in one table to the primary key of an
4 min read
How to Create Id with AUTO_INCREMENT in SQL Server? Structured Query Language also known as SQL is a tool for storing, managing, and manipulating relational databases. SQL Server is a popular relational database management system (RDBMS) developed by Microsoft, providing a variety of operators to perform different operations on given datasets. In thi
4 min read
SQL Query to Create Table With a Primary Key A primary key is essential in SQL databases to uniquely identify each row in a table. It ensures data integrity by containing only unique and non-NULL values. A table can have only one primary key, which can consist of a single column or multiple columns, called a composite key.In this article, we w
5 min read
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
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 Reset Primary Key Sequence in PL/SQL Resetting a PL/SQL primary key sequence is an important task to ensure that the sequence values align with the current state of the table. When a sequence falls out of sync with the data in a table, it can lead to issues where new records receive primary key values that conflict with existing data.I
4 min read
How to Concatenate Text From Multiple Rows in SQL Server When we fetch data from a table, there may be requirements to concatenate the text value of a table column in multiple rows into a single row. There are many ways we can concatenate multiple rows into single row SQL Server. We can use different ways based on need and convenience. In this article, we
6 min read
How to SELECT DISTINCT on Multiple Columns in SQL Server? When working with SQL Server, there are scenarios where we might need to retrieve unique combinations of values from multiple columns. This is where the SELECT DISTINCT statement comes in handy. It allows us to eliminate duplicate rows from the result set. However, using SELECT DISTINCT it on multip
4 min read
How to Print Prime Numbers in MS SQL Server? In this article, we are going to print Prime numbers using MS SQL. Here we will be using 2 while loops statement for printing prime numbers. Steps 1: First we will DECLARE a variable I with initial value 2. Query: DECLARE @I INT=2Step 2: Then we will DECLARE a variable PRIME with an initial value of
3 min read