Types of constraints

A constraint is a rule that is used for optimization purposes.

Constraints can be categorized into five types:

  • A NOT NULL constraint is a rule that prevents null values from being entered into one or more columns within a table.

  • A unique constraint (also referred to as a unique key constraint) is a rule that forbids duplicate values in one or more columns within a table. Unique and primary keys are the supported unique constraints. For example, a unique constraint can be defined on the supplier identifier in the supplier table to ensure that the same supplier identifier is not given to two suppliers.

  • A primary key constraint is a column or combination of columns that has the same properties as a unique constraint. You can use a primary key and foreign key constraints to define relationships between tables.

  • A foreign key constraint (also referred to as a referential constraint or a referential integrity constraint) is a logical rule about values in one or more columns in one or more tables. For example, a set of tables shares information about a corporation's suppliers. Occasionally, a supplier's name changes. You can define a referential constraint that states the ID of the supplier in a table must match a supplier ID in the supplier information. This constraint prevents insert, update, or delete operations that would otherwise result in missing supplier information.

  • A table check constraint (also called a check constraint) sets restrictions on data that is added to a specific table. For example, you can use a table check constraint whenever salary data is added or updated in a table that contains personnel information. For such operations, the table check constraint can ensure that the salary level for an employee is at least $20 000.

An informational constraint is an attribute of a certain type of constraint, but the attribute is not enforced by the database manager.