Content deleted Content added
m →Common restrictions: {{code}} |
Asilvering (talk | contribs) m Removing link(s) Wikipedia:Articles for deletion/Where (SQL) closed as delete (XFDcloser) |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 1:
{{Short description|Type of integrity constraint in SQL}}
A '''check constraint''' is a type of [[integrity constraint]] in [[SQL]] which specifies a requirement that must be met by each [[row (database)|row]] in a database [[table (database)|table]]. The constraint must be a [[Predicate (mathematical logic)|predicate]]. It can refer to a single column, or multiple [[Column (database)|columns]] of the table. The result of the predicate can be either <code>TRUE</code>, <code>FALSE</code>, or <code>UNKNOWN</code>, depending on the presence of [[Null (SQL)|NULLs]]. If the predicate evaluates to <code>UNKNOWN</code>, then the constraint is not violated and the row can be inserted or updated in the table. This is contrary to predicates in
For example, in a table containing products, one could add a check constraint such that the price of a product and quantity of a product is a non-negative value:
If these constraints were not in place, it would be possible to have a negative price (
Check constraints are used to ensure the [[Data validation|validity of data]] in a database and to provide [[data integrity]]. If they are used at the database level, applications that use the database will not be able to add invalid data or modify valid data so the data becomes invalid, even if the application itself accepts invalid data.
Line 15 ⟶ 16:
Each check constraint has to be defined in the <code>CREATE TABLE</code> or <code>ALTER TABLE</code> statement using the syntax:
...,
CONSTRAINT ''constraint_name'' CHECK ( ''predicate'' ),
...
If the check constraint refers to a single column only, it is possible to specify the constraint as part of the column definition.
...
''column_name'' ''type'' CHECK ( ''predicate'' ),
...
==NOT NULL constraint==
A <code>NOT [[Null (SQL)|NULL]]</code> constraint is functionally equivalent to the following check constraint with an <code>IS NOT NULL</code> predicate:
Some [[relational database management system]]s are able to optimize performance when the <code>NOT NULL</code> constraint syntax is used as opposed to the <code>CHECK</code> constraint syntax given above.<ref>PostgreSQL
== Common restrictions ==
|