We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14
Keys in DBMS
Dr. Anand Bihari
SJT Annex- 203N [email protected] What are Keys? A DBMS key is an attribute or set of an attribute which helps us to identify a row(tuple) in a relation(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. They allow us to find the relation between two tables. Keys help us to uniquely identify a row in a table by a combination of one or more columns in that table. Help you to enforce identity and integrity in the relationship. Keys in Database Management System DBMS has following seven types of Keys each have their different functionality: Super Key Primary Key Candidate Key Alternate Key Foreign Key Compound Key Composite Key Surrogate Key Super key A Super key 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. Example: EmpSSN EmpNum Empname 9812345098 AB05 Shown 9876512345 AB06 Roslyn Super Keys are 199937890 AB07 James {EmpSSN} {EmpNum} {EmpSSN, EmpNum} {EmpSSN, Empname} {EmpNum, Empname} {EmpSSN, EmpNum, Empname} Primary Key A column or group of columns in a table which helps us to uniquely identifies every row in table is called a primary key. The same value can't appear more than once in the table for primary key column. 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. Candidate Key The candidate keys in a table are defined as the set of keys that is minimal and can uniquely identify any data row in the table. The Primary key should be selected from the candidate keys. Every table must have at least one candidate 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 Super key vs Candidate Key All the candidate keys are super keys. This is because the candidate keys are chosen out of the super keys. How we choose candidate keys from the set of super keys? The keys from which cannot remove any fields. In the first example, we have not chosen {Emp_SSN, Emp_Name} as candidate key because {Emp_SSN} alone can identify a unique row in the table and Emp_Name is redundant. Alternate key All the keys which are not primary key are called an alternate key. It is a candidate key which is currently not the primary key. However, A table may have single or multiple choices for the primary key. Foreign key A foreign key is an attribute value in a table that acts as the primary key in another table. Hence, the foreign key is useful in linking together two tables. Data should be entered in the foreign key column with great care, as wrongly entered data can invalidate the relationship between the two tables. Compound key Compound key is the set of key attributes that allows us to uniquely recognize a specific record. It is possible that each column may be not unique by itself within the database. However, when combined with the other column or columns the combination of composite keys become unique. Composite key A key which has multiple attributes to uniquely identify rows in a table is called a composite key. 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. 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 Surrogate Key An artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key are unique because they are 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. 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
Above, given example, shown shift timings of the different employee.
In this example, a surrogate key is needed to uniquely identify each employee.