The answer of the question is very simple:
An entity types have many instances from few to several thousands. When we want to pick a particular instance or single instance out of many instances then key is the solution to do it. When defining a entity type then key is also defined for this entity type. Key is simple (consisting of single attribute) or composite (consisting of two or more attribute).
Detail definition of these keys is necessary to understand the purpose of these keys in the database. I am trying to explain the definition and example of these keys to explain the purpose of these keys in the database system.
Following are the different major keys in the database system:
1. Super key
2. Candidate key
3. Primary key
4. Alternate key
5. Foreign key
Super key
Attribute or combination of attribute that uniquely identifies a row in a table is called a super key. For example the relation of a student consist of a different attribute such as registration no, student name, father name, address, phone number, class etc. if we want to see a record of a particular student then what field is used for this purpose? If we use student name for searching a record then this cause a problem because there are many student have a same name such as ahmad, two student have the same name ahmad. In the same way father name, address is not suitable to search a record in a database. Registration no is a unique value that is assign to every student. Particular value of registration number will find a single unique entity instance. Super key with other key is always a super key such as (registration no, student name).
Candidate key
Minimal super key is a candidate key. It means that candidate key indentifies an entity instance uniquely and it should be a minimum. Super key that consist of an single attribute, it also a called a candidate key. For example in above example registration no and student name is a super key but it is not a candidate key because it object the second condition. Registration number attribute uniquely identifies an entity instance and does not contain an extra field. So registration key is a candidate key and also a super key.
Primary key
The key that is chosen by database designer to uniquely indentifies the entities and accessing a particular record in a database. Important point about primary key are
Primary key does not contain a null value.
Value of primary key must be unique.
A table has only one primary key.
For example registration number attribute in a relation. It can be used as a primary key if a relation has a two field Reg# and ID# then database designer chose one of them as a primary key (ID#).
Alternate key
If a relation has a two field Reg# and ID# then database designer chose one of them as a primary key (ID#). The other key that is not chosen as a primary is called an alternate key such as Reg#. Reg# is an alternate key and ID# is a primary key.
Foreign key
Sometime it can be happen that the primary key of one table is used in another table to link both table with each other. The attribute in a relation whose value match with primary key in another table this attribute is called a foreign key. A relation may contain many foreign keys. The table that contain foreign key is called a child table and the table from which foreign key is refers is called a parent table. The primary key is represented by underlining with a solid line, and foreign key is underlined by dashed or dotted line. Foreign key is zero, one and multiple in a relation it depends on how many tables a particular table is related with.
No comments:
Post a Comment