Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Arrays 检查表中varchar[]和varchar[]列的约束_Arrays_Postgresql_Check Constraints_Postgresql 9.5_Check Constraint - Fatal编程技术网

Arrays 检查表中varchar[]和varchar[]列的约束

Arrays 检查表中varchar[]和varchar[]列的约束,arrays,postgresql,check-constraints,postgresql-9.5,check-constraint,Arrays,Postgresql,Check Constraints,Postgresql 9.5,Check Constraint,对于文字游戏,我试图向VARCHAR数组添加CHECK约束: CREATE TABLE words_games ( gid SERIAL PRIMARY KEY, created timestamptz NOT NULL, player1 integer REFERENCES words_users(uid) ON DELETE CASCADE NOT NULL, player2 integer REFERENCES words_

对于文字游戏,我试图向
VARCHAR
数组添加
CHECK
约束:

CREATE TABLE words_games (
        gid SERIAL PRIMARY KEY,
        created timestamptz NOT NULL,

        player1 integer REFERENCES words_users(uid) ON DELETE CASCADE NOT NULL,
        player2 integer REFERENCES words_users(uid) ON DELETE CASCADE,

        played1 timestamptz,
        played2 timestamptz,

        hand1 varchar[7] NOT NULL CHECK (ALL(hand1) ~ '^[*A-Z]$'),
        hand2 varchar[7] NOT NULL CHECK (ALL(hand2) ~ '^[*A-Z]$'),
        pile  varchar[116] NOT NULL CHECK (ALL(pile) ~ '^[*A-Z]$'),

        board varchar[15][15] NOT NULL CHECK (ALL(board) ~ '^[.A-Z]$'),
        style integer NOT NULL CHECK (1 <= style AND style <= 4)
);
可能是因为
ALL
关键字应该在右侧


有谁能推荐一种实现检查约束的方法吗?

是的,不可能使用
all()
any()
函数作为左比较参数。改为创建函数:

create or replace function check_array_regexp_all(text[], text) returns boolean as $$
  select bool_and(x) from (select unnest($1) ~ $2 as x) t;
$$ language sql immutable;
并将其用于约束:

...
hand1 varchar[7] NOT NULL CHECK (check_array_regexp_all(hand1, '^[*A-Z]$')),
...

这是一个很好的例子。唯一的解决方法是为
创建一个逆函数,如
/
~
/等(在您的情况下,它也需要是不可变的)。或者,您可以(它可以有自己的检查约束)&使用该域的数组。@pozs不幸的是,@Abelisto很想知道。谢谢你的头up@pozs我也有同样的想法,我检查过:)
...
hand1 varchar[7] NOT NULL CHECK (check_array_regexp_all(hand1, '^[*A-Z]$')),
...