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?
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?
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 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; ISOLATION_LEVEL being used. To prevent duplicate rows, you'll need to use SERIALIZABLE (because REPEATABLE_READ allows phantom reads) which, if you don't have an index on the column (and use the query shown here), is going to lock the entire table.