唯一与检查约束
Unique vs Check constraint
我向 PostgreSQL 添加了一个 Unique 约束和一个 Check 约束 table。
现在,当我检查 table 的详细信息时,唯一约束列在 'Indexes' 下,而检查约束则没有。
为什么会出现这种行为?
在此处查看文档:https://www.postgresql.org/docs/12/ddl-constraints.html#DDL-CONSTRAINTS-UNIQUE-CONSTRAINTS
Adding a unique constraint will automatically create a unique B-tree index on the column or group of columns listed in the constraint. A uniqueness restriction covering only some rows cannot be written as a unique constraint, but it is possible to enforce such a restriction by creating a unique partial index.
您在 Indexes 下看到 UNIQUE 是因为创建了一个唯一的 B 树索引。
索引是为 UNIQUE 约束创建的,因为它是检查一个值对于大型 tables 是否唯一的高效方法:您必须检查 table 中的所有行才能确保该值是唯一的。没有为 CHECK 约束创建索引,因为没有性能问题:您只需检查当前行的列值 - 您不需要检查 table 中的所有行。
我向 PostgreSQL 添加了一个 Unique 约束和一个 Check 约束 table。
现在,当我检查 table 的详细信息时,唯一约束列在 'Indexes' 下,而检查约束则没有。
为什么会出现这种行为?
在此处查看文档:https://www.postgresql.org/docs/12/ddl-constraints.html#DDL-CONSTRAINTS-UNIQUE-CONSTRAINTS
Adding a unique constraint will automatically create a unique B-tree index on the column or group of columns listed in the constraint. A uniqueness restriction covering only some rows cannot be written as a unique constraint, but it is possible to enforce such a restriction by creating a unique partial index.
您在 Indexes 下看到 UNIQUE 是因为创建了一个唯一的 B 树索引。
索引是为 UNIQUE 约束创建的,因为它是检查一个值对于大型 tables 是否唯一的高效方法:您必须检查 table 中的所有行才能确保该值是唯一的。没有为 CHECK 约束创建索引,因为没有性能问题:您只需检查当前行的列值 - 您不需要检查 table 中的所有行。