排除限制在postgresql中没有生效?

时间:2015-08-13 12:48:26

标签: postgresql

以下是代码:

testdb=# CREATE TABLE COMPANY7(
testdb(#    ID INT PRIMARY KEY     NOT NULL,
testdb(#    NAME           TEXT ,
testdb(#    AGE            INT   ,
testdb(#    ADDRESS        CHAR(50),
testdb(#    SALARY         REAL,
testdb(#    EXCLUDE USING gist
testdb(#    (NAME WITH =,
testdb(#    AGE WITH <>)
testdb(# );
CREATE TABLE
testdb=#  INSERT INTO COMPANY7 VALUES(1, 'Paul', 32, 'California', 20000.00 );
INSERT 0 1
testdb=#  INSERT INTO COMPANY7 VALUES(2, 'Paul', 32, 'Texas', 20000.00 );
INSERT 0 1
testdb=#  INSERT INTO COMPANY7 VALUES(3, 'Allen', 42, 'California', 20000.00 );
INSERT 0 1
testdb=# table company7
testdb-# ;
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | California                                         |  20000
  2 | Paul  |  32 | Texas                                              |  20000
  3 | Allen |  42 | California                                         |  20000
(3 rows)

显然两个约束都被违反,但没有错误,为什么?

PostgreSQL版本9.4.4

在Ubuntu 15.04和OS X 10.10上测试

1 个答案:

答案 0 :(得分:3)

   EXCLUDE USING gist
   (NAME WITH =,
   AGE WITH <>)

这意味着不允许使用相同名称和不同年龄。

我试过这样显示错误

   INSERT INTO appsetup.COMPANY7 VALUES(4, 'Paul', 42, 'California', 20000.00 );

错误

ERROR:  conflicting key value violates exclusion constraint "company7_name_age_excl"
DETAIL:  Key (name, age)=(Paul, 42) conflicts with existing key (name, age)=(Paul, 32).