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
33
votes
1 answer

How to create foreign key that is also a primary key in MySQL?

This should be a fairly straightforward question, but I'm unable to find an easy answer. How do you create a foreign key that is also a primary key in MySQL? Here's my current attempt: CREATE TABLE Sale( sale_id CHAR(40), PRIMARY…
user456584
  • 86,427
  • 15
  • 75
  • 107
33
votes
6 answers

Django + PostgreSQL: How to reset primary key?

I have been working on an application in Django. To begin with, for simplicity, I had been using sqlite3 for the database. However, once I moved to PostgreSQL, I've run into a bit of a problem: the primary key does not reset once I clear out a…
TM.
  • 108,298
  • 33
  • 122
  • 127
33
votes
7 answers

Generating a non-sequential ID/PK for a Django Model

I'm on the cusp of starting work on a new webapp. Part of this will give users pages that they can customise in a one to many relationship. These pages naturally need to have unique URLs. Left to its own devices, Django would normally assign a…
Oli
  • 235,628
  • 64
  • 220
  • 299
33
votes
2 answers

PostgreSQL using UUID vs Text as primary key

Our current PostgreSQL database is using GUID's as primary keys and storing them as a Text field. My initial reaction to this is that trying to perform any kind of minimal cartesian join would be a nightmare of indexing trying to find all the…
Scottie
  • 11,050
  • 19
  • 68
  • 109
33
votes
4 answers

Creating composite primary key in SQL Server

How to add composite primary keys in SQL Server 2008? I have a table as follows. testRequest (wardNo nchar(5) , BHTNo nchar(5) , testID nchar(5) , reqDateTime datetime); I need wardNo, BHTNo and testID to be a…
Dinithi De Silva
  • 1,142
  • 5
  • 28
  • 46
32
votes
2 answers

SQL Server: drop table primary key, without knowing its name

Using: SQL Server Database: Northwind I'd like to drop a table primary key, without knowing the PK constraint name.. eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is…
Bob
31
votes
9 answers

Why single primary key is better than composite keys?

Why is the rejection of composite keys in favor of all tables using a single primary key named id? Cause generally all ORM follow this. EDIT I just started learning ruby on rails and in the book of agile development by pragmatic there is a…
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
30
votes
3 answers

What size INT should I use for my autoincrement ids MySQL

Currently we're using INT(21)* for all autoincrement id columns in out 30+ table database. We are a blogging site, and have tables storing members, comments, blog posts and the like. I'm quite sure we will never reach the limit of our INT(21) id…
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130
30
votes
5 answers

Primary key in cassandra is unique?

It could be kind of lame but in cassandra has the primary key to be unique? For example in the following table: CREATE TABLE users ( name text, surname text, age int, adress text, PRIMARY KEY(name, surname) ); So if is it possible in my…
DarKAngeL
  • 361
  • 1
  • 3
  • 8
30
votes
2 answers

Is it bad to use user name as primary key in database design?

I was told by a friend: What unique key do you use? I hope you are not saving the entire user name --- this will use up too much table space! Assign an unique userID to each (unique) userNAME and save this userID (should be INTEGER…
Steven
  • 24,410
  • 42
  • 108
  • 130
29
votes
1 answer

MySQL - Using foreign key as primary key too

I have table 1 with a primary key user_id and table 2 where user_id is a foreign key. Only 1 record per user_id can exist in table 2, and no record can exist without it. QUESTION: Can user_id in table 2 be both foreign and primary key at the same…
29
votes
8 answers

Is ID column required in SQL?

Traditionally I have always used an ID column in SQL (mostly mysql and postgresql). However I am wondering if it is really necessary if the rest of the columns in each row make in unique. In my latest project I have the "ID" column set as my primary…
lanrat
  • 4,344
  • 10
  • 35
  • 41
29
votes
4 answers

Primary Key Type: int vs long

I know some software shops have been burned by using the int type for the primary key of a persistent class. That being said, not all tables grow past 2 billions. As a matter of fact, most don't. So, do you guys use the long type only for those…
Tom Tucker
  • 11,676
  • 22
  • 89
  • 130
29
votes
11 answers

Oracle (ORA-02270) : no matching unique or primary key for this column-list error

I have two tables, Table JOB and Table USER, here is the structure CREATE TABLE JOB ( ID NUMBER NOT NULL , USERID NUMBER, CONSTRAINT B_PK PRIMARY KEY ( ID ) ENABLE ); CREATE TABLE USER ( ID NUMBER NOT NULL , …
Rachel
  • 100,387
  • 116
  • 269
  • 365
29
votes
9 answers

Creating a Primary Key on a temp table - When?

I have a stored procedure that is working with a large amount of data. I have that data being inserted in to a temp table. The overall flow of events is something like CREATE #TempTable ( Col1 NUMERIC(18,0) NOT NULL, --This will not be an…
Frank V
  • 25,141
  • 34
  • 106
  • 144