CONSTRAINT

SQL CONSTRAINT Keyword

The CONSTRAINT keyword is used to define rules that restrict the type of data that can be inserted into a table. Constraints help ensure accuracy consistency and integrity of the data in a database.

SQL ADD CONSTRAINT Syntax

The ADD CONSTRAINT command is used to add constraints after a table has already been created. It can be used to add PRIMARY KEY UNIQUE CHECK and FOREIGN KEY constraints.

Tutorials dojo strip
SQL

SQL ADD CONSTRAINT Example

MySQL / SQL Server / Oracle / MS Access:

This adds a primary key constraint named PK_Vehicles on the columns VehicleID and Brand.

SQL

Note: SQLite does not support adding constraints after table creation. Constraints must be defined when the table is first created.

SQL DROP CONSTRAINT

The DROP CONSTRAINT command is used to remove existing constraints such as UNIQUE PRIMARY KEY FOREIGN KEY and CHECK.

DROP a UNIQUE Constraint

SQL Server / Oracle / MS Access:

SQL

MySQL:

SQL

DROP a PRIMARY KEY Constraint

SQL Server / Oracle / MS Access:

SQL

MySQL:

SQL

DROP a FOREIGN KEY Constraint

SQL Server / Oracle / MS Access:

SQL

MySQL:

SQL

DROP a CHECK Constraint

SQL Server / Oracle / MS Access:

SQL

MySQL:

SQL

Note: SQLite does not support dropping constraints using ALTER TABLE. To remove a constraint in SQLite the table must be recreated without the constraint.

SQL CONSTRAINTS Labs

Tutorials dojo strip