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
226
votes
8 answers

How to properly create composite primary keys - MYSQL

Here is a gross oversimplification of an intense setup I am working with. table_1 and table_2 both have auto-increment surrogate primary keys as the ID. info is a table that contains information about both table_1 and table_2. table_1 (id, field) …
filip
  • 3,036
  • 4
  • 22
  • 20
220
votes
15 answers

Strings as Primary Keys in MYSQL Database

I am not very familiar with databases and the theories behind how they work. Is it any slower from a performance standpoint (inserting/updating/querying) to use Strings for Primary Keys than integers? For Example I have a database that would have…
mainstringargs
  • 13,563
  • 35
  • 109
  • 174
218
votes
12 answers

Remove Primary Key in MySQL

I have the following table schema which maps user_customers to permissions on a live MySQL database: mysql> describe user_customer_permission; +------------------+---------+------+-----+---------+----------------+ | Field | Type | Null…
markb
  • 3,451
  • 5
  • 24
  • 25
212
votes
14 answers

Is there a REAL performance difference between INT and VARCHAR primary keys?

Is there a measurable performance difference between using INT vs. VARCHAR as a primary key in MySQL? I'd like to use VARCHAR as the primary key for reference lists (think US States, Country Codes) and a coworker won't budge on the INT…
Jake McGraw
  • 55,558
  • 10
  • 50
  • 63
194
votes
19 answers

Surrogate vs. natural/business keys

Here we go again, the old argument still arises... Would we better have a business key as a primary key, or would we rather have a surrogate id (i.e. an SQL Server identity) with a unique constraint on the business key field? Please, provide…
Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
186
votes
14 answers

Insert auto increment primary key to existing table

I am trying to alter a table which has no primary key nor auto_increment column. I know how to add an primary key column but I was wondering if it's possible to insert data into the primary key column automatically (I already have 500 rows in DB and…
FlyingCat
  • 14,036
  • 36
  • 119
  • 198
170
votes
9 answers

Is there AUTO INCREMENT in SQLite?

I am trying to create a table with an auto-incrementing primary key in Sqlite3. I am not sure if this is really possible, but I am hoping to only have to designate the other fields. For example: CREATE TABLE people (id integer primary key auto…
ewok
  • 20,148
  • 51
  • 149
  • 254
170
votes
4 answers

Is an index needed for a primary key in SQLite?

When an integer column is marked as a primary key in an SQLite table, should an index be explicitly created for it as well? SQLite does not appear to automatically create an index for a primary key column, but perhaps it indexes it anyway, given its…
Marek Jedliński
  • 7,088
  • 11
  • 47
  • 57
164
votes
4 answers

UUID max character length

We are using UUID as primary key for out oracle DB, and trying to determine an appropriate max character length for the VARCHAR. Apparently this is 36 characters but we have noticed UUID'S generated which are longer than this - up to 60 characters…
user1753862
  • 1,679
  • 2
  • 10
  • 4
164
votes
4 answers

How can I define a composite primary key in SQL?

How can I define a composite primary key consisting of two fields in SQL? I want to make a table name voting with fields QuestionID, MemberID, and vote. And the Composite primary key consists of the fields QuestionID and MemberID. How should I do…
Zeeshan Rang
  • 19,375
  • 28
  • 72
  • 100
159
votes
9 answers

Is it fine to have foreign key as primary key?

I have two tables: User (username, password) Profile (profileId, gender, dateofbirth, ...) Currently I'm using this approach: each Profile record has a field named "userId" as foreign key which links to the User table. When a user registers, his…
Duc Tran
  • 6,016
  • 4
  • 34
  • 42
154
votes
5 answers

SQL - many-to-many table primary key

This question comes up after reading a comment in this question: Database Design When you create a many-to-many table, should you create a composite primary key on the two foreign key columns, or create a auto-increment surrogate "ID" primary key,…
Andy White
  • 86,444
  • 48
  • 176
  • 211
140
votes
9 answers

Entity Framework and SQL Server View

For several reasons that I don't have the liberty to talk about, we are defining a view on our Sql Server 2005 database like so: CREATE VIEW [dbo].[MeterProvingStatisticsPoint] AS SELECT CAST(0 AS BIGINT) AS 'RowNumber', CAST(0 AS BIGINT) AS…
137
votes
3 answers

Updating MySQL primary key

I have a table user_interactions with 4 columns: user_1 user_2 type timestamp The primary key is (user_1,user_2,type) and I want to change to (user_2,user_1,type) So what I did was : drop primary key ... add primary key…
simplfuzz
  • 12,479
  • 24
  • 84
  • 137
128
votes
6 answers

How can I alter a primary key constraint using SQL syntax?

I have a table that is missing a column in its primary key constraint. Instead of editing it through SQL Server, I want to put this in a script to add it as part of our update scripts. What syntax can I use to do this? Must I drop and recreate the…
Jason Rae
  • 2,583
  • 5
  • 25
  • 34