An unnestarray is needed to match regular expression:
select bool_and (n ~ '^[a-zA-Z]*$')
from unnest(array['John','Mary']) a(n)
;
bool_and
t
bool_and. , :
create function check_text_array_regex (
a text[], regex text
) returns boolean as $$
select bool_and (n ~ regex)
from unnest(a) s(n);
$$ language sql immutable;
:
create table student (
id serial,
name text[] check (check_text_array_regex (name, '^[a-zA-Z]*$'))
);
:
insert into student (name) values (array['John', 'Mary']);
INSERT 0 1
insert into student (name) values (array['John', 'Mary2']);
ERROR: new row for relation "student" violates check constraint "student_name_check"
DETAIL: Failing row contains (2, {John,Mary2}).