如何实现属性ID以指定字母开头的检查约束?

huangapple go评论61阅读模式
英文:

How can I implement a check constraint for attribute ID to start with specified letters?

问题

我希望我的属性ID以字母AB开头,后面跟着正好8个整数。当我希望它以指定的字母开头和结尾时,我该如何做?

CREATE TABLE Table(
    ID TEXT NOT NULL 
        PRIMARY KEY 
        CHECK (ID LIKE 'AB________')
);
英文:

I want my attribute ID to start with the letters AB, followed by exactly 8 integers. And how would I do it, when I want it to start and end with defined letters?

CREATE TABLE Table(
    ID TEXT NOT NULL 
        PRIMARY KEY 
        CHECK (ID LIKE AB________')
);

答案1

得分: 3

使用GLOB运算符:

CHECK (ID GLOB 'AB[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')

查看演示。<br/>

或者:

CHECK (ID GLOB 'AB' || REPLACE(HEX(ZEROBLOB(8)), '00', '[0-9]'))

查看演示。<br/>

英文:

Use the GLOB operator:

CHECK (ID GLOB &#39;AB[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]&#39;)

See the demo.<br/>

Or:

CHECK (ID GLOB &#39;AB&#39; || REPLACE(HEX(ZEROBLOB(8)), &#39;00&#39;, &#39;[0-9]&#39;))

See the demo.<br/>

huangapple
  • 本文由 发表于 2023年5月28日 22:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定