blog

Home / DeveloperSection / Blogs / Keys in database.

Keys in database.

James Smith4877 01-Aug-2011

Keys in database

As we know that database uses tables to organize information. To maintain data integrity (that is data should be correct and in well formed) we use concept of keys.  There are five types of keys in database which is as follows---

1)      Candidate key

2)      Primary Key

3)      Foreign Key

4)      Alternate Key

5)      Composite Key.

Candidate Key

Candidate keys are those keys which is candidate for primary key of a table. In simple words we can understand that such type of keys which full fill all the requirements of primary key which is not null and have unique records is a candidate for primary key. So thus type of key is known as candidate key.

Primary Key

Such type of candidate key which is chosen as a primary key for table is known as primary key. Primary keys are used to identify tables. There is only one primary key per table. In SQL Server when we create primary key to any table then a clustered index is automatically created to that column.

Foreign Key

Foreign key are those keys which is used to define relationship between two tables. When we want to implement relationship between two tables then we use concept of foreign key. It is also known as referential integrity. We can create more than one foreign key per table.

Alternate Key

If any table have more than one candidate key, then after choosing primary key from those candidate key, rest of candidate keys are known as an alternate key of that table. Like here we can take a very simple example to understand the concept of alternate key. Suppose we have a table named Employee which has two columns EmpID and EmpMail, both have not null attributes and unique value. So both columns are treated as candidate key. Now we make EmpID as a primary key to that table then EmpMail is known as alternate key.

Composite Key

When we create keys on more than one column then that key is known as composite key. Like here we can take an example to understand this feature.  I have a table Student which has two columns Sid and SrefNo and we make primary key on these two column. Then this key is known as composite key.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By