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 Tuesday, February 18, 2014 3:27 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 12:00 PM
Points: 7,847, Visits: 9,596
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
Post #1542441
Posted Tuesday, February 18, 2014 3:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 7, 2014 7:01 AM
Points: 20, Visits: 47
Your suggestion works for me
Thanks Tom
Post #1542445
Posted Tuesday, February 18, 2014 4:14 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: Yesterday @ 2:01 PM
Points: 40,390, Visits: 36,823
Why clustered? This isn't typically where you would put the clustered index.


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 #1542460
Posted Tuesday, February 18, 2014 4:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 7, 2014 7:01 AM
Points: 20, Visits: 47
Can you please share other approach to do this easiest way in sql server 2005?
Post #1542465
Posted Tuesday, February 18, 2014 4:26 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: Yesterday @ 2:01 PM
Points: 40,390, Visits: 36,823
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 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 #1542467
Posted Tuesday, February 18, 2014 4:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 7, 2014 7:01 AM
Points: 20, Visits: 47
Can you explain it as in sql form as below


CREATE TABLE dbo.Example
(
col1 varchar(100) 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
Post #1542473
Posted Tuesday, February 18, 2014 4:32 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: Yesterday @ 2:01 PM
Points: 40,390, Visits: 36,823
Why a view, rather than just an indexed computed column? It'll work, but....

Do you not have a column that is already unique in the table, eg the primary key columns?



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 #1542475
Posted Tuesday, February 18, 2014 4:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 7, 2014 7:01 AM
Points: 20, Visits: 47
Can you mention a example or a dummy script which create a indexed computed column?
Post #1542480
Posted Tuesday, February 18, 2014 4:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 7, 2014 7:01 AM
Points: 20, Visits: 47
Below script will work in this case or not

CREATE TABLE TEST_UQ (COL1 INT IDENTITY(1,1) PRIMARY KEY, COL2 VARCHAR(10) NULL,COL3 AS (CASE WHEN COL2 IS NULL THEN CAST(COL1 AS VARCHAR(10)) ELSE COL2 END))
GO

CREATE UNIQUE INDEX TEST_UQ_IND_1 ON TEST_UQ (COL3)
GO

This also works for in my case
Post #1542491
Posted Tuesday, February 18, 2014 5:15 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: Yesterday @ 2:01 PM
Points: 40,390, Visits: 36,823
Yup, that's it. Should be less overhead than an indexed view.


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 #1542496
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse