Keys in DBMS
Keys in DBMS
Alternate key
Foreign key
Composite key
Super key is a superset of all keys in table or we can say all keys in table.
rows/records in table.
A super key may have additional attributes that are not needed for unique identify.
A super key may have extra attributes that are not compulsory for identifying records.
Candidate key is minimal superkey without extra, unnecessary attributes. Candidate key cannot have NULL values.
Proper definition is “If a proper subset of any super key is also a super key then that super key cannot be a candidate
key.”
Emp_Id, Name Name, CNIC, Emp_Id
Name, CNIC Name, CNIC, DOB Emp_Id,Name, Name,CNIC,DOB,Email_id
Email, DOB Email_id, DOB,Name
Emp_Id
Emp_id Name CNIC DOB Email Id
CNIC
Email Id E1 Farhan 33100-xxxxxxx 22.1.90 [email protected]
E2 Wali 33101-xxxxxxx 15.3.89 [email protected]
Candidate Keys
E3 Hadi 33102-xxxxxxx 3.9.91 [email protected]
E4 Ahmad 33103-xxxxxxx 27.8.99 [email protected]
All candidate keys can be used for uniquely identifying of records but we can select any one among these.
A candidate key that is selected for uniquely identifying each row or record in table is called “Primary key.”
In the following candidate keys if we select Emp_id for identifying records then this Emp_id will be a primary
key.
While inserting record in table, value of Primary key should be given and mandatory.
Emp_id Name CNIC DOB Email Id
Emp_Id
CNIC E1 Farhan 33100-xxxxxxx 22.1.90 [email protected]
Email Id E2 Wali 33101-xxxxxxx 15.3.89 [email protected]
E3 Hadi 33102-xxxxxxx 3.9.91 [email protected]
Candidate Keys E4 Ahmad 33103-xxxxxxx 27.8.99 [email protected]
The candidate keys which are not selected as primary keys are called “Alternate keys”
In the following candidate keys if we select Emp_id for identifying records then this
Emp_id will be a primary key while CNIC and Email_id will be alternate keys.
Emp_Id
CNIC
Email Id
Candidate Keys
The primary key in one table serves as foreign key in other table.
The table whose primary key is used as foreign key is called “Master Table or Primary Table or Referenced
Table or Base Table” while table that contains foreign key is called “Child Table or Detailed Table or
Dependent Table or Referencing Table ”
Records cannot be inserted into detailed table if corresponding record does not exist in parent table.
E.g. we cannot insert course_ID 3404 as it does not exist in parent table.
Foreign key attribute can have only those values which are defined in master table.
Records cannot be deleted in master table if corresponding record exists in child table.
Foreign key may have Null values. Duplicate values for foreign key attributes are allowed.
Foreign Key S tudent
Course
Std_id Course_ID AGE Name
Primary
Key Course_ID Course_Name S1 3401 25 Farhan
3401 DLD S2 3403 23 Wali
Cannot delete this record S3 3401 24 Hadi Because corresponding
3402 DBMS
S4 3402 23 Ahmad record exists
3403 OOPS
A key which is combination of more than one attribute is called “Composite Key”.
Emp_Id, Name
Name, CNIC
Email, DOB
If one attribute in composite key is foreign key that this key is called “Compound Key”.
In this situation we develop an attribute artificially that can work as key for this table.