I have a varchar(max)
column where users will populate a pre-given key from another system. I want to enable that the users are forced to enter a unique key which means no duplicate values on that specific column. Should I change the collation or is there some sort of check constraint that I can add?
Sql-server – Add Check Constraint On Varchar Column to enforce no duplicates
sql serversql-server-2008
Related Question
- Sql-server – Primary key guarantees: duplicates and nullity
- Sql-server – Constraint on existing table – unique combination of values only when another column is null
- Sql-server – Implement check constraint for varchar (7) to enforce YYYY-MM
- SQL Server – SSIS Data Flow Task Violates Unique Constraint But Linked Server Insert Succeeds
- SQL Server Error – Error 666 on Clustered Primary Key
- SSMS Import/Export Wizard – Violation of PRIMARY KEY Constraint
Best Answer
Ordinarily, you'd just create a unique constraint on that column. But you can't put a unique constraint on a varchar(max) column. That could be close to 2GB per index entry.
In SQL Server, the maximum size of an index key is 900 bytes. I believe SQL Server enforces unique constraints by using a unique index, so I'd expect that limitation to apply to your problem, too.
In any case, I think this is an XY problem.