2. Keys
• Keys play an important role in the relational database.
• It is used to uniquely identify any record or row of data from the table.
• It is also used to establish and identify relationships between tables.
• Example:
3. Why we need a Key?
• To identify any row of data in a table.
• A table could contain thousands of records. (records duplicated)
• Keys in RDBMS ensure that can uniquely identify a table record
• To establish a relationship between and identify the relation between
table
• To enforce identity and integrity in the relationship
4. Types of Keys in DBMS
• Primary Key
• Super Key
• Candidate Key
• Alternate Key
• Foreign Key
• Compound Key
• Composite Key
5. Primary Key
• PRIMARY KEY in DBMS is a column or group of columns in a table that
uniquely identify every row in that table.
• 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
StudID-Primary key
6. Another example of primary key – More than one
attributes
• Table Name: ORDER
{Customer_ID, Product_ID} together can identify the rows uniquely in the
table so this set is the primary key for this table.
7. Define primary key in RDBMS
• Generally we define the primary key during table creation.
Create table ORDER
(Customer_ID int not null,
Product_ID int not null,
Order_Quantity int not null,
Primary key (Customer_ID, Product_ID) )
Another way:
(one attribute)
as primary key)
Create table STUDENT (
Stu_Id int primary key,
Stu_Name varchar(255) not null,
Stu_Age int not null )
8. Super key
• A super key is a group of multiple keys which identifies rows in a
table.
In the above-given example, EmpSSN and EmpNum name are superkeys.
9. Candidate Key
• A candidate key is an attribute or set of attributes that can uniquely
identify a tuple.
• Must not contain null values
• It should contain minimum fields to ensure uniqueness
• The Primary key should be selected from the candidate keys
• A table can have multiple candidate keys but only a single primary
key.
11. Alternate key
• ALTERNATE KEY 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.
• StudID is the primary key, Roll No, Email becomes the alternative key.
12. Foreign Key
• FOREIGN KEY is a column that creates a relationship between two tables.
• Foreign keys are the column of the table used to point to the primary key of
another table.
13. 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.
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.
14. Composite Key
• Whenever a primary key consists of more than one attribute, it is
known as a composite key. This key is also known as Concatenated
Key.
For example, in employee relations, we assume that an employee may be assigned multiple roles, and an
employee may work on multiple projects simultaneously. So the primary key will be composed of all three
attributes, namely Emp_ID, Emp_role, and Proj_ID in combination.