Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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 一列(布尔值)定义另一列(布尔值),但反之亦然_Sql_Postgresql_Constraints - Fatal编程技术网

Sql 一列(布尔值)定义另一列(布尔值),但反之亦然

Sql 一列(布尔值)定义另一列(布尔值),但反之亦然,sql,postgresql,constraints,Sql,Postgresql,Constraints,让我们假设生命不仅存在于地球上,而且宇宙中还有其他生物。我有一张桌子,上面有全世界各种各样的物种 +----+---------------+-------+-------+ | id | name | green | forgn | +----+---------------+-------+-------| | 1 | belgian horse | false | false | | 2 | polar bear | false | false | | 3 |

让我们假设生命不仅存在于地球上,而且宇宙中还有其他生物。我有一张桌子,上面有全世界各种各样的物种

+----+---------------+-------+-------+
| id | name          | green | forgn |
+----+---------------+-------+-------|
|  1 | belgian horse | false | false |
|  2 | polar bear    | false | false |
|  3 | andromeda dog | true  | true  |
|  4 | cosmos cat    | true  | true  |
|  5 | amazon parrot | true  | false |
...
我们还知道,每一个外星生物(在本例中是仙女座的狗和宇宙猫)都是绿色的。所以我想定义一个约束,如果
forgn
为真,
green
也必须为真,但它不适用于其他情况,因此,例如
亚马逊鹦鹉
是一种绿色动物,但它并不生活在地球之外


我想我应该使用某种方式来完成它,但我不太确定如何完成。

您可以在表上添加一个Insert/update触发器。
每次更改列的值时,请更新触发器内的绿色列。

您可以在表中添加插入/更新触发器。
每次更改列的值时,更新触发器内的绿色列。

如果使用布尔类型,则为true>false
在这种情况下,您可以使用

CREATE TABLE tablename (
..........
CHECK (green >= forgn)
)

如果使用布尔类型,则为true>false
在这种情况下,您可以使用

CREATE TABLE tablename (
..........
CHECK (green >= forgn)
)

我在桌上写了一张稍有不同的支票

test=# \d test_stackoverflow;
Table "public.test_stackoverflow"
 Column |  Type   | Modifiers
--------+---------+-----------
 animal | text    |
 p1     | boolean | not null
 p2     | boolean | not null
Check constraints:
    "test_stackoverflow_check" CHECK (p2 AND p1 = p2 OR p2 = false)

test=#
insert into public.test_stackoverflow values ('cat', true, true );
INSERT 0 1
test=#
insert into public.test_stackoverflow values ('dog', true, false );
INSERT 0 1
test=#
insert into public.test_stackoverflow values ('parrot', false, true );
ERROR:  new row for relation "test_stackoverflow" violates check constraint "test_stackoverflow_check"
DETAIL:  Failing row contains (parrot, f, t).
test=# 

我在桌上写了一张稍有不同的支票

test=# \d test_stackoverflow;
Table "public.test_stackoverflow"
 Column |  Type   | Modifiers
--------+---------+-----------
 animal | text    |
 p1     | boolean | not null
 p2     | boolean | not null
Check constraints:
    "test_stackoverflow_check" CHECK (p2 AND p1 = p2 OR p2 = false)

test=#
insert into public.test_stackoverflow values ('cat', true, true );
INSERT 0 1
test=#
insert into public.test_stackoverflow values ('dog', true, false );
INSERT 0 1
test=#
insert into public.test_stackoverflow values ('parrot', false, true );
ERROR:  new row for relation "test_stackoverflow" violates check constraint "test_stackoverflow_check"
DETAIL:  Failing row contains (parrot, f, t).
test=# 

是否要对表中的数据编写查询或强制执行某些规则?@SuperMario后者。我在考虑一个altertable语句。如果有的话。是否要对表中的数据编写查询或强制执行某些规则?@SuperMario后者。我在考虑一个altertable语句。如果有一个,一个非常聪明的解决方案,我没有想到。我已经测试过了,很有效,谢谢!一个非常聪明的解决方案,我没有想到。我已经测试过了,很有效,谢谢!