Sunteți pe pagina 1din 16

Database Systems

7th Lec
Relational Keys
• Super key
• Candidate key
• Primary key
• Alternate key
• Foreign key
• Composite key
• Unique key
Key

• An attribute, or set of attributes, that uniquely identifies a tuple within


a relation.

• The keys are defined in the table to access or sequence the stored data
quickly and smoothly.
• Keys are used to create relationship between different tables.
Primary Key
• The primary key identifies every record in a table uniquely.
• Primary key is unique and can not be have Null Value.

• when we have two or more candidate keys, we have to decide which


of them becomes the primary key. The criterion for this decision is
based on the ease of use in the day-to-day working as well as data
entry.
• {CNIC#, SAP Code}
• Ideally a primary key is composed of only a single attribute.
Primary Key
To define a field as primary key, following
conditions had to be met;
Super Key
• A super key is a set of one or more attributes that are taken
collectively and can identify all other attributes uniquely.

• A super key may contain additional attributes that are not necessary
for unique identification.

• Consider Branch relation:


• {branchNo, street, city, postcode}
• {branchNo, street, city}
• {branchNo, city} or {branchNo, postcode}
Candidate Key
• A table can have more than one set of columns that could be chosen as
the key. These are called Candidate key.

• {CNIC #, SAP Code, Name, Designation, Passport No}

• Individual columns in a table that qualifies for uniqueness of each


row/tuple.
Candidate Key
Composite Key
• A key consisting of two or more columns is called as a composite key.

• There are situations when a single column can not constitute a key.
This is because a single column cannot uniquely identify every row in
the table. Instead we need to have two or more columns together in
order to identify every row in the table uniquely.
{ Supplier-ID, Prod-ID, Quantity }

{First-Name, Last-Name, DoB} Combine and join to make strong key


Alternate Key

• Any candidate key, which is not the primary key is an alternate key.

• Alternate keys or secondary are keys that may or may not identify a
record uniquely, but help in faster searching.
Alternate Key
Foreign Key
• A foreign key is a set of columns in one table, which is a primary key
in another table.

Branch (branchNo, street, city, postcode)

Staff (staffNo, Name, position, sex, DOB, salary, branchNo)


Foreign Key
Unique Key
• Unique key is same as primary key with the difference being the
existence of null.
• Unique key field allows one value as NULL value.
Assignment Submission date 12
• Choose one Database/Relation and apply all these keys.

S-ar putea să vă placă și