Questions tagged [primary-key]

A key is a set of attributes that is irreducibly unique and non-nullable within a table.

A key is a set of attributes that is irreducibly unique and non-nullable within a table. Irreducible means that all the attributes of the key are necessary to guarantee uniqueness - remove any one attribute and the uniqueness property would be lost. A key may consist of zero, one or more attributes and a relational table (relation variable) must have at least one key and may have more than one.

When a table has more than one key then by convention one of the keys is designated the primary one (meaning it is the preferred identifier or has some other significance for the database designer or user). The others are called secondary keys or alternate keys.

In relational database design the primary key is frequently the one referenced by foreign keys in other tables. That's not always the case however. In principle a primary key is no different in features or function from a non-primary key. The primary-key tag is therefore appropriate for any discussion about keys in general.

Keys are more formally known as candidate keys or minimal superkeys.

Attributes of a key are called prime attributes. Attributes which are not part of any key are called non-prime.

In SQL, keys are optional on tables. Primary keys are defined in the ISO SQL Standard, through the PRIMARY KEY constraint. The syntax to add such a constraint to an existing table is defined in SQL:2003 like this:

ALTER TABLE <TABLE identifier> 
      ADD [ CONSTRAINT <CONSTRAINT identifier> ] 
      PRIMARY KEY ( <COLUMN expression> {, <COLUMN expression>}... )

The primary key can also be specified directly during table creation. In the SQL Standard, primary keys may consist of one or multiple columns. Each column participating in the primary key is implicitly defined as NOT NULL. Note that some DBMS require explicitly marking primary-key columns as NOT NULL.

CREATE TABLE TABLE_NAME (
    id_col  INT,
    col2    CHARACTER VARYING(20),
    ...
    CONSTRAINT tab_pk PRIMARY KEY(id_col),
    ...
)

If the primary key consists only of a single column, the column can be marked as such using the following syntax:

CREATE TABLE TABLE_NAME (
    id_col  INT  PRIMARY KEY,
    col2    CHARACTER VARYING(20),
    ...
)
4532 questions
45
votes
2 answers

Composite PRIMARY KEY enforces NOT NULL constraints on involved columns

This is one strange, unwanted behavior I encountered in Postgres: When I create a Postgres table with composite primary keys, it enforces NOT NULL constraint on each column of the composite combination. For example, CREATE TABLE distributors (m_id…
user3422637
  • 3,967
  • 17
  • 49
  • 72
44
votes
6 answers

MySQL: #1075 - Incorrect table definition; autoincrement vs another key?

Here is a table in MySQL 5.3.X+ db: CREATE TABLE members` ( `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, `memberid` VARCHAR( 30 ) NOT NULL , `Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , `firstname` VARCHAR( 50 ) NULL , …
Haradzieniec
  • 9,086
  • 31
  • 117
  • 212
44
votes
3 answers

Creating tables and problems with primary key in Rails

When I try to run the following code in Rails using Mysql2 as database manager: rake db:migrate I obtain the following error: rake aborted! "Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL:" Why do I get this error, if the primary key…
Hedley Quintana
  • 471
  • 1
  • 4
  • 5
43
votes
13 answers

GUID vs INT IDENTITY

Possible Duplicate: How do you like your primary keys? I'm aware of the benefits of using a GUID, as well as the benefits of using and INT as a PK in a database. Considering that a GUID is in essence a 128 bit INT and a normal INT is 32 bit, the…
CodeMonkey1313
  • 15,717
  • 17
  • 76
  • 109
43
votes
5 answers

How to add composite primary key to table

create table d(id numeric(1), code varchar(2)) After I create the above table how can I add a composite primary key on both fields and also a foreign key?
Domnic
  • 3,817
  • 9
  • 40
  • 60
42
votes
9 answers

SQL - Inserting a row and returning primary key

I have inserted a row with some data in a table where a primary key is present. How would one "SELECT" the primary key of the row one just inserted? I should have been more specific and mentioned that I'm currently using SQLite.
Samuel Moriarty
  • 958
  • 1
  • 8
  • 20
42
votes
2 answers

Changing primary key int type to serial

Is there a way to change existing primary key type from int to serial without dropping the table? I already have a lot of data in the table and I don't want to delete it.
MarisP
  • 967
  • 2
  • 10
  • 24
41
votes
3 answers

How to get primary key value with Entity Framework Core

We are currently using the method below which depends upon IObjectContextAdapter in an abstract Repository. From what I'm reading, it appears that anything related to ObjectContext is being chopped out of Entity Framework Core. The method below is…
41
votes
7 answers

Using text as a primary key in SQLite table bad?

Is it bad to have text as a primary key in an SQLite database? I heard that it's bad for performance reasons, is this true? And will the rowid be used as the actual primary key in such a case?
mpellegr
  • 3,072
  • 3
  • 22
  • 36
40
votes
3 answers

The differences between INT and UUID in MySQL

If I set the primary key to be INT type (AUTO_INCREMENT) or set it in UUID, what is the difference between these two in the database performance (SELECT, INSERT etc) and why?
孙为强
  • 427
  • 1
  • 4
  • 6
40
votes
1 answer

What are the pros and cons for choosing a character varying data type for primary key in SQL?

In the databases course that I did during my education (approx. 4 years ago), I thought that it is recommended avoiding the use of character strings as primary key's data type. Can someone tell me what are the pros and cons for choosing a character…
artaxerxe
  • 6,281
  • 21
  • 68
  • 106
40
votes
1 answer

database: primary key, Clustered or NonClustered

I am creating a database in SQL server 2008, CREATE TABLE Users ( U_Id INT NOT NULL FirstName VARCHAR(50) NOT NULL, LastName VARCHAR(50) NOT NULL, Email VARCHAR(200) Password VARCHAR(50) ) I want to make U_Id the primary key. I…
YtotheZ
  • 433
  • 1
  • 5
  • 7
39
votes
3 answers

Add primary key to PostgreSQL table only if it does not exist

I have simple table creating script in Postgres 9.1. I need it to create the table with 2-attributes PK only if it does not exist. CREATE TABLE IF NOT EXISTS "mail_app_recipients" ( "id_draft" Integer NOT NULL, "id_person" Integer NOT…
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
39
votes
2 answers

Can foreign key references contain NULL values in PostgreSQL?

As an example create table indexing_table ( id SERIAL PRIMARY KEY, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), ); Is there a difference between the following tables? Table 1: create table referencing_table ( indexing_table_id INTEGER…
Alex
  • 15,186
  • 15
  • 73
  • 127
39
votes
3 answers

MySQL InnoDB: autoincrement non-primary key

Is it possible to auto-increment a non-primary key? Table "book_comments" book_id medium_int timestamp medium_int user_id medium_int vote_up small_int vote_down small_int comment text comment_id medium_int Primary key ->…
ProfileTwist
  • 1,524
  • 1
  • 13
  • 18