Primary key

From Oracle FAQ
Jump to: navigation, search

A primary key is a column or a set of columns 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.

The value(s) of a primary key:

  • Must uniquely identify the row
  • Cannot have NULL values
  • Should be irreducible (no attribute can be removed without losing the uniqueness property)
  • Should not change over the time


Examples[edit]

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));

Also see[edit]

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 #