Different Types of Keys in SQL

Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Contenidos

Scope of the Article

→ In this article, we are going to discuss what is SQL, how SQL is helpful in daily usage, why should we use SQL 

→ SQL is a structured query language that is used to manage the data in the database or a table.

→ Next, we will discuss the different types of keys used in SQL.

→ Different types of keys include primary key, unique key, foreign key, candidate key, composite key, and more. We will be discussing them in the article.

Introduction 

SQL stands for Structured Query Language which is used for storing data or manipulating data or retrieving data in tables or databases

We can also perform some more operations like insertion of data, deletion of data, creation of tables, and modifying the existing content in the database. SQL is mainly designed for storing the data and manipulating data. SQL is used as a special tool for many users because it helps the users to main data in a structured format. 

We can handle relational databases using SQL by creating views, functions, and procedures. Anyways SQL allows users to manipulate data in the database. 

We had some commands which are used for manipulating, creating, updating, or managing the database. 

  1. CREATE 
  2. UPDATE
  3. DELETE
  4. SELECT 
  5. DROP
  6. INSERT

SQL allows Keys for identifying each row in the table. That can be identified by combining 1 or more columns. We have different types of keys that hat is used in SQL, and we will be discussing them.

espec-sql-server-1

Different types of keys in SQL

The different types of SQL are

  1. Primary key
  2. Unique key
  3. Candidate key
  4. Super key
  5. Alternate key
  6. Foreign key
  7. Composite key

We can use more than one key that is supported by SQL, multiple usages of keys are allowed in SQL. 

Let us know about each of the keys in detail.

Primary key:

⇒Primary keys are keys that are supported by SQL and used in the creation of SQL queries. 

⇒Primary keys are mainly responsible for defining a field or attribute in the database or the table uniquely, which means the values of the specified field should not be the same.

⇒ Primary keys don’t allow null values, so it will not allow the null values

⇒ Primary key can be declared while the creation of tables and should have only one attribute or one column. 

Ex:

If we take an example to understand what is the primary key in SQL, we will consider a table.

 

Student_id Student_name Student_roll Student_branch Student_address
1 Abc 512 CSE HYDERABAD
2 Xyz 212 EEE NEW DELHI
3 Pqr 412 ECE BANGALORE 
4 Rst 1212 IT MUMBAI
5 Mno 1712 ETE KERALA 

 

⇒ Here Student_id is considered the primary key which is always unique and cannot have null values. 

Unique key:

⇒ Unique keys are keys that are supported by SQL and used in the creation of SQL queries. 

⇒ Unique keys are mainly responsible to define a field or attribute in the database or the table uniquely, which means the values of the specified field should not be the same.

⇒ Unique keys allow null values, so they can have null values. 

⇒ Unique keys can be declared while the creation of tables and should have only one attribute or one column. 

⇒ Unique keys give an efficient and faster response. 

Ex:

If we take an example to understand what a unique key in SQL is, we will consider a table.

Student_id Student_name Student_roll Student_branch Student_aadhar_no.
1 Abc 512 CSE 814525253636
2 Xyz 212 EEE 369625854174
3 Pqr 412 ECE 963652857414 
4 Rst 1212 IT 852395214563
5 Mno 1712 ETE 814865154262

 

⇒ Here Student_aadhar_no. is considered a unique key which should always be unique and allows the null values. 

Candidate key:

⇒ Candidate keys are keys that are supported by SQL and every table should have at least 1 candidate key. 

⇒ Candidate keys are mainly responsible to define a field or attribute in the database or the table uniquely, which means the values of the specified field should not be the same, this means if an attribute is satisfied to be unique we call it a Candidate keys. 

⇒ Candidate keys can be one column or more than one column which are already unique.

Ex:

If we take an example to understand what is a Candidate key in SQL, we will consider a table.

Student_id Student_name Student_roll Student_branch Student_aadhar_no.
1 Abc 512 CSE 814525253636
2 Xyz 212 EEE 369625854174
3 Pqr 412 ECE 963652857414 
4 Rst 1212 IT 852395214563
5 Mno 1712 ETE 814865154262

 

⇒ Student_id and Student_aadhar_no. is considered candidate keys as those are already unique and also satisfy the property of both primary and unique. 

Super key:

⇒ Super keys are keys that are supported by SQL and each column of the table depends on the super key. 

⇒ Super keys are mainly responsible for defining a field or attribute in the database or the table; it is said to be a super key when it is a candidate key. 

⇒ So, candidate keys are said to be a subset of super keys. 

⇒ As discussed, Candidate keys are those which are unique.

Ex:

If we take an example to understand what is Super key in SQL, we will consider a table.

Student_id Student_name Student_roll Student_branch Student_aadhar_no.
1 Abc 512 CSE 814525253636
2 Xyz 212 EEE 369625854174
3 Pqr 412 ECE 963652857414 
4 Rst 1212 IT 852395214563
5 Mno 1712 ETE 814865154262

 

⇒ Student_id and Student_aadhar_no. is considered as Super keys as those which satisfy the property of both primary and unique. 

Alternate key:

⇒ Alternate keys are keys that are supported by SQL and are used as a substitute for the primary key. 

⇒ Alternate keys should satisfy the condition of the primary keys.

⇒ As primary keys are to be for only 1 field these can be used. But alternate keys are not primary keys.

Ex:

If we take an example to understand what Alternate keys are in SQL, we will consider a table.

Student_id Student_name Student_roll Student_branch Student_aadhar_no.
1 Abc 512 CSE 814525253636
2 Xyz 212 EEE 369625854174
3 Pqr 412 ECE 963652857414 
4 Rst 1212 IT 852395214563
5 Mno 1712 ETE 814865154262

 

⇒ Student_id used as the primary key and Student_aadhar_no. is considered Alternate keys as those which satisfy the property of primary (allows null values and should be unique) 

Composite key:

⇒ Composite keys are those which are also the type of Keys in SQL, which are used for combining two or more columns that help in identifying a particular row.

⇒ Composite keys sometimes act as primary keys or candidate keys.

Foreign Key:

⇒ Foreign keys are keys that are supported by SQL and used in the creation of SQL queries. 

⇒ Foreign keys are mainly responsible for defining a field or attribute in the database of one table using that in another table, which means we are referring to one table’s primary key in another table.

⇒ Foreign keys use the primary keys of another table.

Ex:

If we take an example to understand what is the foreign key in SQL, we will consider a table.

Student_info table:

Student_id Student_name Student_roll Student_branch Student_aadhar_no.
1 Abc 512 CSE 814525253636
2 Xyz 212 EEE 369625854174
3 Pqr 412 ECE 963652857414 
4 Rst 1212 IT 852395214563
5 Mno 1712 ETE 814865154262

 

Student_marks table:

Student_id Student_name Student_roll Student_marks
1 Abc 512 99
2 Xyz 212 86
3 Pqr 412 89
4 Rst 1212 88
5 Mno 1712 98

 

⇒Here Student_id is considered the primary key in a table named Student_info  which is always unique and cannot have null values. 

⇒Student_id is referred to as a foreign key in a table named Student_marks.

Conclusion:

  1. We discussed SQL and operations of the SQL,  and how SQL manages the data in the database or the tables.
  2. We knew the commands used in SQL, such as create, delete, update, select, drop and insert.
  3. Next, we went ahead and discussed the different types of keys available in SQL.
  4. We had discussed each of the key in an understandable way with examples. You can learn more about keys and other SQL topics from this SQL tutorial for beginners. Hope this article was helpful to get an overview on the types of keys in SQL. 

Suscribite a nuestro Newsletter

No te enviaremos correo SPAM. Lo odiamos tanto como tú.