Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we can't have more than one primary key?

I Know there can't be more than 1 primary key in a table but what is the technical reason ?

like image 407
Vishwanath Dalvi Avatar asked Aug 19 '11 19:08

Vishwanath Dalvi


People also ask

Why can't we have multiple primary keys?

A primary key is a field or set of fields with values that are unique throughout a table. Values of the key can be used to refer to entire records, because each record has a different value for the key. Each table can only have one primary key.

Can you have more than one primary key?

Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

Why do we only have one primary key?

Primary key will not accept NULL values whereas Unique key can accept NULL values. A table can have only one primary key whereas there can be multiple unique key on a table. A Clustered index automatically created when a primary key is defined whereas Unique key generates the non-clustered index.

Can we have 2 primary keys in a table?

You can have only one primary key, but that can consist of as many columns as you need to uniquely identify your rows. where P_Td and LastName are columns in your table. If you think you want more than one primary key, then the answer is "not really." You can have only one primary key.


1 Answers

Pulled directly from SO:

You can only have one primary key, but you can have multiple columns in your primary key.

You can also have Unique Indexes on your table, which will work a bit like a primary key in that they will enforce unique values, and will speed up querying of those values.

Primary in the context of Primary Key means that it's ranked first in importance. Therefore, there can only be one key. It's by definition.

It's also usually the key for which the index has the actual data attached to it, that is, the data is stored with the primary key index. Other indices contain only the data that's being indexed, and perhaps some Included Columns.

like image 147
Taryn Avatar answered Oct 01 '22 12:10

Taryn