I have this very simple piece that's been bugging my brain for a few hours now:
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))
);
What I'm trying to achieve here is creating a constraint that, if the question field contains 'see picture' then the picture cannot be NULL else, it can be NULL for every question which doesn't contain 'see picture' in it. I had tried other expressions in the CHECK clause but in avail.
These inserts work fine:
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)
This is not working:
INSERT INTO driving_school_questions (question, picture)
VALUES ('blah blah', NULL);--it should work but constraint violation