8. SQL Statements : CREATE TABLE : Constraints : Primary Key Constraint
 
Share this page                  
Primary Key Constraint
The primary key constraint is used to denote one or more columns to which other tables refer in referential constraints. A table can have only one primary key; the primary key for a table is implicitly unique and must be declared not null.
This is an example of a primary key constraint and a related referential constraint:
Referenced table:
CREATE TABLE partnumbers(partno INT PRIMARY KEY...);
Referencing table:
CREATE TABLE inventory(ipartno INT...
    FOREIGN KEY (ipartno) REFERENCES partnumbers);
In this case, the part numbers in the inventory table are checked against those in the partnumbers table; the referential constraint for the inventory table is a table-level constraint and therefore must specify the FOREIGN KEY clause. The referential constraint for the inventory does not specify the column that is referenced in the partnumbers table. By default, the DBMS checks the column declared as the primary key. For related details, see Referential Constraint.