LoginSignup
0

More than 5 years have passed since last update.

DBレベルでパターンに一致するもののみ保存する方法について

Last updated at Posted at 2017-05-07

PostgreSQL9.6でしか確かめていないのと、実践的に使えるのか分かりませんが、次のようにすればCHECK制約でパターンマッチするものだけ登録することができます。コメントアウトしている方のCHECK制約でも多分、同じ結果になると思います。

DROP TABLE IF EXISTS AccessLog;
CREATE TABLE AccessLog (
  url VARCHAR(200) NOT NULL
  -- CHECK(SUBSTRING(url FROM 'https?://([^/]*)') IS NOT NULL)
  CHECK(url ~ 'https?://[^/*]')
);
INSERT INTO AccessLog VALUES('http://wwww.example.com');
INSERT INTO AccessLog VALUES('https://foo.bar');
INSERT INTO AccessLog VALUES('Hello World'); -- エラー発生

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