Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 834
AxiomaticNexus Avatar asked Oct 21 '22 23:10

AxiomaticNexus


1 Answers

Try declaring the NativeName column with a more specific (binary) collation.

eg:

 NativeName nvarchar(50) collate SQL_Latin1_General_CP437_BIN not null unique 
like image 79
podiluska Avatar answered Nov 04 '22 01:11

podiluska