I was looking at ways to make a newly added column a Unique but not primary column. I had a look at this W3School link . But Instead of following their approach I simply changed my table in the Visual Studio designer as.
CREATE TABLE [dbo].[Userpro] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NULL,
[Postcode] NVARCHAR (4) NULL,
[Gender] INT NULL,
[Blog] NVARCHAR (MAX) NULL,
[FeedBack] NVARCHAR (MAX) NULL,
[Username] NVARCHAR (50) NOT NULL Unique,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Notice that I simply added "Unique"[Username] NVARCHAR (50) NOT NULL Unique
. I am unsure if this has the same effect or should I go back and just use the script in the link.
2条答案
按热度按时间ffscu2ro1#
That is perfect.
Adding
UNIQUE
will have the effect you describe. It is also explained in the link you provide.wlwcrazw2#
If you want to specify the name for the
unique
constraint, then instead of adding theunique
keyword to your column definition, you can append the full constraint definition to theCREATE
statement.The design view will automatically update and you will see
Check_unique_username
listed in the "Keys" section. You can also add it manually to the keys section by right-clicking and choosing "add new" > "unique key" - then use the properties panel to specify the name and column.