What is primary key and unique key?

The primary key (or unique key) of a relational table is a candidate key to uniquely identify each row in a table.

A unique key must uniquely identify all possible rows that exist in a table and not only the currently existing rows. It can either be a normal attribute that is guaranteed to be unique (such as Social Security Number in a table with no more than one record per person) or it can be generated by the DBMS (such as a globally unique identifier, or GUID, in Microsoft SQL Server).

A primary key is a special case of unique key. Primary keys may consist of a single attribute or multiple attributes in combination. And it’s unique, what the difference from the unique key is the primary key cannot be NULL, and primary keys must be defined by using another syntax. Depending on its design, a table may have arbitrarily many unique keys but at most one primary key.

For example, if we have a Students table that contains a record for each student at a university. The student's unique student ID number would be a good choice for a primary key in the Students table. The student's first and last name would not be a good choice, as there is always the chance that more than one students might have the same name.

Defining primary keys:

defining_primary_key

Defining unique keys:

defining_unique_key


: What is data model?
: What is field?



Tags: primary key  unique key  table  database  

Readers also visit these:

What is data, database (DB), DBMS and DBS?
How to compare and synchronize the permissions?
How to compare and synchronize two database users?
How can I get technical support?
What is Database Integrity?

Related Questions:

What is Database Integrity?
What is foreign key?
What is data, database (DB), DBMS and DBS?
What is role?