Keys in Database are Primary Keys, Candidate key, Alternate key and Foreign Key.
Candidate key
- A candidate key is a field or set of fields that could be used as the primary key.
- Like a primary key, it must be unique, but it is not necessarily chosen as the primary key.
Primary Key
- A primary key is a unique field use to identify each record in a table.
- It ensures above rule that each record can be uniquely identified. That means No two records in the same table can have the same primary key value.
- It is often a single field. But if it can’t identify a record uniquely using single field, it can be a combination of fields.
- Among candidate keys, the most suitable key should be identified as primary key.
- Example, empNo, sid, product_id are primary keys
Alternate key
- An alternate key is any candidate key that is not chosen as the primary key.
- An alternate key can serve as a unique key for a record if the primary key is not used.
Example
- employeeID, firstName, LastName, address, NIC, telephone, email are some fields for an Employee Table.
- Candidate Keys – employeeID, NIC, telephone, email
- Primary Keys – employeeID
- Alternate Keys – NIC, telephone, email