简体   繁体   English

unicode字符上的SQL Server唯一约束问题

[英]SQL Server unique constraint issue on unicode characters

I have my table definition as follows: 我的表定义如下:

create table [Language](
Id int primary key identity,
Code varchar(11) not null unique,
NativeName nvarchar(50) not null unique
)

And then, I have a long list of statements that insert into that table. 然后,我有一长串插入该表的语句。 The problem is that some of the insert statements conflict on my NativeName column's unique constraint. 问题是一些插入语句与我的NativeName列的唯一约束冲突。 The weird thing is that the content is not unique at all. 奇怪的是,内容并不是唯一的。 For example, if I only insert the following with the table empty: 例如,如果我只插入以下表空:

insert into Language (Code, NativeName) values('am', N'አማርኛ');
insert into Language (Code, NativeName) values('dv', N'ދިވެހިބަސް‏');

I get for the second insert. 我得到第二次插入。

Violation of UNIQUE KEY constraint 'UQ__Language__EB1957A5F98D1F9C'. Cannot insert duplicate key in object 'dbo.Language'. The duplicate key value is (ދިވެހިބަސް‏).

Does anyone know why unicode characters are causing these issues? 有谁知道为什么unicode字符会导致这些问题?

Try declaring the NativeName column with a more specific (binary) collation. 尝试使用更具体(二进制)的排序规则声明NativeName列。

eg: 例如:

 NativeName nvarchar(50) collate SQL_Latin1_General_CP437_BIN not null unique 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM