简体   繁体   English

区分大小写的SQL Server 2008唯一列

[英]SQL Server 2008 Unique Column that is Case Sensitive

Is there a way to make a column both UNIQUE and Case Sensitive? 有没有办法让列既独特又区分大小写?

I want to be able to put 我希望能够投入

abcde and ABCDE abcdeABCDE

in a unique column. 在一个独特的列中。

The uniqueness can be enforced with a unique constraint. 可以使用唯一约束来强制执行唯一性。

Whether or not the unique index is case-sensitive is defined by the server's (or the table's) collation . 唯一索引是否区分大小写由服务器(或表的) 排序规则定义

You can get the current collation of your database with this query: 您可以使用此查询获取数据库的当前排序规则:

SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

and you should get something like: 你应该得到类似的东西:

SQLCollation
————————————
SQL_Latin1_General_CP1_CI_AS

Here, the "CI_AS" at the end of the collation means: CI = Case Insensitive, AS = Accent sensitive. 这里,整理结束时的“CI_AS”表示:CI = Case Insensitive,AS = Accent sensitive。

This can be changed to whatever you need it to be. 这可以改为你需要的任何东西。 If your database and/or table does have a case-sensitive collation, I would expect that the uniqueness of your index will be case-sensitive as well, eg your abcdef and ABCDEF should be both acceptable as unique strings. 如果您的数据库和/或表确实具有区分大小写的排序规则,我希望您的索引的唯一性也区分大小写,例如,您的abcdefABCDEF应该都是可接受的唯一字符串。

Marc

UPDATE: 更新:

I just tried this (SQL Server 2008 Developer Edition x64) - works for me (my database is generally using the "Latin1_General_CI_AS collation, but I can define a different one per table / per VARCHAR column even): 我刚试过这个(SQL Server 2008 Developer Edition x64) - 适用于我(我的数据库通常使用“Latin1_General_CI_AS排序规则,但我可以为每个表/每个VARCHAR列定义一个不同的一个):

CREATE TABLE TestUnique
    (string VARCHAR(50) COLLATE SQL_Latin1_General_Cp1_CS_AS)

CREATE UNIQUE INDEX UIX_Test ON dbo.TestUnique(string)

INSERT INTO dbo.TestUnique(string) VALUES ('abc')
INSERT INTO dbo.TestUnique(string) VALUES ('ABC')

SELECT * FROM dbo.TestUnique

and I get back: 我回来了:

string
ABC
abc

and no error about the unique index being violated. 并且没有关于违反唯一索引的错误。

In case some one needs to do it on an existing table which already has a unique key/index defined on a varchar / nvarchar column, here is the script. 如果某个人需要在已经在varchar / nvarchar列上定义了unique key/index的现有表上执行此操作,则此处是脚本。

ALTER TABLE [YourTable] DROP CONSTRAINT [UIX_YourUniqueIndex]
GO

ALTER TABLE [YourTable] ALTER COLUMN [YourColumn] [nvarchar](50) COLLATE Latin1_General_CS_AS NOT NULL;
GO

ALTER TABLE [YourTable] ADD  CONSTRAINT [UIX_YourUniqueIndex] UNIQUE NONCLUSTERED 
(
    [YourColumn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

I needed to import data from a case sensitive database. 我需要从区分大小写的数据库导入数据。 When I tried to put the primary key on the column that is the primary key on the source I couldn't do it because of duplicate keys. 当我尝试将主键放在作为源上主键的列上时,由于重复键,我无法执行此操作。 I changed the collation for the column (varchar) to case sensitive (Right click on the table, choose Design, highlight the column you want to change and click on the elipsis in Collation) and now it works fine. 我将列(varchar)的排序规则更改为区分大小写(右键单击表,选择“设计”,突出显示要更改的列,然后单击“排序”中的省略号),现在它可以正常工作。 (SQL Server 2008 R2 64 bit). (SQL Server 2008 R2 64位)。

Thanks @Devraj Gadhavi for the step by step as this is exactly what I needed to do as well. 感谢@Devraj Gadhavi一步一步,因为这正是我需要做的。 I was about to make those scripts but then (using SSMS 2008R2), I achieved the same in a more lazy :-) way. 我即将制作这些脚本但是(使用SSMS 2008R2),我以更加懒惰的方式实现了同样的方式:-)。 In the tree view I located my table and column and then right clicked on the column I wanted to change the collation on and chose 'Modify'. 在树形视图中,我找到了我的表和列,然后右键单击了我想要更改排序规则的列并选择了“修改”。 In the displayed window, I changed the collation in the properties to the case sensitive one, then anywhere in the open space at the top section of the window (where the columns are listed in table form) I right clicked and chose "Generate Change Script..." 在显示的窗口中,我将属性中的排序规则更改为区分大小写,然后在窗口顶部的开放空间中的任何位置(列表以表格形式列出)中我右键单击并选择“生成更改脚本” ......”

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

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