用于根据另一列值限制 NULL 值的 CHECK 约束
CHECK constraint for restricting NULL values based on another column values
我有一个非常简单的作品已经困扰我几个小时了:
CREATE TABLE driving_school_questions(
question VARCHAR2(200),
picture VARCHAR2(2),
CONSTRAINT q_p_chk CHECK ((question LIKE '%see picture%' AND picture IS NOT NULL)
AND
(question LIKE '% %' OR picture IS NULL))
);
我想在这里实现的是创建一个约束,如果问题字段包含 'see picture' 那么图片不能为 NULL 否则,对于每个不包含的问题都可以为 NULL 'see picture' 在里面。我曾在 CHECK 子句中尝试过其他表达式,但无济于事。
这些插入工作正常:
INSERT INTO driving_school_questions (question, picture)
VALUES ('blahblah see picture', '23'); --NOT NULL so ok for now
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah see picture ', NULL); --It's ok to be NULL(constraint violated)
这不起作用:
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah', NULL);--it should work but constraint violation
我觉得你只需要一个检查,防止'see picture'和null的单一组合:
CREATE TABLE driving_school_questions(
question VARCHAR2(200),
picture VARCHAR2(2),
CONSTRAINT q_p_chk CHECK (NOT(question LIKE '%see picture%' AND picture IS NULL))
);
这会查找那个单一组合,并检查不是您拥有的组合。
INSERT INTO driving_school_questions (question, picture)
VALUES ('blahblah see picture', '23');
1 row inserted.
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah see picture ', NULL);
Error report -
ORA-02290: check constraint (SCHEMA.Q_P_CHK) violated
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah', NULL);
1 row inserted.
正如@vkp 所建议的,您可以使用正则表达式来检查 'see picture' 部分,以防止错误匹配以及大小写问题:
CONSTRAINT q_p_chk CHECK (NOT(REGEXP_LIKE(question, '(^|\s)see picture(\s|$)', 'i')
AND picture IS NULL))
这意味着这两个都可以:
INSERT INTO driving_school_questions (question, picture)
VALUES ('blahblah isee pictures', null);
INSERT INTO driving_school_questions (question, picture)
VALUES ('See picture', '25');
但这是不允许的:
INSERT INTO driving_school_questions (question, picture)
VALUES ('See Picture', null);
您甚至可能只想将整个字符串值限制为 'see picture',或几个可能值之一;你也可以用稍微修改过的正则表达式模式来做。
我有一个非常简单的作品已经困扰我几个小时了:
CREATE TABLE driving_school_questions(
question VARCHAR2(200),
picture VARCHAR2(2),
CONSTRAINT q_p_chk CHECK ((question LIKE '%see picture%' AND picture IS NOT NULL)
AND
(question LIKE '% %' OR picture IS NULL))
);
我想在这里实现的是创建一个约束,如果问题字段包含 'see picture' 那么图片不能为 NULL 否则,对于每个不包含的问题都可以为 NULL 'see picture' 在里面。我曾在 CHECK 子句中尝试过其他表达式,但无济于事。
这些插入工作正常:
INSERT INTO driving_school_questions (question, picture)
VALUES ('blahblah see picture', '23'); --NOT NULL so ok for now
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah see picture ', NULL); --It's ok to be NULL(constraint violated)
这不起作用:
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah', NULL);--it should work but constraint violation
我觉得你只需要一个检查,防止'see picture'和null的单一组合:
CREATE TABLE driving_school_questions(
question VARCHAR2(200),
picture VARCHAR2(2),
CONSTRAINT q_p_chk CHECK (NOT(question LIKE '%see picture%' AND picture IS NULL))
);
这会查找那个单一组合,并检查不是您拥有的组合。
INSERT INTO driving_school_questions (question, picture)
VALUES ('blahblah see picture', '23');
1 row inserted.
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah see picture ', NULL);
Error report -
ORA-02290: check constraint (SCHEMA.Q_P_CHK) violated
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah', NULL);
1 row inserted.
正如@vkp 所建议的,您可以使用正则表达式来检查 'see picture' 部分,以防止错误匹配以及大小写问题:
CONSTRAINT q_p_chk CHECK (NOT(REGEXP_LIKE(question, '(^|\s)see picture(\s|$)', 'i')
AND picture IS NULL))
这意味着这两个都可以:
INSERT INTO driving_school_questions (question, picture)
VALUES ('blahblah isee pictures', null);
INSERT INTO driving_school_questions (question, picture)
VALUES ('See picture', '25');
但这是不允许的:
INSERT INTO driving_school_questions (question, picture)
VALUES ('See Picture', null);
您甚至可能只想将整个字符串值限制为 'see picture',或几个可能值之一;你也可以用稍微修改过的正则表达式模式来做。