SQL Constraints: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 25: Line 25:
</syntaxhighlight>
</syntaxhighlight>


The primary key constraint can also be added after table creation with <code[[SQL_CREATE_ALTER_DROP_TABLE#Overview|ALTER TABLE]]</code>.
The primary key constraint can also be added after table creation with <code>[[SQL_CREATE_ALTER_DROP_TABLE#Overview|ALTER TABLE]]</code>.


=Foreign Key Constraint=
=Foreign Key Constraint=
Line 35: Line 35:
);
);
</syntaxhighlight>
</syntaxhighlight>
The foreign key constraint can also be added after table creation with <code[[SQL_CREATE_ALTER_DROP_TABLE#Overview|ALTER TABLE]]</code>.
The foreign key constraint can also be added after table creation with <code>[[SQL_CREATE_ALTER_DROP_TABLE#Overview|ALTER TABLE]]</code>.


=Check Constraint=
=Check Constraint=

Revision as of 00:09, 23 May 2024

Internal

Overview

NOT NULL Constraint

CREATE TABLE ...
( ...
  somecolumn varchar(30) NOT NULL, 
  ...
);

Also see:

NULL

Primary Key Constraint

Primary Key
CREATE TABLE ...
( ...
  CONSTRAINT pk_someconstraint PRIMARY KEY (somecolumn)
);

The primary key constraint can also be added after table creation with ALTER TABLE.

Foreign Key Constraint

Foreign Key
CREATE TABLE ...
( ...
  CONSTRAINT fk_someconstraint FOREIGN KEY (somecolumn)
);

The foreign key constraint can also be added after table creation with ALTER TABLE.

Check Constraint

CREATE TABLE ...
( ...
  eye_color CHAR(2) CHECK (eye_color IN ('BR', 'BL', 'GR')),
  ...
);