如何在SQL Express 2008中限制nvarchar列的重复值 [英] How to restric the duplicate value on nvarchar column in SQL express 2008

查看:60
本文介绍了如何在SQL Express 2008中限制nvarchar列的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何限制sql Express 2008中nvarchar列的重复值



以下是我的表格



Hi,

How to restric the duplicate value on nvarchar column in sql Express 2008

Below is my table

CREATE TABLE [dbo].[User](
	[User_No] [int] IDENTITY(1,1) NOT NULL,
	[User_Name] [nvarchar](max) NOT NULL
) ON [PRIMARY]

GO





如果User_Name重复,我想限制。



< b>我尝试了什么:



创建UN IQUE INDEX ix_IndexName ON用户(User_Name)WITH IGNORE_DUP_KEY =< on>



但收到错误。

'<'附近的语法不正确



HTML编码[/ edit]



I want to restric if the User_Name is duplicate.

What I have tried:

CREATE UNIQUE INDEX ix_IndexName ON User(User_Name) WITH IGNORE_DUP_KEY = <on>

but got error.
Incorrect syntax near '<'

[edit]HTML encoded[/edit]

推荐答案

参考 SQL UNIQUE Con​​straint [ ^ ]



refer SQL UNIQUE Constraint[^]

ALTER TABLE [User]
ADD CONSTRAINT uc_User_Name UNIQUE (User_Name)


尝试删除<和>符号:

Try removing the < and > symbols:
CREATE UNIQUE INDEX ix_IndexName ON User(User_Name) WITH IGNORE_DUP_KEY = ON

或更好:

Or better:

CREATE UNIQUE INDEX ix_IndexName ON User(User_Name) WITH IGNORE_DUP_KEY = OFF

如果你想禁止重复。



但就个人而言,我这样做是我的用户界面代码的一部分:当用户尝试注册时,我检查是否采用了用户名。如果是,请告诉他们。如果不是,请告诉他们没事。

等到插入的最后一分钟对用户来说不是最好的方式。

If you want to disallow duplicates.

But personally, I'd do that as part of my user interface code: when a user tries to register I check to see if the username is taken. If it is, tell them. If it isn't, tell them it's fine.
Waiting until the last minute when it's inserted isn't the "nicest" way for a user.


这篇关于如何在SQL Express 2008中限制nvarchar列的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆