CONSTRAINT

Complete constraints reference: PRIMARY KEY, FOREIGN KEY, UNIQUE, and CHECK syntax in CREATE/ALTER TABLE, ON DELETE/UPDATE actions, TABLE_CONSTRAINTS table.

MariaDB supports constraints at table level, using CREATE TABLE or ALTER TABLE statements.

Syntax

Description

Constraints provide restrictions on the data you can add to a table. This allows to enforce data integrity in MariaDB, rather than through application logic. When a statement violates a constraint, MariaDB throws an error.

There are four types of table constraints:

  • PRIMARY KEY – Sets the column for referencing rows. Values must be UNIQUE and NOT NULL.

    • This constraint is documented here.

  • FOREIGN KEY – Sets the column to reference the primary key on another table.

    • This constraint is documented here and, in more detail, here and here.

  • UNIQUE – Requires values in column or columns only occur once in the table.

    • This constraint is documented here.

  • CHECK – Checks whether the data meets the given condition.

    • This constraint is documented here and, in more detail, here.

The Information Schema TABLE_CONSTRAINTS Table contains information about tables that have constraints:

FOREIGN KEY Constraints

InnoDB supports foreign key constraints. The syntax for a foreign key constraint definition in InnoDB looks like this:

circle-info

The symbol clause is optional. If you omit it, MariaDB automatically sets one.

The Information Schema REFERENTIAL_CONSTRAINTS table has more information about foreign keys:

circle-info

From MariaDB 12.1, multiple tables in the same database are allowed to use the same constraint name.

Consider this example, which creates two tables, then adds constraints to them:

Previously, the last statement failed because of duplicate constraint names:

Now, the statement works, because internally, names are assigned that aren't duplicates. (User-visible names can be duplicates, but internally they're prefixed with the schema and table names.)

CHECK Constraints

Constraints are enforced. You can define constraints in 2 different ways:

  • CHECK(expression) given as part of a column definition.

  • CONSTRAINT [constraint_name] CHECK (expression)

Before a row is inserted or updated, all constraints are evaluated in the order they are defined. If any constraint expression returns false, then the row is not inserted or updated. You can use most deterministic functions in a constraint, including UDFs.

If you use the CONSTRAINT [constraint_name] CHECK (expression) format and don't give a name to the constraint, the constraint gets an automatically generated name. This is done so that you can later delete it with ALTER TABLE DROP constraint.

You can disable all constraint expression checks by setting the check_constraint_checks variable to OFF. This is useful for example when loading a table that violates some constraints that you want to later find and fix in SQL.

Replication

In row-based replication, only the master checks constraints, and failed statements are not replicated. In statement-based replication, the slaves also check constraints. Constraints should therefore be identical, as well as deterministic, in a replication environment.

Auto_increment

auto_increment columns are not permitted in check constraints.

Examples

Foreign Key Constraint

Numeric Constraints and Comparisons

Adding Constraints

Dropping Contraints

Date Comparisons and Character Length

Misplaced Parenthesis

In the last two examples, compare the definition of table t2 to that of table t3. CHAR_LENGTH(name)>2 is different from CHAR_LENGTH(name>2), as the latter mistakenly performs a numeric comparison on the name field, leading to unexpected results.

See Also

This page is licensed: GPLv2, originally from fill_help_tables.sqlarrow-up-right

spinner

Last updated

Was this helpful?