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

后端 未结 14 1978
庸人自扰
庸人自扰 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:09

    MySql 5 or higher behaves like this (I've just tested):

    • you can define unique constraints involving nullable columns. Say you define a constraint unique (A, B) where A is not nullable but B is
    • when evaluating such a constraint you can have (A, null) as many times you want (same A value!)
    • you can only have one (A, not null B) pair

    Example: PRODUCT_NAME, PRODUCT_VERSION 'glass', null 'glass', null 'wine', 1

    Now if you try to insert ('wine' 1) again it will report a constraint violation Hope this helps

提交回复
热议问题