
- #Postgresql alter table add constraint unique how to
- #Postgresql alter table add constraint unique install
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. As a result, the system reports an error.A check constraint is the most generic constraint type. ALTER TABLE aa ENABLE CONSTRAINT aa_a1_check NOT VALID Įxecute the following statement to insert the values that do not meet the constraint. ALTER TABLE aa ENABLE CONSTRAINT aa_a1_check ĮRROR: check constraint "aa_a1_check" is violated by some rowĮxecute the following statement to enable the constraint but not to check whether the existing values meet the constraint. The system reports an error because a value in the table does not meet the constraint. INSERT INTO aa VALUES (10) Įxecute the following statement to enable the constraint and check whether the existing values meet the constraint. The value can be inserted because the constraint is disabled. ALTER TABLE aa ADD CONSTRAINT aa_a1_check CHECK(a1>10) DISABLE Įxecute the following statement to insert a value that does not meet the constraint. In this case, if the foreign key constraint is not disabled or deleted, the primary key constraint cannot be disabled.Įxecute the following statement to create a table: CREATE TABLE aa(a1 INT) Įxecute the following statement to create a constraint in which the values of the a1 column must be greater than 10. For example, a primary key constraint is referenced by a foreign key constraint of another table. Note A primary key constraint or a unique constraint may be referenced by other objects. The error includes the table data that does not meet the constraint. If the data does not meet the constraint, the system reports an error. By default, the system checks whether data in the table meets the constraint.


This indicates that this constraint does not apply to the data in the table and the data that is to be inserted to the table. The constraint_name constraint is in the disabled state.
#Postgresql alter table add constraint unique install
You can execute the following statement to install the plug-in: CREATE EXTENSION IF NOT EXISTS polar_constraint
#Postgresql alter table add constraint unique how to
For more information about how to upgrade the kernel version, see Upgrade the minor version.

