- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Database
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL - Composite Key
A Composite Key is a key that consists of two or more attributes (columns or fields) in a table. It can also be described as a Primary key created on multiple columns.Using the composite key, we can uniquely identify any record in a database table. A combination of these columns guarantees uniqueness, though the individual column may or may not guarantee for the uniqueness in a table.
A KEY is a set of one or more attributes whose combined values are unique among all occurrences in the given table.
Say we have a CUSTOMERS table with multiple columns from which we will select two columns named Adharcard_Id and Mobile_number and will combine them to make the Composite key, and that combination can be used to fetch any customers records uniquely in a table.
Let’s understand with a proper diagram −
Here are some important points about the Composite Key −
- A Composite Key may or may not be a part of the Foreign key.
- A Composite Key can not be NULL.
- A Composite Key also can be created by combining more than one Candidate Key.
- It is also known as Compound key.
- All the attributes in a compound keys are foreign keys.
Why do we need Composite Key?
When the database table doesn’t have any column which is alone capable of identifying a unique row (or a record) from the table, then we might need two or more two fields/columns to get a unique record/row from the table.
Syntax
Following is the syntax to create a COMPOSITE KEY while creating a table −
CREATE TABLE TABLE_NAME(COLUMN1, COLUMN2, COLUMN3…., CONSTRAINT COMPOSITE_KEY_NAME PRIMARY KEY(COLUMN1, COLUMN2,..);
Note − Here the COMPOSITE_KEY_NAME is the name of the Composite Key in a table. It is optional to specify this. It comes handy when you want to drop the composite key constraint from the column of the table.
Example
In the following example, we are trying to create a table named CUSTOMERS with multiple columns, and while creating the table will pass two columns named ADHARCARD_ID and MOBILE_NO to the PRIMARY KEY (ADHARCARD_ID, MOBILE_NO) to create a composite key on these columns.
Following is the statement to create table in SQL −
CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADHARCARD_ID BIGINT, MOBILE_NO BIGINT, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), CONSTRAINT CK_CUSTOMERS PRIMARY KEY (ADHARCARD_ID, MOBILE_NO) );
Where the CK_CUSTOMERS is the name of a composite of this table.
Output
Following is the output of the above statement −
(0 rows affected)
Verification
As we have created a Composite Key for the customer's table by using the columns ADHARCARD_ID, and MOBILE_NO, the values of these columns can not be duplicated.
First of all, lets insert a record into a customers table −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADHARCARD_ID,MOBILE_NO, ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 901234984567, 9021345687,'Ahmedabad', 2000.00 );
Now, lets insert one more record with the same adharcard_id and mobile number into the customers table −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADHARCARD_ID,MOBILE_NO,ADDRESS,SALARY) VALUES (2, 'Khilan', 25,901234984567,9021345687, 'Delhi', 1500.00 );
You can observe that the second INSERT statement generates an error message saying “Violation of PRIMARY KEY” as shown below −
Msg 2627, Level 14, State 1, Line 14 Violation of PRIMARY KEY constraint 'CK_CUSTOMERS'. Cannot insert duplicate key in object 'dbo.CUSTOMERS'. The duplicate key value is (901234984567, 9021345687).
Dropping the Composite Key
If you have created the Composite key for the customer's table, then there must also be a way to drop the composite key. You can do that by using the ALTER TABLE… DROP statement.
Syntax
Following is the syntax to drop the Composite key from the column of a table −
ALTER TABLE TABLE_NAME DROP COMPOISTE_KEY_NAME; (in SQL) ALTER TABLE TABLE_NAME DROP PRIMARY KEY;(in MYSQL)
Example
Using the following SQL statement, we can drop the Composite key constraint from the table −
ALTER TABLE CUSTOMERS DROP CK_CUSTOMERS;
Where CK_CUSTOMERS is the table composite key name.
Output
The above SQL statement produces the following output −
(0 rows affected)
Verification
Since, we have dropped the composite from the customer table, so now you can insert the duplicate value of the columns adharcard_id and mobile number.
Let’s insert two record with the same adharcard_id and mobile number into the Customers table −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADHARCARD_ID,MOBILE_NO, ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 901234984567, 9021345687,'Ahmedabad', 2000.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADHARCARD_ID,MOBILE_NO,ADDRESS,SALARY) VALUES (2, 'Khilan', 25,901234984567,9021345687, 'Delhi', 1500.00 );
If you verify the content of the table below, both the Customers have the same adharcard id and mobile number −
SELECT * FROM CUSTOMERS;
+----+--------+-----+--------------+------------+-----------+---------+ | ID | NAME | AGE | ADHARCARD_ID | MOBILE_NO | ADDRESS | SALARY | +----+--------+-----+--------------+------------+-----------+---------+ | 1 | Ramesh | 32 | 901234984567 | 9021345687 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | 901234984567 | 9021345687 | Delhi | 1500.00 | +----+--------+-----+--------------+------------+-----------+---------+