Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a database table contains more than one primary key?

Tags:

database

Can a database table contains more than one primary key?

Yes, I am talking about RDBMS.

like image 589
Yatendra Avatar asked Jan 06 '10 06:01

Yatendra


4 Answers

A table can have:

  • No primary keys;
  • One primary key consisting of one column; or
  • One composite primary key consisting of two or more columns.

Other than that you can have any number of unique indexes, which will do basically the same thing.

like image 102
cletus Avatar answered Nov 15 '22 12:11

cletus


The primary key of a relational table uniquely identifies each record in the table. So, in order to keep the uniqueness of each record, you cant have more than one primary key for the table. 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). Primary keys may consist of a single attribute or multiple attributes in combination.

like image 37
Zaki Avatar answered Nov 15 '22 10:11

Zaki


That's why it is called Primary Key because it is, well, PRIMARY

like image 30
junmats Avatar answered Nov 15 '22 10:11

junmats


Yes, you can have Composite primary keys, that is, having two fields as a primary key.

like image 45
Ali Avatar answered Nov 15 '22 11:11

Ali