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

Should I use an int or a long for the primary key in an entity framework model

I am writing an MVC5 Internet application and I have a question about the id field for a model. Should I use an int or a long for the id in a model? I am talking about the field that is used for the primary key? Is an int enough if the database has…
Simon
  • 7,991
  • 21
  • 83
  • 163
28
votes
7 answers

Adding column with primary key in existing table

I am trying to add primary key as newly added column Product_Detail_ID (int and not null) in existing table name Product_Details. But I am gettin an error. (There are no other primary or foreign keys assigned to this table.) ALTER TABLE…
Joy1979
  • 599
  • 5
  • 12
  • 23
27
votes
8 answers

Why is it not good to have a primary key on a join table?

I was watching a screencast where the author said it is not good to have a primary key on a join table but didn't explain why. The join table in the example had two columns defined in a Rails migration and the author added an index to each of the…
pez_dispenser
  • 4,394
  • 7
  • 37
  • 47
27
votes
11 answers

What is the difference b/w Primary Key and Unique Key

I tried to find it out in google but not satisfactory answer is given out there. Can anybody explain the solid difference. actually if Primary key is used to select data uniquely then what is the need of Unique key? When should I use a Primary…
nectar
  • 9,525
  • 36
  • 78
  • 100
27
votes
2 answers

How large can an id get in postgresql

I am using postgresql, and was wondering how large id INTEGER PRIMARY KEY can get compared to id SERIAL PRIMARY KEY In java an int is 4 bytes (32 bits) so it can get up to 2,147,483,647. Is this the case in postgresql? If so does that mean I…
abden003
  • 1,325
  • 7
  • 24
  • 48
26
votes
1 answer

String as Primary Key in Laravel migration

I've had to change a table in my database so that the primary key isn't the standard increments. Here's the migration, public function up() { Schema::create('settings', function (Blueprint $table) { $table->text('code', 30)->primary(); …
DGeo
  • 275
  • 1
  • 3
  • 7
26
votes
3 answers

How to change the column length of a primary key in SQL Server?

I know how to change the length of a column, but my SQL statement fails because the column I'm trying to change is a PK, so I get the following error: Msg 5074, Level 16, State 1, Line 1 The object 'PK_TableName' is dependent on column…
LearnByReading
  • 1,813
  • 4
  • 21
  • 43
26
votes
6 answers

How do I rename a primary key column in MySQL?

How do I rename a primary key column in MySQL?
Vinicius Rocha
  • 4,023
  • 4
  • 29
  • 38
26
votes
1 answer

Should I index primary key column(s) in Oracle

I've recently stopped to think that Primary Keys are not indexes, they're a combination of Unique and Null constraints. And till now, I've never created index for PK columns. My question is if I should create index for PK columns if this column is…
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
25
votes
6 answers

SQL Server Change Primary Key Data Type

I am working on SQL Server 2012: I have a table with a primary key column as INT. I need to change this to a GUID. Do I alter the table and remove int column as primary key? Add the GUID column and set it as Primary and drop the old INT…
Guygar
  • 449
  • 1
  • 5
  • 8
25
votes
3 answers

2 foreign keys referencing same table

Can I have two foreign keys in the same table that are referencing an other table named profil(same one) ? my table is MailSent, it contains : primary key (Id), date, foreignkey1 (profil_sender), foreignkey2 (profil_receiver)
Najoua
  • 391
  • 1
  • 7
  • 15
25
votes
4 answers

Is a primary key automatically an index?

If I run Profiler, then it suggests a lot of indexes like this one CREATE CLUSTERED INDEX [_dta_index_Users_c_9_292912115__K1] ON [dbo].[Users] ( [UserId] ASC )WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF)…
Lieven Cardoen
  • 25,140
  • 52
  • 153
  • 244
25
votes
6 answers

Add primary key to a table with many records

I have a table in SQL Server 2005 containing 10000054 records; these records are inserted through a bulk insert operation. The table does not contain a primary key and I want to have one. If I try to modify the table's structure, adding a new…
lmsasu
  • 7,459
  • 18
  • 79
  • 113
25
votes
7 answers

Pros and cons of using md5 hash of URI as the primary key in a database

I'm building a database that will store information on a range of objects (such as scientific papers, specimens, DNA sequences, etc.) that all have a presence online and can be identified by a URL, or an identifier such as a DOI. Using these GUIDs…
rdmpage
  • 941
  • 2
  • 8
  • 9
25
votes
8 answers

What are the pros and cons of using multi column primary keys?

I would like to see an example of: When this is appropriate When this is not appropriate Is there a time when the choice of database would make a difference to the above examples?
Curtis Inderwiesche
  • 4,940
  • 19
  • 60
  • 82