Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK not to use a Primary Key When I don't Need one

If I don't need a primary key should I not add one to the database?

like image 261
Luke101 Avatar asked Nov 20 '09 15:11

Luke101


People also ask

When would you not use a primary key?

A primary key is mainly formally defined to aid referencial Integrity, however if the table is very small, or is unlikely to contain unique data then it's an un-necessary overhead. Defining indexes on the table can normally be used to imply a primary key without formally declaring one.

What happens if you don't specify primary key?

If you insert a row without specifying the primary key, then SQL will automatically pick one for you that's different from other values. You can also specify AUTOINCREMENT after PRIMARY KEY .

Can primary key be optional?

A table cannot have more than one primary key, but it can have multiple unique keys. Primary keys are optional, and can be defined when a table is created or altered. They are also beneficial, because they order the data when data is exported or reorganized.

Does primary key matter?

Yes it does. By default, the primary key constraint is enforced in SQL Server by a unique clustered index. The clustered index defines the logical order of rows in the table.


2 Answers

You do need a primary key. You just don't know that yet.

like image 69
Otávio Décio Avatar answered Oct 24 '22 05:10

Otávio Décio


A primary key uniquely identifies a row in your table.

The fact it's indexed and/or clustered is a physical implementation issue and unrelated to the logical design.

You need one for the table to make sense.

like image 32
gbn Avatar answered Oct 24 '22 05:10

gbn