Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在Postgres中,如何约束两列的唯一性而忽略它们的顺序_Sql_Database_Postgresql_Constraints - Fatal编程技术网

Sql 在Postgres中,如何约束两列的唯一性而忽略它们的顺序

Sql 在Postgres中,如何约束两列的唯一性而忽略它们的顺序,sql,database,postgresql,constraints,Sql,Database,Postgresql,Constraints,例如,在我的Postgres数据库中,我使用两列作为主键 CREATE TABLE example ( a integer, b integer, c integer, PRIMARY KEY (a, c) ); 我想确保不能添加(c,a)。换句话说,如果(1,2)已在数据库中,则无法添加(2,1)。是否可以添加这样的约束?您可以添加基于返回的唯一索引,例如: create unique index so45 on example ((ARRAY[greates

例如,在我的Postgres数据库中,我使用两列作为主键

CREATE TABLE example (
    a integer,
    b integer,
    c integer,
    PRIMARY KEY (a, c)
);

我想确保不能添加
(c,a)
。换句话说,如果(1,2)已在数据库中,则无法添加(2,1)。是否可以添加这样的约束?

您可以添加基于返回的唯一索引,例如:

create unique index so45 on example ((ARRAY[greatest(a,c),least(a,c)]));
示例:

t=# insert into example select 1,2,3;
INSERT 0 1
t=# insert into example select 1,2,1;
INSERT 0 1
t=# insert into example select 3,2,1;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
t=# alter table example drop CONSTRAINT example_pkey ;
ALTER TABLE
t=# insert into example select 1,3,3;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
在这种情况下,不需要PK来限制唯一性:

t=# insert into example select 1,2,3;
INSERT 0 1
t=# insert into example select 1,2,1;
INSERT 0 1
t=# insert into example select 3,2,1;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
t=# alter table example drop CONSTRAINT example_pkey ;
ALTER TABLE
t=# insert into example select 1,3,3;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.

您可以添加基于返回的唯一索引,例如:

create unique index so45 on example ((ARRAY[greatest(a,c),least(a,c)]));
示例:

t=# insert into example select 1,2,3;
INSERT 0 1
t=# insert into example select 1,2,1;
INSERT 0 1
t=# insert into example select 3,2,1;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
t=# alter table example drop CONSTRAINT example_pkey ;
ALTER TABLE
t=# insert into example select 1,3,3;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
在这种情况下,不需要PK来限制唯一性:

t=# insert into example select 1,2,3;
INSERT 0 1
t=# insert into example select 1,2,1;
INSERT 0 1
t=# insert into example select 3,2,1;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
t=# alter table example drop CONSTRAINT example_pkey ;
ALTER TABLE
t=# insert into example select 1,3,3;
ERROR:  duplicate key value violates unique constraint "so45"
DETAIL:  Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
该模块允许将此解决方案推广到任意长度的整数集,
create extension intarray;在示例(排序(数组[a,c])上创建唯一索引集
该模块允许将此解决方案推广到任意长度的整数集,
在阵列中创建扩展;在示例(排序(数组[a,c])上创建唯一索引集