Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Apply Unique constraints on existing database Expand / Collapse
Author
Message
Posted Monday, February 17, 2014 6:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 10:45 PM
Points: 19, Visits: 35
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
Post #1542081
Posted Monday, February 17, 2014 6:43 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 5:39 AM
Points: 2,104, Visits: 5,378
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/
Post #1542083
Posted Monday, February 17, 2014 6:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 10:45 PM
Points: 19, Visits: 35
Yes i need this thanks for your help
Post #1542087
Posted Monday, February 17, 2014 6:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 10:45 PM
Points: 19, Visits: 35
How can i do it with sql server 2005
Post #1542090
Posted Monday, February 17, 2014 6:55 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:05 AM
Points: 41,532, Visits: 34,449
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 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1542091
Posted Monday, February 17, 2014 7:03 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 10:45 PM
Points: 19, Visits: 35
Can i apply this approach for column which is varchar type?
Post #1542099
Posted Monday, February 17, 2014 7:13 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:05 AM
Points: 41,532, Visits: 34,449
Yes.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1542109
Posted Monday, February 17, 2014 11:23 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 12:16 AM
Points: 8,289, Visits: 8,742
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
Post #1542233
Posted Monday, February 17, 2014 9:51 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 10:45 PM
Points: 19, Visits: 35
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
Post #1542357
Posted Monday, February 17, 2014 10:04 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 18, 2014 10:45 PM
Points: 19, Visits: 35

Previous section is works fine when 'col1 int NULL'
Post #1542359
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse