SQL Server 2008 Unique Column that is Case Sensitive

iq3niunx  于 11个月前  发布在  SQL Server
关注(0)|答案(5)|浏览(133)

Is there a way to make a column both UNIQUE and Case Sensitive?

I want to be able to put

abcde and ABCDE

in a unique column.

y3bcpkx1

y3bcpkx11#

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.

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, e.g. your abcdef and ABCDEF should be both acceptable as unique strings.

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):

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.

2nc8po8w

2nc8po8w2#

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.

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
f3temu5u

f3temu5u3#

If you right click on your table in the tree-view and select design for your table, and then click on the column name, there is a panel that appears at the bottom called 'column properties' where you can set the collation options using the UI.

rjzwgtxy

rjzwgtxy4#

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. (SQL Server 2008 R2 64 bit).

ctehm74n

ctehm74n5#

Thanks @Devraj Gadhavi for the step by step as this is exactly what I needed to do as well. I was about to make those scripts but then (using SSMS 2008R2), I achieved the same in a more lazy :-) way. 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..."

相关问题