How do I specify unique constraint for multiple columns in MySQL?

后端 未结 14 1994
庸人自扰
庸人自扰 2020-11-21 06:50

I have a table:

table votes (
    id,
    user,
    email,
    address,
    primary key(id),
);

Now I want to make the columns user

14条回答
  •  太阳男子
    2020-11-21 07:21

    If you want to avoid duplicates in future. Create another column say id2.

    UPDATE tablename SET id2 = id;
    

    Now add the unique on two columns:

    alter table tablename add unique index(columnname, id2);
    

提交回复
热议问题