从它自动创建索引的意义上说,它是一个索引.严格来说,Constraints不一定要涉及一个指数,this one does:
排除约束是使用索引来实现的,因此每个指定的运算符必须与索引访问方法index_method
的适当运算符类(参见第11.10节)相关联.
您的怀疑是正确的,因为unique
(因此也是primary key
)会自动创建索引并由索引强制执行,而创建唯一约束有时会换成只创建唯一索引,因为它们最终提供了相同的功能:
添加唯一约束将自动在约束中列出的列或列组上创建唯一B树索引.
请记住,所有这些都只是实现细节,无论多么不可能,它们都可能在future 发生变化
在psql
中,您可以运行\d+ workers_times
元命令来查看表上的所有约束and索引,无论它们是单独创建的还是作为约束的一部分--此排除约束以索引demo的形式列出
create table workers_times(
user_id uuid,
available tstzrange);
create extension btree_gist;
ALTER TABLE workers_times
ADD CONSTRAINT no_overlap_times
EXCLUDE USING gist (user_id WITH =, available WITH &&);
\d+ workers_times
Table "public.workers_times"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
-----------+-----------+-----------+----------+---------+----------+-------------+--------------+-------------
user_id | uuid | | | | plain | | |
available | tstzrange | | | | extended | | |
Indexes:
"no_overlap_times" EXCLUDE USING gist (user_id WITH =, available WITH &&)
Access method: heap
Here's a table显示了索引、约束、作为约束的索引和排除约束之间的区别: