What is PRIMARY KEY

In the database code, I see references to PRIMARY KEY

. What is a PRIMARY KEY and what's the difference if some of the columns in one of my tables form a PRIMARY KEY ... or don't form a PRIMARY KEY.

(Surprisingly, this question doesn't seem to have been asked before on SO)

+3


source to share


4 answers


1st normal form requires you to have a unique key so you can relate. If this requirement is not met (i.e. if you don't have unique keys) then your table will be just a heap, not a relation.

A primary key is more or less (that is, roughly speaking) one specially chosen unique key. The one who designs the scheme chooses it. The main difference between a PC and a unique key is that unique keys can contain NULL values, while PCs cannot. In addition, you can have multiple unique keys in a given table, but no more than one PC.

By making one of the unique keys the primary key, you allow other tables to easily point to that table through their foreign keys (FK). Technically, FKs (child tables) can also point to any unique key (from the parent table), but usually people use a primary key (PK) for this purpose, which (as said) is basically just one of the unique keys. This means that FK usually indicates PK.



See also:

What is the Difference Between Primary Key and Unique V / H Key

+1


source


A primary key is a key in a relational database that is unique for each record.

If the reference to a column in the database is the primary key, then this value will be unique. when you try to add another row to the value with the same primary key, it throws an error.



If you don't have a primary key in the column, you can add any non-unique values ​​as well.

+1


source


Structurally speaking, a primary key

is column

/ group of columns

having a unique index

and cannot be null

. And each table can have at most one primary key (although a primary key can have more than one field).

Semantically speaking, PK uniquely identifies a row in a table. Due to its limitations, you can be 100% sure that no two records have the same PK value. This allows you to optimize both SQL server and SQL client side

+1


source


A primary key (abbreviated as PK ) is a zero-constrained table index . The main conclusion that a table has a zero index must have a unique identifier for each row in the table . A primary key can be built on one column or several different columns of the same table, as well as on a regular index. Developers usually choose a unique identifier (eg "id") as the primary key, although there is no strict definition of what the primary key should be.

Relational databases use primary keys, which is the notion of atomicity in 1st normal form (1NF) to refer to a table row from any other table, which means you have the same data / row only once in the database and use its primary key to make the link.

The last important opinion about primary keys is better performance . Indexes usually help with performance problems in complex cases. However, in the case of a primary key, you can expect that you will frequently refer to table rows using the primary key as an access condition or where clause (e.g. SELECT * FROM mytable WHERE id = 3). You can expect your DBMS to organize data into storage for quick access using a primary key. Having no primary key in the table, you can expect some performance issues .

Further reading of the main keys:

0


source







All Articles