What are Constraints?

Constraints are used to control, reference, verify and restrict. Constraints can be of the following two formats.

Types of Constraints

Table Constraints Syntax

CREATE TABLE Command Syntax Including Constraints

CREATE TABLE [ schema.] table (
	column [ CONSTRAINT constraint ]
	{
		[ NOT ] NULL | UNIQUE | CHECK (condition)
		| PRIMARY KEY
		| REFERENCES [ schema. ] object  [ (column) ]
			 [ ON DELETE { CASCADE | SET NULL } ]
	} [ constraint state ]
	[ CONSTRAINT constraint ] constraint spec ]
		[, [ CONSTRAINT constraint ] constraint spec ] ]);

constraint spec: 
	{ UNIQUE (column [, column name ])
	| CHECK (condition)
	| PRIMARY KEY (column [, column ])
	| FOREIGN KEY (column [, column ])
		REFERENCES [ schema.] object { (column [, column ] ) }
			[ ON DELETE { CASCADE | SET NULL } ] constraint state

constraint state: [ [ [ NOT ] DEFERRABLE ] [ INITIALLY { IMMEDIATE | DEFERRED } ]
[ ENABLE | DISABLE ] [ VALIDATE | NOVALIDATE ] [ RELY | NORELY ] [ using index ] [ EXCEPTIONS INTO [ schema.] table ] ]

using index: [ [ schema.] index { [ CREATE INDEX ... ] [ TABLESPACE tablespace ] ] } ... ]

ALTER TABLE Command Syntax Including Constraints

ALTER TABLE [ schema.] table
ADD CONSTRAINT constraint table level constraint
MODIFY CONSTRAINT constraint constraint state
RENAME CONSTRAINT constraint TO constraint
DROP {
	  CONSTRAINT constraint [ CASCADE ]
	| { PRIMARY KEY | UNIQUE (column [, column ] ) } [ CASCADE ] 
		[ { KEEP | DROP } INDEX ] 
            }

constraint state: [ [ [ NOT ] DEFERRABLE ] [ INITIALLY { IMMEDIATE | DEFERRED } ]
[ ENABLE | DISABLE ] [ VALIDATE | NOVALIDATE ] [ RELY | NORELY ] [ using index ] [ EXCEPTIONS INTO [ schema.] table ] ]

using index: [ [ schema.]index { [ CREATE INDEX ... ] [ TABLESPACE tablespace ] ] } ... ]