Content deleted Content added
Quaker5567 (talk | contribs) No edit summary |
Asilvering (talk | contribs) m Removing link(s) Wikipedia:Articles for deletion/Where (SQL) closed as delete (XFDcloser) |
||
(48 intermediate revisions by 41 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 <code>WHERE</code> clauses in <code>[[Select (SQL)|SELECT]]</code> or <code>[[Update (SQL)|UPDATE]]</code> statements.
For example, in a
If these constraints
Check constraints are used to ensure the [[Data validation|validity of data]] in a database
==Definition==
Each check constraint has to be defined in the <code>CREATE TABLE</code> or <code>ALTER TABLE</code> statement using the syntax:
CREATE TABLE ''table_name'' (
...,
CONSTRAINT ''constraint_name'' CHECK ( ''predicate'' ),
...
)
ALTER TABLE ''table_name''
ADD 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.
CREATE TABLE ''table_name'' (
...
''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:
CHECK (''column'' IS NOT NULL)
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 13 Documentation, Chapter 5. ''Data Definition'', Section 5.4.2. ''Not-Null Constraints'', Website: https://www.postgresql.org/docs/13/ddl-constraints.html, Accessed on Jan 9, 2021</ref>
== Common restrictions ==
Most database management systems restrict check constraints to a single row, with access to constants and deterministic functions, but not to data in other tables, or to data invisible to the current transaction because of [[transaction isolation]].
Such constraints are not truly ''table check constraints'' but rather ''row check constraints''. Because these constraints are generally only verified when a row is directly updated (for performance reasons,) and often implemented as implied <code>INSERT</code> or <code>UPDATE</code> triggers, [[integrity constraints]] could be violated by indirect action were it not for these limitations. Furthermore, otherwise-valid modifications to these records would then be prevented by the <code>CHECK</code> constraint. Some examples of dangerous constraints include:
* {{code|2=sql|1=CHECK ((select count(*) from invoices where invoices.customerId = customerId) < 1000)}}
* {{code|2=sql|1=CHECK (dateInserted = CURRENT_DATE)}}
* {{code|2=sql|1=CHECK (countItems = RAND())}}
User-defined [[Database trigger|triggers]] can be used to work around these restrictions. Although similar in implementation, it is semantically clear that triggers will only be fired when the table is directly modified, and that it is the designer's responsibility to handle indirect, important changes in other tables; constraints on the other hand are intended to be "true at all times" regardless of the user's actions or the designer's lack of foresight.
== References ==
<references/>
[[Category:SQL]]
|