词条 | Check constraint |
释义 |
A check constraint is a type of integrity constraint in SQL which specifies a requirement that must be met by each row in a database table. The constraint must be a predicate. It can refer to a single column, or multiple columns of the table. The result of the predicate can be either 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 (−$30) or quantity (−3 items). Check constraints are used to ensure the 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. DefinitionEach check constraint has to be defined in the 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 constraintA Some relational database management systems are able to optimize performance when the Common restrictionsMost 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
User-defined 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. References1. ^PostgreSQL 8.3devel Documentation, Chapter 5. Data Definition, Section 5.3.2. Not-Null Constraints, Website: http://developer.postgresql.org/pgdocs/postgres/ddl-constraints.html, Accessed on May 5, 2007 1 : SQL |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。