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
75
votes
3 answers

is primary key automatically indexed in postgresql?

I have created table name as d with ID column as primary key and then just inserted records as shown in output, but after fetching all records this output still displayed same as order in which records are inserted. but output as a see now not in…
Ram Talreja
  • 767
  • 1
  • 5
  • 3
74
votes
4 answers

How can I set two primary key fields for my models in Django?

I have a model like this: class Hop(models.Model): migration = models.ForeignKey('Migration') host = models.ForeignKey(User, related_name='host_set') How can I have the primary key be the combination of migration and host?
Vahid Kharazi
  • 5,723
  • 17
  • 60
  • 103
73
votes
5 answers

SQL: set existing column as Primary Key in MySQL

I have a database with 3 columns: id, name, somethingelse This table has no index set and i am getting "No index defined!" in phpmyadmin id is a 7digit alphanumeric value, unique to each row. I want to set Drugid to be the primarykey/index (i…
krasatos
  • 1,177
  • 3
  • 13
  • 26
72
votes
2 answers

SQL Server: how to add new identity column and populate column with ids?

I have a table with huge amount of data. I'd like to add extra column id and use it as a primary key. What is the better way to fill this column with values from one 1 to row count Currently I'm using cursor and updating rows one by one. It takes…
Sergejs
  • 2,540
  • 6
  • 32
  • 51
71
votes
1 answer

Change primary key in PostgreSQL table

I have users table in my PostgreSQL 9.3.6 database with two columns: id and another_id. The id is a primary key, the another_id is just another integer column with unique constraint. There are other tables that reference users by primary key. Here's…
Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202
70
votes
8 answers

What are the performance improvement of Sequential Guid over standard Guid?

Has someone ever measured performance of Sequential Guid vs. Standard Guid when used as Primary Keys inside a database? I do not see the need for unique keys to be guessable or not, passing them from a web UI or in some other part seems a bad…
massimogentilini
  • 4,102
  • 5
  • 29
  • 32
70
votes
6 answers

CREATE TABLE AS with PRIMARY KEY in one statement (PostgreSQL)

Is there a way to set the PRIMARY KEY in a single "CREATE TABLE AS" statement? Example - I would like the following to be written in 1 statement rather than 2: CREATE TABLE "new_table_name" AS SELECT a.uniquekey, a.some_value + b.some_value FROM…
TimY
  • 5,256
  • 5
  • 44
  • 57
68
votes
9 answers

Best way to reset an Oracle sequence to the next value in an existing column?

For some reason, people in the past have inserted data without using sequence.NEXTVAL. So when I go to use sequence.NEXTVAL in order to populate a table, I get a PK violation, since that number is already in use in the table. How can I update the…
Cade Roux
  • 88,164
  • 40
  • 182
  • 265
66
votes
5 answers

How to reset the primary key of a table?

In my table tbphotos I had a 100 records. I then deleted all the records and now that I want to restart data entry I see that my primary key doesn't start from 1, but it starts from 101, Is there any way to reset the primary key? I am using MySQL…
Kaveh
  • 2,530
  • 7
  • 29
  • 34
66
votes
3 answers

MySQL - Meaning of "PRIMARY KEY", "UNIQUE KEY" and "KEY" when used together while creating a table

Can anyone explain about the purpose of PRIMARY KEY, UNIQUE KEY and KEY, if it is put together in a single CREATE TABLE statement in MySQL? CREATE TABLE IF NOT EXISTS `tmp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` varchar(255) NOT NULL, …
sura2k
  • 7,365
  • 13
  • 61
  • 80
64
votes
1 answer

Composite Primary Key On Table Variable In Stored Procedure

I'm new to stored procedures and trying to add a composite primary key to a table variable. DECLARE @statistictemp TABLE ( MajorName VARCHAR(50) NOT NULL, SubName VARCHAR(50) NOT NULL, DetailedName VARCHAR(50) NOT NULL, …
Greg
  • 8,574
  • 21
  • 67
  • 109
62
votes
7 answers

make an ID in a mysql table auto_increment (after the fact)

I acquired a database from another developer. He didn't use auto_incrementers on any tables. They all have primary key ID's, but he did all the incrementing manually, in code. Can I turn those into Auto_incrementers now? Wow, very nice, thanks a…
Gene R
  • 1,555
  • 5
  • 19
  • 32
59
votes
6 answers

When should I use primary key or index?

When should I use a primary key or an index? What are their differences and which is the best?
Sein Kraft
  • 8,417
  • 11
  • 37
  • 39
58
votes
5 answers

What is the difference between a primary key and a index key

Can anyone tell me what is the difference between a primary key and index key. And when to use which?
pavan
  • 979
  • 3
  • 11
  • 21
58
votes
3 answers

Change Primary Key

I have a table in Oracle which has following schema: City_ID Name State Country BuildTime Time When I declared the table my primary key was both City_ID and the BuildTime, but now I want to change the primary key to three columns: City_ID …
Mohit BAnsal
  • 1,571
  • 5
  • 16
  • 14