Primary key

From Oracle FAQ
Jump to: navigation, search

A primary key is a column in a table whose values uniquely identify the rows in the table. The primary key is chosen from this list of candidates based on its perceived value to the business as an identifier.

A primary key value:

  • Must uniquely identify the row;
  • cannot have NULL values;
  • Should not change over the time; and
  • Should be as short as possible.

[edit] Examples

Single column primary key (inline definition):

CREATE TABLE t1 (id NUMBER PRIMARY KEY, val1 VARCHAR2(30));

Unnamed composite primary key (out-of-line - after column definitions):

CREATE TABLE t1 (
        c1 NUMBER,
        c2 VARCHAR2(30),
        c3 VARCHAR2(30),
        PRIMARY KEY (c1,c2));

Named composite primary key (out-of-line - after column definitions):

CREATE TABLE t1 (
        c1 NUMBER,
        c2 VARCHAR2(30),
        c3 VARCHAR2(30),
        CONSTRAINT t1_pk PRIMARY KEY (c1,c2));

[edit] Also see

Glossary of Terms
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #