0% found this document useful (0 votes)
2 views

Keys in DBMS

The document explains various types of keys in database management, including super keys, candidate keys, primary keys, alternate keys, foreign keys, composite keys, and surrogate keys. It details their definitions, characteristics, and relationships, emphasizing the importance of unique identification of records in tables. Additionally, it discusses how foreign keys maintain referential integrity between related tables.

Uploaded by

orwawajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Keys in DBMS

The document explains various types of keys in database management, including super keys, candidate keys, primary keys, alternate keys, foreign keys, composite keys, and surrogate keys. It details their definitions, characteristics, and relationships, emphasizing the importance of unique identification of records in tables. Additionally, it discusses how foreign keys maintain referential integrity between related tables.

Uploaded by

orwawajid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Instructor: Rimla Anwar

A Key is an attribute or set of attributes that uniquely


identifies any record/row/tuple from table.
It is also used to define and identify relationships among
table.

Following are different types of keys. Name Roll no Department


Farhan 1 CS
 Super key
Hadi 2 CS
 Candidate key Wali 3 CS

 Primary key Farhan 4 CS

 Alternate key

 Foreign key

 Composite key
 Super key is a superset of all keys in table or we can say all keys in table.

 A super key is an attribute or combination of attributes that can be used to identify

rows/records in table.

 A super key may have additional attributes that are not needed for unique identify.

 Superkeys: Name, CNIC


Name, DOB
Emp_IdName, Name,CNIC,DOB,Email_id
Extra attributes Name,Email_id
Emp_Id Emp_id,Email _id
CNIC Emp_id Name CNIC DOB Email Id
Email Id E1 Farhan 33100-xxxxxxx 22.1.90 [email protected]
Emp_Id, Name
E2 Wali 33101-xxxxxxx 15.3.89 [email protected]
Emp_id, CNIC
E3 Hadi 33102-xxxxxxx 3.9.91 [email protected]
Emp_id, DOB
Emp_id,Email _id E4 Ahmad 33103-xxxxxxx 27.8.99 [email protected]
 Candidate key is attribute or set of attribute that can uniquely identify each record in table.

 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.

 Value of primary key should be unique (No duplication)

 Value of primary key cannot be Null or Duplicate.

 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

Emp_id Name CNIC DOB Email Id


E1 Farhan 33100-xxxxxxx 22.1.90 [email protected]
E2 Wali 33101-xxxxxxx 15.3.89 [email protected]
E3 Hadi 33102-xxxxxxx 3.9.91 [email protected]
E4 Ahmad 33103-xxxxxxx 27.8.99 [email protected]
 Foreign key an attribute used for linking different tables.

 It makes relationship b/w tables.

 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 ”

Foreign Key Dep artment


Employee Primary Key
Emp_id Name CNIC Email Id Dept_Id Dept_Id Dept_Name
E1 Farhan 33100-xxxxxxx [email protected] 1 1 CS
E2 Wali 33101-xxxxxxx [email protected] 2 2 Account
E3 Hadi 33102-xxxxxxx [email protected] 1 3 English
E4 Ahmad 33103-xxxxxxx [email protected]
 Foreign key helps to maintain referential integrity of data.

 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

C_ID Name CNIC C_ID P_ID PRODUCT


C1 Farhan 33100-xxxxxxx C1 Marker
C2 Wali 33101-xxxxxxx C2
C3 Hadi 33102-xxxxxxx C3
C4 Ahmad 33103-xxxxxxx C4
 As we know that composite key consists of more than one attributes for uniquely identifying records.

 If one attribute in composite key is foreign key that this key is called “Compound Key”.

Primary Key Std_id+Course_ID


Primary
Key
Std_id Course_ID AGE Name

Course_ID Course_Name S1 3401 25 Farhan

3401 DLD S2 3403 23 Wali

3402 DBMS S3 3401 24 Hadi

3403 OOPS S4 3402 23 Ahmad


 Sometime a relation has no such attribute that can uniquely identify records in a table.

 In this situation we develop an attribute artificially that can work as key for this table.

 This key is called “Surrogate Key” or “Artificial Key”

Original Table without Key Attribute Surrogate Key


Name Age Dept City Sr.no Name Age Dept City
Farhan 30 CS Fsd 1 Farhan 30 CS Fsd
Ahmad 32 Com Fsd 2 Ahmad 32 Com Fsd
Ali 30 CS LHR 3 Ali 30 CS LHR
Farhan 33 IT ISB 4 Farhan 33 IT ISB

You might also like