Hi...
I know the concept of primary key and foreign key but I want to know the concept of composite key ? Please explain anyone with an example ?
Thanks........
Hi...
I know the concept of primary key and foreign key but I want to know the concept of composite key ? Please explain anyone with an example ?
Thanks........
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
SenthilkumarRank #387 · 6.5K reputationPosted Mar 12, 2012, 8:50 AM EDT
The key formed by combining two or more columns to enforce uniqueness of the row is called composite key.
For example,
I have created the ProductsMaster table. Lets take an example for "Soap" is a product. There can be more than one soap in the column but it should be different manfacturer. Here we need to make the product name and manfacturer is unique column.
In the above example it can be any number of soap in the product columns but it should have different manfacturers. When we try try to insert the combination of same product name and manfacturer then it will throw the duplicate insert error.
Here two columns enforces the uniqueness of the columns which typically table has two keys. It is called composite key.
If it is useful then mark it as "Accepted Answer".
VulpesRank #55 · 29.2K reputationPosted Mar 12, 2012, 8:37 AM EDT
There's an example here:
http://sqlzoo.net/howto/source/z.dir/tip241027/sqlserver
Vineet Kumar SainiRank #257 · 9.8K reputationPosted Mar 17, 2012, 9:18 PM EDT
geetha geethaRank #7,130 · 248 reputationPosted Nov 2, 2021, 11:08 AM EDT
Composite Key:
Composite key is a key which is the combination of more than one field or column of a given table.
Create table Stud_attendance (
STU_NAME VARCHAR(25) NOT NULL,
DATE_VALUE DATE NOT NULL,
CLASS_PERIOD INT NOT NULL,
PRESENCE_OF_STUDENT VARCHAR(15),
CONSTRAINT COMP_K PRIMARY KEY (STU_NAME, DATE_VALUE, CLASS_PERIOD)
);
Here in the above example, we are deriving the attendance of students present in each day. Here the columns “STU NAME”,” DATE”, “CLASS PERIOD” and “PRESENCE OF STUDENT” individually won’t be able to fetch a row as they consist of duplicates.
In the above example, we will consider “STU NAME”, “DATE” and “CLASS PERIOD” combine as a composite key, which will be used to fetch the rows from the table.