英文:
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/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论