May 15, 2015

Keys in SQL

  1. Primary Key
  2. Candidate Key
  3. Foreign Key
  4. Alternate Key
  5. Composite or Compound Key
  6. Unique Key


Candidate Key :


A Column with
  • Unique values
  • No null values


[A Table can have more than one column with above attributes. All these columns are candidate keys]


Primary Key:


A Column with
  • Unique values
  • No null values


[A Table can have more than one column with above attributes. But one of these column can be a primary key]


(Info: A table can have only one primary key but any number of candidate keys)


Alternate Key


When a table contains more than one candidate key (say 3 candidate keys), after selecting a column as a Primary key, other 2 columns are called as an Alternate Key


Unique Key:


A Column
  • with Unique values
  • can have null values


(Info: A table may not necessarily contain all the keys, but at least a primary key)

Composite Key:

Two or more columns collectively used to identify a row
Example: 
1. Identifying a student in a student table using his name and his father name
2. Identifying a student in a student table using his name, father name & Birth-date


Foreign Key:

  • Presence of a column in a table which is a primary key for another table.
  • This column is responsible for establishing relationship between two tables.

No comments:

Post a Comment