![]() ![]() PostgreSQL does not support CHECK constraints that reference table data other than the new or updated row being checked. To ensure that a column does not contain null values, the not-null constraint described in the next section can be used. Since most expressions will evaluate to the null value if any operand is null, they will not prevent null values in the constrained columns. It should be noted that a check constraint is satisfied if the check expression evaluates to true or the null value. Names can be assigned to table constraints in the same way as column constraints:ĬONSTRAINT valid_discount CHECK (price > discounted_price) ( PostgreSQL doesn't enforce that rule, but you should follow it if you want your table definitions to work with other database systems.) The above example could also be written as:ĬHECK (discounted_price > 0 AND price > discounted_price) ![]() Column constraints can also be written as table constraints, while the reverse is not necessarily possible, since a column constraint is supposed to refer to only the column it is attached to. We say that the first two constraints are column constraints, whereas the third one is a table constraint because it is written separately from any one column definition. Column definitions and these constraint definitions can be listed in mixed order. It is not attached to a particular column, instead it appears as a separate item in the comma-separated column list. The first two constraints should look familiar. Say you store a regular price and a discounted price, and you want to ensure that the discounted price is lower than the regular price:ĭiscounted_price numeric CHECK (discounted_price > 0), (If you don't specify a constraint name in this way, the system chooses a name for you.)Ī check constraint can also refer to several columns. So, to specify a named constraint, use the key word CONSTRAINT followed by an identifier followed by the constraint definition. Price numeric CONSTRAINT positive_price CHECK (price > 0) This clarifies error messages and allows you to refer to the constraint when you need to change it. You can also give the constraint a separate name. The check constraint expression should involve the column thus constrained, otherwise the constraint would not make too much sense. A check constraint consists of the key word CHECK followed by an expression in parentheses. Default values and constraints can be listed in any order. For instance, to require positive product prices, you could use:Īs you see, the constraint definition comes after the data type, just like default value definitions. It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression. This is the default.A check constraint is the most generic constraint type. Refuse to drop the index if any objects depend on it. CASCADEĪutomatically drop objects that depend on the index, and in turn all objects that depend on those objects (see Section 5.14). The name (optionally schema-qualified) of an index to remove. IF EXISTSĭo not throw an error if the index does not exist. Lastly, indexes on partitioned tables cannot be dropped using this option.įor temporary tables, DROP INDEX is always non-concurrent, as no other session can access them, and non-concurrent index drop is cheaper. (Thus, an index that supports a UNIQUE or PRIMARY KEY constraint cannot be dropped this way.) Also, regular DROP INDEX commands can be performed within a transaction block, but DROP INDEX CONCURRENTLY cannot. Only one index name can be specified, and the CASCADE option is not supported. There are several caveats to be aware of when using this option. With this option, the command instead waits until conflicting transactions have completed. A normal DROP INDEX acquires an ACCESS EXCLUSIVE lock on the table, blocking other accesses until the index drop can be completed. Drop the index without locking out concurrent selects, inserts, updates, and deletes on the index's table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |