Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce uniqueness of the first 35 characters of a varchar(45) column in SQL Server

I have table with articles and there is around 150.000 of records, in that table I have column name which is type of varchar(45). Now I want to make index, constraints or trigger on that table which will keep column name unique at left of 35 character of same column.

What will be proper way to do that, I am afraid that I am going to lose performance of this large table If I use wrong method.

like image 984
adopilot Avatar asked Nov 18 '25 14:11

adopilot


1 Answers

Create a computed column:

alter table YourTable add left35 as substring(col1, 1, 35)

Then you can create a unique index on that column:

create unique index IX_YourTable_left35 on YourTable(left35)

That will enfore the uniqueness of the left 35 characters of col1.

like image 172
Andomar Avatar answered Nov 20 '25 07:11

Andomar