February 17, 2014 at 6:33 am
I want to apply unique constraints on existing database,but column accepts null values how can i set unique constraints on that column without lose of data. helps me out
February 17, 2014 at 6:43 am
I assume that you meant that the table should have many records with null value in this column, but the rest of the values should be unique. If I'm correct then you can try and use filtered unique index on that table (the filter would be of course on the null value).
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
February 17, 2014 at 6:48 am
Yes i need this thanks for your help
February 17, 2014 at 6:51 am
How can i do it with sql server 2005
February 17, 2014 at 6:55 am
With a hell of a lot of difficulty (and a computed column). The filtered index is 2008 and above, which since this was posted in the SQL 2008 forum we assumed you were using.
For 2005, define a computed column on ISNULL(<column that you want to be unique>,<the primary key column of the table>) and put a unique index on that computed column.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 17, 2014 at 7:03 am
Can i apply this approach for column which is varchar type?
February 17, 2014 at 7:13 am
Yes.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 17, 2014 at 11:23 am
GilaMonster (2/17/2014)
Yes.
It's easier with varchar, nvarchar, and varbinary since it's generally not too difficult to convert the primary key to one of those types; other column types could be an issue.
And there's another complication - you need some way of ensuring that the value you convery the primary key into when the column in question has a null never clashes with some other value in that column, which may turn out to be nontrivial.
Tom
February 17, 2014 at 9:51 pm
I am creating CLUSTERED INDEX by running below query on sql server 2005
CREATE TABLE dbo.Example
(
col1 varchar(Max) NULL,
);
GO
CREATE VIEW dbo.ExampleUnique
WITH SCHEMABINDING AS
SELECT e.col1
FROM dbo.Example AS e
WHERE e.col1 IS NOT NULL;
GO
CREATE UNIQUE CLUSTERED INDEX cuq
ON dbo.ExampleUnique (col1);
GO
It shows error: Column 'col1' in table 'dbo.ExampleUnique' is of a type that is invalid for use as a key column in an index.
Any suggestions
February 17, 2014 at 10:04 pm
Previous section is works fine when 'col1 int NULL'
February 18, 2014 at 3:27 am
ashu.sajwan (2/17/2014)
...col1 varchar(Max) NULL,
...
CREATE UNIQUE CLUSTERED INDEX cuq
ON dbo.ExampleUnique (col1);
It shows error: Column 'col1' in table 'dbo.ExampleUnique' is of a type that is invalid for use as a key column in an index.
Any suggestions
The reason for the failure is that columns of lob type (nvarchar(max), varchar(max), varbinary(max), ntext, text, image) can't be key columns in an index. So for example varchar(768) is OK, but varchar(MAX) isn't.
Incidentally, I think this is a very good technique given that your version of SQL Server doesn't support filtered indexes.
Tom
February 18, 2014 at 3:36 am
Your suggestion works for me
Thanks Tom
February 18, 2014 at 4:14 am
Why clustered? This isn't typically where you would put the clustered index.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 18, 2014 at 4:22 am
Can you please share other approach to do this easiest way in sql server 2005?
February 18, 2014 at 4:26 am
I already did.
GilaMonster (2/17/2014)
For 2005, define a computed column on ISNULL(<column that you want to be unique>,<the primary key column of the table>) and put a unique index on that computed column.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 15 posts - 1 through 15 (of 29 total)
You must be logged in to reply to this topic. Login to reply