Total Page Preview:   000000000341

What is composite key in SQL in English and Hindi

 

Composite key in SQL in English

In SQL, a composite key is a key that is made up of two or more columns in a table. It is used when no single column in the table uniquely identifies each row, but the combination of columns does. A composite key is also known as a composite primary key, as it is often used as the primary key of the table.

Here's an example of how to create a composite key in SQL:
Let's say we have a table named "orders" with columns for order ID, customer ID, and product ID. We want to ensure that each combination of customer ID and product ID can only be used once in the table, so we create a composite key on the customer ID and product ID columns:
In this example, we've created a table called "orders" with five columns. The first column is the order ID, which we've set as the primary key. The second and third columns are for the customer ID and product ID. The fourth column is for the order date, and the fifth column is for the quantity of the product ordered.
 
We've also added a composite key constraint to the customer ID and product ID columns, which ensures that each combination of customer ID and product ID can only be used once in the table. This means that a customer can order a product multiple times, but each combination of customer ID and product ID can only appear once in the table.
If we try to insert a new row into the "orders" table with a combination of customer ID and product ID that already exists in the table, we'll get an error because the composite key constraint has been violated.
 
 
 
Composite key in SQL in Hindi
SQL mai, composite key aisa key hota hai jo table ke do ya do se zyada columns se bana hota hai. Ye tab use hota hai jab koi single column table ke har row ko uniquely identify nahi kar paata hai, lekin kuch columns ke combination se ye ho sakta hai. Composite key ko composite primary key bhi kaha jata hai, kyunki ye aksar table ke primary key ke roop mein use kiya jata hai.
Neechay ek SQL example diya gaya hai jismein composite key ka use kiya gaya hai:
Maan lijiye humare paas ek "orders" table hai jismein columns hai order ID, customer ID aur product ID ke liye. Hum chahte hain ki har customer ID aur product ID combination table mein sirf ek baar istemal ho, isliye hum customer ID aur product ID columns pe composite key lagaate hain:
Is example mein humne "orders" table create kiya hai jismein paanch columns hai. Pehla column order ID hai jo humne primary key set kiya hai. Dusra aur teesra column customer ID aur product ID ke liye hai. Chautha column order date ke liye hai aur paanchva column product ki quantity ke liye hai.
 
Humne customer ID aur product ID columns pe composite key constraint bhi add kiya hai, jisse ki har customer ID aur product ID combination table mein sirf ek baar hi istemal ho sakta hai. Iska matlab hai ki ek customer ek product ko multiple times order kar sakta hai, lekin har customer ID aur product ID combination table mein sirf ek baar hi appear ho sakta hai.
Agar hum "orders" table mein ek row insert karte hain jismein customer ID aur product ID ka combination pehle se table mein maujood hai to hume error milega kyunki composite key constraint ko violate kiya ja raha hai.
 
 
 
 
 

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments


                           
                           

                           

                           

Facebook User: