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.
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
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.
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).
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..."
5条答案
按热度按时间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:
and you should get something like:
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):
and I get back:
and no error about the unique index being violated.
2nc8po8w2#
In case some one needs to do it on an existing table which already has a
unique key/index
defined on avarchar
/nvarchar
column, here is the script.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.
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).
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..."