I'm having difficulties adding a constraint with two constraint conditions to an already existing table. I'm working with the two relations below, and I'm trying to add the constraint to the "books" table.
The relations are:
books((book_id), title, author_id, subject_id)
subjects((subject_id), subject, location)
Where the keys within the parantheses are primary keys, and the italics are foreign keys.
The first criteria/condition is that the subject_id is NOT NULL, and that the subject_id when inserting a new book-tuple into books, already has to exist as a primary key in the subjects relation.
ALTER TABLE books ADD CONSTRAINT hasSubject
CHECK(subject_id IS NOT NULL AND subject_id REFERENCES subjects(subject_id))
I keep getting the error message: "syntax error at or near 'REFERENCES'"
Any ideas? Thank you in advance!