postgresql - 为什么约束不在公共模式中不可见?
问题描述
我正在架构中创建一个具有唯一索引的表,如下所示:
create schema s1;
create table s1.test(key int);
create unique index test_index on s1.test(key);
现在,当我查询时information_schema.table_constraints
,未显示索引。这是为什么?但是,索引正常工作:
test=# insert into s1.test(key) values (1);
INSERT 0 1
test=# insert into s1.test(key) values (1);
ERROR: duplicate key value violates unique constraint "test_index"
DETAIL: Key (key)=(1) already exists.
test
我在这里使用的数据库归当前用户所有。
更新
看起来约束也没有显示在public
模式中:
create table test(key int);
create unique index test_index on test(key);
select * from information_schema.table_constraints;
解决方案
现在,当我查询 information_schema.table_constraints 时,没有显示索引
UNIQUE INDEX
!=CONSTRAINT
您需要添加CONSTRAINT
:
ALTER TABLE test ADD CONSTRAINT uq_test_key UNIQUE(key);
-- constraint info
SELECT *
FROM information_schema.table_constraints;
-- supportive index info
select
t.relname as table_name,
i.relname as index_name,
a.attname as column_name
from
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a
where
t.oid = ix.indrelid
and i.oid = ix.indexrelid
and a.attrelid = t.oid
and a.attnum = ANY(ix.indkey)
and t.relkind = 'r'
and t.relname = 'test';
DBFiddle Demo CONSTRAINT - 约束 + 索引
DBFiddle 演示仅索引 - 索引
推荐阅读
- apache - 如何在不使用 VM 的情况下在同一台机器上运行 Apache 服务器和 WAMP?
- ruby - 将 curl PUT 转换为 ruby net:http
- java - java方法在数组中搜索字符串
- javascript - [[ERROR]] 选择的安装文件夹不是 wampserver
- laravel - Laravel eloquent 模型使用自定义表和主键的主键值不正确
- android - 在Android中离线存储json数据?
- amazon-web-services - aws 配置:安全组 ID 和名称不匹配
- r - 使用 ggplot2 和 cowplot 组合绘图和重叠 geom_rect 对象
- java - 这段代码中 Integer.Min_value 的用途是什么?
- node.js - GSI 中使用 FilterExpression 的 DynamoDB 查询