0% found this document useful (0 votes)
16 views15 pages

Database Systems DBMS KEYS 7

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)
16 views15 pages

Database Systems DBMS KEYS 7

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/ 15

Database Systems

RDBMS KEYS

Dr. Waheed Anwar

Computer Science
Why we need Keys?
❑ 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 ensure that you can uniquely identify a table record
despite these challenges.
❑ Allows you to identify the relation between tables
❑ To establish a relationship between tables
❑ Help you to enforce identity and integrity in the
relationship.

Computer Science
What are Keys?
A RDBMS key is an attribute or set of an attribute which helps you to identify a
row(tuple) in a relation(table).
They allow you to find the relation between two tables.
RDBMS has flowing types of Keys each have their different functionality:
1. Super Key
2. Candidate Key
3. Primary Key
4. Alternate Key
5. Foreign Key
6. Compound Key
7. Surrogate Key

Computer Science
1-Super Key
A super key is a single or group of multiple
attributes which uniquely identifies row in a
table. A Super key may have additional attributes
that are not needed for unique identification.
Table Student
rollno name address phone age

1 Ali Hamza Bahawalpur 03007453324 18

2 Shazia Kanwal Multan 03023453534 18

3 Farha saif Bahawalpur 03048385444 20

4 Marwa Ahmad Lahore 18

Examples: rollno, phone, (rollno,name), (rollno,


address),(rollno,age),(rollno,phone)
(rollno,name,address)
Computer Science
2-Candidate Key
Minimal super key is the candidate key
Properties of Candidate key:
1. It must contain unique values
2. Candidate key may have multiple attributes
3. Must not contain null values
4. It should contain minimum fields to ensure uniqueness
5. Uniquely identify each record in a table

Examples: Table Student


StudID RollNo FirstName LastName Email
StudID, RollNo,
1 11 Waseem ali [email protected]
Email
2 12 Fozia ghaffar [email protected]

3 13 Dua azhar [email protected]


m

Computer Science
3-Primary Key
A selected attribute or group of attribute in a table which helps us to uniquely
identifies every row in that table is called a primary key. This DBMS can't be a
duplicate. The same value can't appear more than once in the table.
• 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 Table Student

Examples: rollno name address phone age

1 Ali Hamza Bahawalpur 03007453324 18


rollno, phone 2 Shazia Kanwal Multan 03023453534 18

3 Farha saif Bahawalpur 03048385444 20

Computer Science
4- Alternate Key
All the minimal super keys which are not primary key are
called an alternate key. It is a candidate key which is
currently not the primary key.
Example: In this table.
StudID, RollNo, Email are qualified to become a primary key. But since StudID
is the primary key, RollNo, Email becomes the alternative key.

Table Student
StudID RollNo FirstName LastNa Email
me

1 11 Waseem ali [email protected]

2 12 Fozia ghaffar [email protected]

3 13 Dua azhar [email protected]

Computer Science
Examples

Computer Science
5- Foreign Key
A foreign key is a column which is added to
create a relationship with another table. It is
a primary key in other table. Foreign keys
help us to maintain data integrity and also
allows navigation between two different
instances of an entity.

Computer Science
Foreign Key Example
Table Student
DEPT_CO
ROLLNO NAME ADDRESS PHONE AGE DE

1 Ali Hamza Bahawalpur 03007453324 18 CS

2 Shazia Kanwal Multan 03023453534 18 CS

3 Farha saif Bahawalpur 03048385444 20 ECE

4 Marwa Ahmad Lahore 18 CV

Table DEPT
DEPT_CODE DEPT_NAME

CS COMPUTER SCIENCE

IT INFORMATION TECHNOLOGY

ECE ELECTRONICS AND COMMUNICATION ENGINEERING

CV CIVIL ENGINEERING

Computer Science
6- Compound Key
Compound key has many fields which allow you to uniquely
recognize a specific record. It is possible that each column
may be not unique by itself within the database. However,
when combined with the other column or columns the
combination of composite keys become unique.

OrderNo PorductID Product Name Quantity

B005 JAP102459 Mouse 5

B005 DKT321573 USB 10

B005 OMG446789 LCD Monitor 20

B004 DKT321573 USB 15

B002 OMG446789 Laser Printer 3


Example:
(Order ID and Product ID) could be used as it uniquely identified
each record.
Computer Science
7- 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. They do
not lend any meaning to the data in the table.
Surrogate key is usually an integer.
Surrogate keys are allowed when
1. No property has the parameter of the primary key.
2. In the table when the primary key is too big or complicated.

Computer Science
7- Surrogate Key Example
Fname Lastname Start Time End Time

Asif Saeed 09:00 18:00

Jameel Fareed 08:00 17:00

Anas Ali 11:00 20:00

Shahid Wali 14:00 23:00

Above, given example, shown shift timings of the


different employee. In this example, a surrogate key is
needed to uniquely identify each employee.

Id Fname Lastname Start Time End Time

1 Asif saeed 09:00 18:00

2 Jameel Fareed 08:00 17:00

3 Anas ali 11:00 20:00

4 Shahid Wali 14:00 23:00

Computer Science
Keys
A Super Key in DBMS can identify every attribute in a database in
Super Key
a unique way. Hence, it is effectively just a key.

Candidate A candidate key requires identifying or recognizing any table row


Key uniquely while having the fewest available properties.

A key that includes multiple attributes is referred to as a composite


Composite
key. The key (Section, Roll Number) in the student table given
Key above is a Composite Key.

Alternate The remaining keys are known as Alternate Keys when a key is
Key selected from the collection of candidate keys.

Primary The primary key is selected as one candidate key from collecting all
Key potential candidate keys.

A key in another table Y that is leveraged to identify the rows in


Foreign Key table Y from the perspective of table X is known as a foreign key in
table X.

Computer Science
Difference b/w Primary & Foreign Keys
Primary Key Foreign Key

Helps you to uniquely identify a record in It is a field in the table that is the primary key
the table. of another table.

Primary Key never accept null values. A foreign key may accept multiple null
values.

Primary key is a clustered index and data A foreign key cannot automatically create an
in the DBMS table are physically index, clustered or non-clustered. However,
organized in the sequence of the clustered you can manually create an index on the
index. foreign key.

You can have the single Primary key in a You can have multiple foreign keys in a
table. table.

Computer Science

You might also like