Keys
• KEYS in DBMS is an attribute or set of attributes which helps you to
identify a row(tuple) in a relation(table).
• They allow you to find the relation between two tables.
• Keys help you uniquely identify a row in a table by a combination of one
or more columns in that table.
• Key is also helpful for finding unique record or row from the table.
Database key is also helpful for finding unique record or row from the
table.
Keys
Keys in DBMS, is an attribute or set of an attribute which helps you to identify a
row(tuple) in a relation(table). Keys allows you to find the relation between two
tables. Keys help you uniquely identify a row in a table by a combination of one or
more columns in that table.
Why do 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 establish a relationship between and identify the relation
between tables
❑ Help you to enforce identity and integrity in the relationship.
Types of Keys
• There are following 10 important keys in DBMS-
Types of Keys
There are following 10 important keys in DBMS-
1. Super key
2. Candidate key
3. Primary key
4. Alternate key
5. Foreign key
6. Partial key
7. Composite key
8. Unique key
9. Surrogate key
10. Secondary key
Primary Key
•It is the first key which is used to
identify one and only one
instance of an entity uniquely. An
entity can contain multiple keys
as we saw in PERSON table. The
key which is most suitable from
those lists become a primary key.
•In the EMPLOYEE table, ID can be
primary key since it is unique for
each employee. In the EMPLOYEE
table, we can even select
License_Number and
Passport_Number as primary key
since they are also unique.
•For each entity, selection of the
primary key is based on
requirement and developers.
Primary key
PRIMARY KEY is a column or group of columns in a table that uniquely
identify every row in that table. The Primary Key can't be a duplicate
meaning the same value can't appear more than once in the table. A table
cannot have more than one primary key.
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.
Example:
In the following example, <code>StudID</code> is a Primary Key.
StudID Roll No First Name LastName Email
1 11 Tom Price abc@[Link]
2 12 Nick Wright xyz@[Link]
3 13 Dana Natan mno@[Link]
Candidate Key
•A candidate key is an attribute or set
of an attribute which can uniquely
identify a tuple.
•The remaining attributes except for
primary key are considered as a
candidate key. The candidate keys are
as strong as the primary key.
For example: In the EMPLOYEE table,
id is best suited for the primary key.
Rest of the attributes like SSN,
Passport_Number, and License_Number,
etc. are considered as a candidate key.
candidate key
CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes. The Primary key should be
selected from the candidate keys. Every table must have at least a single candidate
key. A table can have multiple candidate keys but only a single primary key.
Properties of Candidate key:
✔ It must contain unique values
✔ Candidate key may have multiple attributes
✔ Must not contain null values
✔ It should contain minimum fields to ensure uniqueness
✔ Uniquely identify each record in a table
Example:
In the given table Stud ID, Roll No, and email are candidate keys which help us to
uniquely identify the student record in the table.
StudID Roll No First Name LastName Email
1 11 Tom Price abc@[Link]
2 12 Nick Wright xyz@[Link]
3 13 Dana Natan mno@[Link]
Super Key
• Super key is a set of an attribute which can
uniquely identify a tuple. Super key is a
superset of a candidate key.
• For example: In the above EMPLOYEE table,
for(EMPLOEE_ID, EMPLOYEE_NAME) the name
of two employees can be the same, but their
EMPLYEE_ID can't be the same. Hence, this
combination can also be a key.
• The super key would be EMPLOYEE-ID,
(EMPLOYEE_ID, EMPLOYEE-NAME), etc.
Super Key
A superkey is a group of single or multiple keys which identifies
rows in a table. A Super key may have additional attributes that are
not needed for unique identification.
EmpSSN EmpNum Empname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James
In the above-given example, EmpSSN and EmpNum name are
superkeys.
Alternate Key
• Candidate keys that are left
unimplemented or unused after
implementing the primary key are called
as alternate keys.
OR
• Unimplemented candidate keys are
called as alternate keys.
Alternate key
ALTERNATE KEYS is a column or group of columns in a table that
uniquely identify every row in that table. A table can have multiple choices for
a primary key but only one can be set as the primary key. All the keys which
are not primary key are called an Alternate Key.
Example:
In this table, StudID, Roll No, Email are qualified to become a primary key.
But since StudID is the primary key, Roll No, Email becomes the alternative
key.
StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.
com
2 12 Nick Wright xyz@gmail.
com
3 13 Dana Natan mno@yaho
[Link]
Foreign Key
•Foreign keys are the column of the table
which is used to point to the primary key of
another table.
•In a company, every employee works in a
specific department, and employee and
department are two different entities. So
we can't store the information of the
department in the employee table. That's
why we link these two tables through the
primary key of one table.
•We add the primary key of the
DEPARTMENT table, Department_Id as a
new attribute in the EMPLOYEE table.
•Now in the EMPLOYEE table,
Department_Id is the foreign key, and both
the tables are related.
Partial Key
•Partial key is a key using which all the
records of the table can not be identified
uniquely.
•However, a bunch of related tuples can be
Dependent_n
selected from the table using the partial key. Emp_no ame Relation
Mothe
Example- E1 Suman
r
Consider the following schema-
E1 Ajay Father
Department ( Emp_no ,
Dependent_name , E2 Vijay Father
Relation ) E2 Ankush Son
Here, using partial key Emp_no, we can not
identify a tuple uniquely but we can select a
bunch of tuples from the table.
Composite Key
• A primary key comprising of multiple attributes and not just a single
attribute is called as a composite key.
Compound
key
COMPOUND KEY has two or more attributes that allow you to uniquely
recognize a specific record. It is possible that each column may not be unique by
itself within the database. However, when combined with the other column or
columns the combination of composite keys become unique. The purpose of the
compound key in database is to uniquely identify each record in the table.
Example:
In this example, OrderNo and ProductID can't be a primary key as it does not
uniquely identify a record. However, a compound key of Order ID and Product
ID could be used as it uniquely identified each record.
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
Composite key and Surrogate key
COMPOSITE KEY is a combination of two or more columns that uniquely identify rows in
a table. The combination of columns guarantees uniqueness, though individually uniqueness
is not guaranteed. Hence, they are combined to uniquely identify records in a table.
The difference between compound and the composite key is that any part of the compound
key can be a foreign key, but the composite key may or maybe not a part of the foreign key.
SURROGATE KEYS is An artificial key which aims to uniquely identify each record is
called a surrogate key. This kind of partial key in dbms is unique because it is 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. A surrogate key is a value generated right before the
record is inserted into a table.
Fname Lastname Start Time End Time
Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00
Surrogate Key
• Surrogate key is a key with the following properties-
• It is unique for all the records of the table.
• It is updatable.
• It can not be NULL i.e. it must have some value.
Example-
• Mobile Number of students in a class where every student owns a
mobile phone.
Unique Key
• Unique key is a key with the following properties-
• It is unique for all the records of the table.
• Once assigned, its value can not be changed i.e. it is non-updatable.
• It may have a NULL value.
Example-
• The best example of unique key is Adhaar Card Numbers.
• The Adhaar Card Number is unique for all the citizens (tuples) of India
(table).
• If it gets lost and another duplicate copy is issued, then the duplicate copy
always has the same number as before.
• Thus, it is non-updatable.
• Few citizens may not have got their Adhaar cards, so for them its value is
NULL.
Secondary Key
• Secondary key is required for the indexing
purpose for better and faster searching.