Lecture - 7
Lecture - 7
• 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 in RDBMS 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.
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.
There can be multiple super keys.
(EmpSSN), (EmpNum), (EmpSSN, EmpName), (EmpNum, EmpName), (EmpSSN,
EmpNum), (EmpSSN, EmpNum, EmpName) are possible superkeys from (EmpSSN,
EmpNum, EmpName).
CANDIDATE KEY in SQL 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.
Candidate key 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 is primary key from (StudID, Roll No, First Name, LastName, Email)
In this table, StudID, Roll No, Email is qualified to become a primary key. But since
StudID is the primary key, Roll No, Email becomes the alternative key.
FOREIGN KEY is a column that creates a relationship between two tables. The
purpose of foreign keys is to maintain data integrity and allow navigation between two
different instances of an entity. It acts as a cross-reference between two tables as it
references the primary key of another table.
(Deptcode, Deptname) and (Teacher_id, Lname, Fname)
In this key in dbms example, we have two table, teach and department in a school.
However, there is no way to see which teacher work in which department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can create
a relationship between the two tables.
COMPOUND KEY is created when two or more primary keys from different tables are
present as foreign keys within an entity. The foreign keys are used together to uniquely
identify each record.
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 in DBMS is usually an integer. A surrogate key is a
value generated right before the record is inserted into a table.
Adding of serial number when the table don’t have a primary key.