在SQLite中创建约束难以

发布于 2025-02-11 08:10:50 字数 751 浏览 2 评论 0原文

我有一个具有联系的表作为列的表,其限制是按照(000)0000000000的模式,

create table patient
(USERNUM INT PRIMARY KEY,
CONTACT varchar(15) constraint check_contact check (contact like '%([0-9]*3)[0-9]*10%'),
age int constraint check_age check (age >0 and age <=100),
location varchar(50)
);


这不允许我插入记录,尽管数字的模式相同。 我已经用下面的命令更改了我的表作为替代方案

alter table patient add contact varchar(15) constraint ch_contact 
check (SUBSTRING(contact, 1, 1) = '(' and SUBSTRING(contact, 5, 1) = ')' 
and SUBSTRING(contact, 2, 4) like '[0-9]' and SUBSTRING(contact, 6, 15) like '[0-9]');

,但它不允许我插入记录如下

insert into patient (USERNUM, CONTACT, AGE, LOCATION)
VALUES (5, '(000)0234567890', 34, 'VIZAG');
'''

I have a table with contact as one of the column with constraint following the pattern of (000)0000000000

create table patient
(USERNUM INT PRIMARY KEY,
CONTACT varchar(15) constraint check_contact check (contact like '%([0-9]*3)[0-9]*10%'),
age int constraint check_age check (age >0 and age <=100),
location varchar(50)
);


This does not allow me to insert the record though the pattern of number is same.
I have altered my table with below command as an alternative

alter table patient add contact varchar(15) constraint ch_contact 
check (SUBSTRING(contact, 1, 1) = '(' and SUBSTRING(contact, 5, 1) = ')' 
and SUBSTRING(contact, 2, 4) like '[0-9]' and SUBSTRING(contact, 6, 15) like '[0-9]');

but it does not allow me to insert the record as below

insert into patient (USERNUM, CONTACT, AGE, LOCATION)
VALUES (5, '(000)0234567890', 34, 'VIZAG');
'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-02-18 08:10:50

您可能可以在此处使用glob操作员:

CONTACT varchar(15) CONSTRAINT check_contact
    CHECK (contact GLOB '([0-9][0-9][0-9])[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')

You might be able to use the GLOB operator here:

CONTACT varchar(15) CONSTRAINT check_contact
    CHECK (contact GLOB '([0-9][0-9][0-9])[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文