开发者

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

开发者 https://www.devze.com 2023-03-13 11:30 出处:网络
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 tha

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号