سؤال

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