In the relational database, key plays a significant role and is used to define specific table rows. It also establishes connecting tables.
Types of keys in DBMS
Primary key – A primary is a column or group of columns in a table that uniquely identifies tuples (rows) within that table.
Let`s take an example of primary keys in DBMS
Let ‘s take one example in order to understand the primary key concept. Three attributes appear in the following table: Emp ID, Emp Name & Emp Age. One attribute, or a set of more than one attribute, may be a primary key out of these three attributes.
The Emp Name attribute alone can not be a primary key, because more than one student may have the same name.
The Emp Age attribute alone can not be a primary key, because more than one student may be of the same age.
The Emp Id attribute alone is a primary key, since each student has a unique I d that identifies the student record in the table.
Table Name: EMP
Emp_Id | Emp_Name | Emp_Age |
101 | Steve | 23 |
102 | John | 24 |
103 | Robert | 28 |
104 | Steve | 29 |
105 | Carl | 29 |
Super Key – A super key is a set of one of more columns (attributes) that identify rows in a table in a unique way.
Candidate Key – The candidate key has no redundant attribute and is classified as a super key.
Alternate Key – One of the candidate keys is usually selected as the primary key, other keys are known as alternate or secondary keys.
Composite Key – A key which consists of more than one attribute in a table to identify unique rows (also known as records & tuples) is called the composite key.
Foreign Key – Foreign keys are table columns which point to another table ‘s primary key. They act as tables-to-table cross-reference.