LoginSignup
0
0

More than 5 years have passed since last update.

mysqlのカラムごとに重複を許可しない設定

Last updated at Posted at 2017-04-16

mysqlでテーブルを作った後にカラムをユニークにしたくなったので、その時にやったこと。

カラムにuniqueを設定する方法

alter table t_name add unique (col_name);

これだけでいけました。

この後にinsertしようして重複があると、下のようなエラーが出ました。

`_query': Duplicate entry '' for key 'col_name' (Mysql2::Error)

エラーをさせない方法

insert ignore into col_name (col_name, ..) value ("value", ...)

普通のinsert文にignoreを追加するだけでした。

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