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
1068
votes
11 answers

How can I do 'insert if not exists' in MySQL?

I started by googling and found the article How to write INSERT if NOT EXISTS queries in standard SQL which talks about mutex tables. I have a table with ~14 million records. If I want to add more data in the same format, is there a way to ensure…
warren
  • 32,620
  • 21
  • 85
  • 124
739
votes
9 answers

SQLite primary key on multiple columns

What is the syntax for specifying a primary key on more than 1 column in SQLite ?
Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74
713
votes
34 answers

How to reset Postgres' primary key sequence when it falls out of sync?

I ran into the problem that my primary key sequence is not in sync with my table rows. That is, when I insert a new row I get a duplicate key error because the sequence implied in the serial datatype returns a number that already exists. It seems…
meleyal
  • 32,252
  • 24
  • 73
  • 79
596
votes
22 answers

Unable to update the EntitySet - because it has a DefiningQuery and no element exist

I am using Entity Framework 1 with .net 3.5. I am doing something simple like this: var roomDetails = context.Rooms.ToList(); foreach (var room in roomDetails) { room.LastUpdated = DateTime.Now; } I am getting this error when I try to…
Positonic
  • 9,151
  • 14
  • 57
  • 84
505
votes
5 answers

What's the Hi/Lo algorithm?

What's the Hi/Lo algorithm? I've found this in the NHibernate documentation (it's one method to generate unique keys, section 5.1.4.2), but I haven't found a good explanation of how it works. I know that Nhibernate handles it, and I don't need to…
DiegoCofre
  • 5,053
  • 3
  • 17
  • 6
470
votes
12 answers

Can I have multiple primary keys in a single table?

Can I have multiple primary keys in a single table?
vaithi
426
votes
9 answers

What are the best practices for using a GUID as a primary key, specifically regarding performance?

I have an application that uses GUID as the Primary Key in almost all tables and I have read that there are issues about performance when using GUID as Primary Key. Honestly, I haven't seen any problem, but I'm about to start a new application and I…
VAAA
  • 14,531
  • 28
  • 130
  • 253
324
votes
16 answers

SQL Server add auto increment primary key to existing table

As the title, I have an existing table which is already populated with 150000 records. I have added an Id column (which is currently null). I'm assuming I can run a query to fill this column with incremental numbers, and then set as primary key and…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
314
votes
5 answers

What is Hash and Range Primary Key?

I am not able to understand what Range / primary key is here in the docs on Working with Tables and Data in DynamoDB How does it work? What do they mean by "unordered hash index on the hash attribute and a sorted range index on the range attribute"?
mannuscript
  • 4,711
  • 6
  • 26
  • 25
299
votes
15 answers

Difference between primary key and unique key

I'm using a MySQL database. In which situations should I create a unique key or a primary key?
Anuj
  • 3,049
  • 3
  • 15
  • 11
293
votes
9 answers

Difference between Key, Primary Key, Unique Key and Index in MySQL

When should I use KEY, PRIMARY KEY, UNIQUE KEY and INDEX?
HELP
  • 14,237
  • 22
  • 66
  • 100
286
votes
5 answers

How to add an auto-incrementing primary key to an existing table, in PostgreSQL?

I have a table with existing data. Is there a way to add a primary key without deleting and re-creating the table?
xRobot
  • 25,579
  • 69
  • 184
  • 304
265
votes
2 answers

Django queries - id vs pk

When writing django queries one can use both id/pk as query parameters. Object.objects.get(id=1) Object.objects.get(pk=1) I know that pk stands for Primary Key and is just a shortcut, according to django's documentation. However it is not clear…
Art
  • 23,747
  • 29
  • 89
  • 101
258
votes
12 answers

SQL-script: How to write ALTER statements to set Primary key on an existing table?

I have an existing table called Person which already has existing 1000 rows of data. In this table I have 5 fields: personId Pname PMid Pdescription Pamt The table already has an existing compound-key [PersonId, Pname] as the primary key. Now I…
jay
  • 3,699
  • 7
  • 28
  • 28
226
votes
6 answers

ALTER TABLE to add a composite primary key

I have a table called provider. I have three columns called person, place, thing. There can be duplicate persons, duplicate places, and duplicate things, but there can never be a dupicate person-place-thing combination. How would I ALTER TABLE to…
David542
  • 104,438
  • 178
  • 489
  • 842
1
2 3
99 100