DBMS Keys Asst.Prof. Rupali Lohar Dept. of Computer Science & Engineering B. R. Harne College Of Engineering & Technology, Karav, Post Vangani (W Tal Ambernath, Mumbai, Maharashtra 421503
Overview: What are Keys? Why we need a Key? Various Keys in Database Management System What is Super key? What is Primary Key? What is Alternate key? What is Candidate Key? What is Foreign key? What is Compound key? What is Composite key? What is Surrogate Key? Difference Between Primary key & Foreign key
Key- Key helps you to identify a row(tuple) in a relation(table). They allow you to find the relation between two tables. Key is also helpful for finding unique record or row from the table . Each record with in a table can be uniquely identified bye combination of one more fields in a table
Why we need a Key Keys help you to identify any row of data in a table . Help you to enforce identity and integrity in the relationship . Allows you to establish a relationship between and identify the relation between tables Keys help you uniquely identify a row in a table by a combination of one or more columns in that table.
Types of Keys in Database Management System
Primary key A primary key is a column in a table that uniquely identifies the rows in that table. The data values placed in the primary key column must be unique to each row; no duplicates can be used. Nulls are not allowed in primary key columns. A table cannot have more than one 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.
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 . 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
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.
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.
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 . A Super key may have additional attributes that are not needed for unique identification.
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.
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.
ALTERNATE KEYS 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.
COMPOSITE 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.
SURROGATE KEYS 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 . Surrogate keys in sql are allowed when No property has the parameter of the primary key. In the table when the primary key is too big or complicated.
Which one of the following is a set of one or more attributes taken collectively to uniquely identify a record? A. Candidate key B . Sub key C . Super key D . Foreign key
Answer: Option C Explanation: Super key Super key is the superset of all the keys in a relation.
Which one of the following cannot be taken as a primary key ? A. Id B. Register number C. Dept_id D. Street
Answer: Option D Explanation: Street Street is the only attribute which can occur more than once.