1
1

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.

一意制約を動的に利用したい

Posted at
id id_type
id_0001 main
id_0002 sub
id_0001 main 新規登録 NG
id_0002 sub 新規登録 OK

例えばid_typeが"main"の時だけIDを一意にしたい、id_typeが"sub"の時はidの重複を許可する
というようなことをしたいときは以下のように制約を張れば良い

Oracle:ファンクション索引

CREATE UNIQUE INDEX インデックス名 ON テーブル名 (CASE WHEN id_type='main' THEN id ELSE NULL END);

PostgreSQL:部分インデックス

CREATE UNIQUE INDEX インデックス名 ON テーブル名 USING btree (id) WHERE id_type='main';

MySQL:Generated Column

使い方、未調査。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?