CREATE TABLE [User](
UserId int identity(1,1),
Email varchar(max) unique not null,
UserName varchar(30) unique not null,
[Password] varchar(max)not null,
PRIMARY KEY(UserId)
);
When I run this script I get an error saying that Email and UserName are of invalid types for unique constraint. So my question is how can I create a unique column in table script?
You cannot set varchar(max) or nvarchar(max) as unique values.. Please consider setting the length of these columns to a lower value.
I think the max length you can use for unique for nvarchars is 450
Passwords and e-mails shouldn't be very long anyway, perhaps 100 length should be more than enough.
Anyway, just dont use max if you want it to be a unique column.
Personally, I think you don't have to enforce passwords to be unique. A user might know if the password is being used by another account if his password is not accepted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.