0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

postgresqlのtextカラムで無効な文字列を弾く

Posted at

textの場合 

postgres=# CREATE TABLE test (
    sample text CONSTRAINT check_invalid_char CHECK (sample !~ 'a')
);
postgres=# INSERT INTO test VALUES ('hoge');
postgres=# INSERT INTO test VALUES ('hage');
ERROR:  new row for relation "test" violates check constraint "check_invalid_char"
DETAIL:  Failing row contains (hage).

text配列の場合

postgres=# CREATE FUNCTION check_text_value (
     vals text[]
) RETURNS boolean AS $$
     SELECT bool_and (t !~ 'a') FROM unnest(vals) AS t
$$ LANGUAGE sql IMMUTABLE
postgresql=# CREATE TABLE test (
    text[] vals CHECK (check_text_value (vals))
);

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?