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
57
votes
5 answers

Primary & Foreign Keys in pgAdmin

I was wondering can some give me an explanation on how to assign primary and foreign keys in pgAdmin? I can't find any information online. For example...I've got a Student table with all their details (address, d.o.b. and etc.). I'm going to add a…
Mr Teeth
  • 1,269
  • 5
  • 19
  • 23
57
votes
10 answers

Determine a table's primary key using TSQL

I'd like to determine the primary key of a table using TSQL (stored procedure or system table is fine). Is there such a mechanism in SQL Server (2005 or 2008)?
rein
  • 32,967
  • 23
  • 82
  • 106
53
votes
5 answers

Python Pandas to_sql, how to create a table with a primary key?

I would like to create a MySQL table with Pandas' to_sql function which has a primary key (it is usually kind of good to have a primary key in a mysql table) as so: group_export.to_sql(con = db, name = config.table_group_export, if_exists =…
patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
53
votes
2 answers

Can we use Guid as a Primary Key in Sqlite Database

Is is possible to use GUID as primary Keys in SQLITE Database?If Possible which datatype can be used?
user2134137
  • 531
  • 1
  • 4
  • 6
52
votes
7 answers

Indexes and multi column primary keys

In a MySQL database I have a table with the following primary key PRIMARY KEY id (invoice, item) In my application I will also frequently be selecting on item by itself and less frequently on only invoice. I'm assuming I would benefit from indexes…
David Jenings
  • 593
  • 1
  • 4
  • 8
52
votes
15 answers

What is the difference between a primary key and a unique constraint?

Someone asked me this question on an interview...
Tim
  • 1,197
  • 3
  • 11
  • 7
51
votes
3 answers

Composite Primary Key performance drawback in MySQL

We have a table with a composite Primary key consisting of three fields (and it is in MySQL 5.1). There are near 200 inserts and 200 selects per second on this table, and the size of the table is around 1 million rows and it is increasing. My…
Ahmad
  • 513
  • 1
  • 4
  • 6
50
votes
4 answers

When does the JPA set a @GeneratedValue @Id

I have a simple JPA entity that uses a generated long "ID" as its primary key: @Entity public class Player { private long id; protected Player() { // Do nothing; id defaults to 0L } @GeneratedValue @Id public long getId()…
Raedwald
  • 46,613
  • 43
  • 151
  • 237
50
votes
4 answers

Primary and Foreign Key at the same time

Would it be possible in SQL Server 2008 to have a table created with 2 columns that are at the same time primary and foreign keys? If yes, how would such a code look like? I've searched and came up with nothing.
Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
50
votes
6 answers

Can we update primary key values of a table?

Can we update primary key values of a table?
akp
  • 1,823
  • 7
  • 26
  • 29
50
votes
3 answers

MySQL: is primary key unique by default?

If I define a column as a primary key in MySQL, is it also unique key by default or do I need to also define it as unique key (in case I want it to be unique)? I saw this question What is the difference b/w Primary Key and Unique Key that explain…
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
50
votes
5 answers

How to make a primary key start from 1000?

create table tablename ( id integer unsigned not null AUTO_INCREMENT, .... primary key id ); I need the primary key to start from 1000. I'm using MySQL.
user198729
  • 61,774
  • 108
  • 250
  • 348
50
votes
5 answers

Why can I create a table with PRIMARY KEY on a nullable column?

The following code creates a table without raising any errors: CREATE TABLE test( ID INTEGER NULL, CONSTRAINT PK_test PRIMARY KEY(ID) ) Note that I cannot insert a NULL, as expected: INSERT INTO test VALUES(1),(NULL) ERROR: null value in column…
A-K
  • 16,804
  • 8
  • 54
  • 74
47
votes
1 answer

Compound primary key in Table type variable

SQL Server 2008: DECLARE @MyTable TABLE( PersonID INT NOT NULL, Person2ID INT NOT NULL, Description NVARCHAR(100), CONSTRAINT PK PRIMARY KEY CLUSTERED (PersonID, Person2ID) ); Gives: Msg 156, Level 15, State 1, Line 5 Incorrect syntax…
pkario
  • 2,180
  • 6
  • 26
  • 30
46
votes
4 answers

How do I retroactively add a primary key to my table in rails?

I've created a table without a primary key (:id => false), but now it has come back to bite my ass. My app is already in production and I can't just drop it and recreate another one. Is there a way to run a migration to add another auto increment…
Jonathan Chiu
  • 1,637
  • 2
  • 16
  • 25