patternsqlModerate
Is there any way to have Unique Constraint and NOT have Unique Index?
Viewed 0 times
uniqueanywayconstraintandindextherenothave
Problem
Looking into the Microsoft documentation, it says when a unique constraint is created, a unique index is created automatically.
Is there a trick/workaround to have a unique constraint and NOT have a unique index, so it does not take space?
Is there a trick/workaround to have a unique constraint and NOT have a unique index, so it does not take space?
Solution
Reasonable Answer: No.
You really should just let it create the unique index and let it do it's job. Space is cheap.
If you make the column you want unique the clustered index key, then it won't cost you any additional disk space, but it may slow down access to the table if your queries don't go through the unique column.
Less Reasonable Answer: Yes.
But it burns CPU, increases disk I/O and slows down just about everything.
You put an
Example (of how not to do it):
You really should just let it create the unique index and let it do it's job. Space is cheap.
If you make the column you want unique the clustered index key, then it won't cost you any additional disk space, but it may slow down access to the table if your queries don't go through the unique column.
Less Reasonable Answer: Yes.
But it burns CPU, increases disk I/O and slows down just about everything.
You put an
AFTER trigger on the table and have it count the number of rows in the base table for the column you want to make unique. If the count is greater than 0 then rollback the transaction. Example (of how not to do it):
CREATE TABLE dbo.Test
(
TestID INT NOT NULL PRIMARY KEY IDENTITY(1,1)
, TestValue VARCHAR(100) NOT NULL
);
GO
CREATE TRIGGER trg_TestInsert ON dbo.Test AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @DuplicateCount INT;
SET @DuplicateCount = 0;
WITH CTE_Count AS
(
SELECT TestValue, COUNT(*) AS TestValueCount
FROM dbo.Test
GROUP BY TestValue
)
SELECT @DuplicateCount = COUNT(TestValue)
FROM CTE_Count
WHERE TestValueCount >=2;
IF @DuplicateCount >= 1
BEGIN
ROLLBACK;
END
END;
GO
/** Test Insert Statement */
INSERT INTO dbo.Test (TestValue)
VALUES ('TestMe');
INSERT INTO dbo.Test (TestValue)
VALUES ('TestYou');
--HINT: This one will fail.
INSERT INTO dbo.Test (TestValue)
VALUES ('TestYou');
SELECT TestID, TestValue FROM dbo.Test;
--Cleanup.
DROP TABLE dbo.Test;Code Snippets
CREATE TABLE dbo.Test
(
TestID INT NOT NULL PRIMARY KEY IDENTITY(1,1)
, TestValue VARCHAR(100) NOT NULL
);
GO
CREATE TRIGGER trg_TestInsert ON dbo.Test AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @DuplicateCount INT;
SET @DuplicateCount = 0;
WITH CTE_Count AS
(
SELECT TestValue, COUNT(*) AS TestValueCount
FROM dbo.Test
GROUP BY TestValue
)
SELECT @DuplicateCount = COUNT(TestValue)
FROM CTE_Count
WHERE TestValueCount >=2;
IF @DuplicateCount >= 1
BEGIN
ROLLBACK;
END
END;
GO
/** Test Insert Statement */
INSERT INTO dbo.Test (TestValue)
VALUES ('TestMe');
INSERT INTO dbo.Test (TestValue)
VALUES ('TestYou');
--HINT: This one will fail.
INSERT INTO dbo.Test (TestValue)
VALUES ('TestYou');
SELECT TestID, TestValue FROM dbo.Test;
--Cleanup.
DROP TABLE dbo.Test;Context
StackExchange Database Administrators Q#205352, answer score: 13
Revisions (0)
No revisions yet.