In the above-given example, employee ID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID. In this tutorial, you will learn:
What are Keys?
Why we need a Key?
Various Keys in Database Management System
What is Super key?
What is Primary Key?
What is Alternate key?
What is Candidate Key?
What is Foreign key?
What is Compound key?
What is Composite key?
What is Surrogate Key?
Difference Between Primary key & Foreign key
Why we need a Key?
Here are some reasons for using sql key in the DBMS system.
Keys help you to identify any row of data in a table. In a real-world application, a table could contain thousands of records. Moreover, the records could be duplicated. Keys in RDBMS ensure that you can uniquely identify a table record despite these challenges. Allows you to establish a relationship between and identify the relation between tables Help you to enforce identity and integrity in the relationship.
What is the Super key?
A superkey is a group of single or multiple keys which identifies rows in a table. A Super key may have additional attributes that are not needed for unique identification.
Super Key Primary Key Candidate Key Alternate Key Foreign Key Compound Key Composite Key Surrogate Key
Let’s look at each of the keys in DBMS with example:
Super Key – A super key is a group of single or multiple keys which identifies rows in a table. Primary Key – is a column or group of columns in a table that uniquely identify every row in that table. Candidate Key – is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no repeated attributes. Alternate Key – is a column or group of columns in a table that uniquely identify every row in that table. Foreign Key – is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity. Compound Key – has two or more attributes that allow you to uniquely recognize a specific record. It is possible that each column may not be unique by itself within the database. Composite Key – is a combination of two or more columns that uniquely identify rows in a table. The combination of columns guarantees uniqueness, though individual uniqueness is not guaranteed. Surrogate Key – An artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key are unique because they are created when you don’t have any natural primary key.
Example: In the above-given example, EmpSSN and EmpNum name are superkeys.
Rules for defining Primary key:
Two rows can’t have the same primary key value It must for every row to have a primary key value. The primary key field cannot be null. The value in a primary key column can never be modified or updated if any foreign key refers to that primary key.
Example:
In the following example, StudID
is a Primary Key.
Example:
In this table, StudID, Roll No, Email are qualified to become a primary key. But since StudID is the primary key, Roll No, Email becomes the alternative key.
Properties of Candidate key:
It must contain unique values Candidate key in SQL may have multiple attributes Must not contain null values It should contain minimum fields to ensure uniqueness Uniquely identify each record in a table
Candidate key Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to uniquely identify the student record in the table.
Example: In this key in dbms example, we have two table, teach and department in a school. However, there is no way to see which search work in which department. In this table, adding the foreign key in Deptcode to the Teacher name, we can create a relationship between the two tables. This concept is also known as Referential Integrity. Example: In this example, OrderNo and ProductID can’t be a primary key as it does not uniquely identify a record. However, a compound key of Order ID and Product ID could be used as it uniquely identified each record. The difference between compound and the composite key is that any part of the compound key can be a foreign key, but the composite key may or maybe not a part of the foreign key. Above, given example, shown shift timings of the different employee. In this example, a surrogate key is needed to uniquely identify each employee. Surrogate keys in sql are allowed when
No property has the parameter of the primary key. In the table when the primary key is too big or complicated.
Summary
What is key in DBMS: A key in DBMS is an attribute or set of attributes which helps you to identify a row(tuple) in a relation(table) Keys in RDBMS allow you to establish a relationship between and identify the relation between tables Eight types of key in DBMS are Super, Primary, Candidate, Alternate, Foreign, Compound, Composite, and Surrogate Key. A super key is a group of single or multiple keys which identifies rows in a table. A column or group of columns in a table which helps us to uniquely identifies every row in that table is called a primary key All the different keys in DBMS which are not primary key are called an alternate key A super key with no repeated attribute is called candidate key A compound key is a key which has many fields which allow you to uniquely recognize a specific record A key which has multiple attributes to uniquely identify rows in a table is called a composite key An artificial key which aims to uniquely identify each record is called a surrogate key Primary Key never accept null values while a foreign key may accept multiple null values.