Lecture 2-2 By Umair Shah Key • A Key is a data item that exclusively identifies a record. In other words, key is a set of column(s) that is used to uniquely identify the record in a table. • Uniquely identify a tuple in a relation (Special attribute). • It is used to fetch or retrieve records / data-rows from data table according to the condition/requirement. • Key provide several types of constraints like column can’t store duplicate values or null values. • Keys are also used to generate relationship among different database tables or views. Type of key • Candidate Key – A Candidate key is an attribute or set of attributes that uniquely identifies a record. Among the set of candidate, one candidate key is chosen as Primary Key. So a table can have multiple candidate key but each table can have maximum one primary key. – Example: • Possible Candidate Keys in Branch_Info table. – Branch_Id – Branch_Name – Branch_Code • Possible Candidate keys in Student_Information table. – Student_Id – College_Id – Rtu_Roll_No Type of key • Primary Key – A Primary key uniquely identifies each record in a table and must never be the same for the 2 records. Primary key is a set of one or more fields ( columns) of a table that uniquely identify a record in database table. A table can have only one primary key and one candidate key can select as a primary key. The primary key should be chosen such that its attributes are never or rarely changed, for example, we can’t select Student_Id field as a primary key because in some case Student_Id of student may be changed. – Example: • Primary Key in Branch_Info table: – Branch_Id • Primary Key in Student_Information Table: – College_Id Type of key • Foreign Keys: – Foreign key is used to generate the relationship between the tables. Foreign Key is a field in database table that is Primary key in another table. A foreign key can accept null and duplicate value. – Example: • Branch_Id is a Foreign Key in Student_Information table that primary key exist in Branch_Info(Branch_Id) table. Type of key • Super Key – Super key is a set of one or more than one keys that can be used to uniquely identify the record in table. A Super key for an entity is a set of one or more attributes whose combined value uniquely identifies the entity in the entity set. A super key is a combine form of Primary Key, Alternate key and Unique key and Primary Key, Unique Key and Alternate Key are subset of super key. A Super Key is simply a non-minimal Candidate Key, that is to say one with additional columns not strictly required to ensure uniqueness of the row. A super key can have a single column. Type of key – Example: • Super Keys in Branch_Info Table. – Branch_Id – Branch_Name – Branch_Code – { Branch_Id, Branch_Code } – { Branch_Name , Branch_Code } • Super Keys in Student_Information Table: – Student_Id – College_Id – Rtu_Roll_No – { Student_Id, Student_Name} – { College_Id, Branch_Id } – { Rtu_Roll_No, Session } Data Integrity • Data Integrity – Referential integrity is a subset of data integrity, which is concerned with the accuracy and consistency of all data (relationship or otherwise). Maintaining data integrity is a crucial part of working with databases. What is Referential Integrity? • Referential integrity refers to the accuracy and consistency of data within a relationship. • In relationships, data is linked between two or more tables. This is achieved by having the foreign key (in the associated table) reference a primary key value (in the primary – or parent – table). Because of this, we need to ensure that data on both sides of the relationship remain intact. • So, referential integrity requires that, whenever a foreign key value is used it must reference a valid, existing primary key in the parent table. Referential Integrity • Example – For example, if we delete record number 15 in a primary table, we need to be sure that there’s no foreign key in any related table with the value of 15. We should only be able to delete a primary key if there are no associated records. Otherwise, we would end up with an orphaned record. Referential Integrity • So referential integrity will prevent users from: – Adding records to a related table if there is no associated record in the primary table. – Changing values in a primary table that result in orphaned records in a related table. – Deleting records from a primary table if there are matching related records.