I have a table

table userinfo (
     user,
     email,
     address,
     primary key(user)
);

In this table 'user' field is same as 'email' at the time of insertion. But user can change their 'email' latter on. So i want email to be unique as well and insert query to fail if email already exists in the table.

Is it Possible?

Thanks in advance and this is just an example.

有帮助吗?

解决方案

yes, add a UNIQUE constraint

table userinfo (
     user,
     email,
     address,
     primary key(user),
     CONSTRAINT tb_uq UNIQUE (email)
);

the table will now have unique USER and unique Email.

其他提示

You can create multiple indexes on a table. So if you create 2 unique indexs just on the columns you want, it will work how you want. If you create 1 index on multiple columns it is the combination of those columns that needs to be unique.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top