Imagine you have the following table (note: this is a contrived/simplified example):
CREATE TABLE foo (
book_id number,
page number,
-- [a bunch of other columns describing a single page in a book]
);
ALTER TABLE foo
ADD (CONSTRAINT foo_pk PRIMARY KEY(book_id, page));
While (book_id, page) pairs are unique, the same page number will be repeated between books (many books will have a page 1). Therefore, if a SQL query doesn't specify a book_id, the wrong page(s) may be selected/updated/deleted. All of our queries should act on just one book at a time, but I've seen a couple bugs where the book_id parameter was accidentally omitted.
Is there a programmatic way to enforce that every select, insert, update, etc query specifies a book_id in the where clause?
We generate the SQL code for the queries dynamically and execute them using Spring's JdbcTemplate. The database is Oracle. Using automated tests to check that the many possible queries (plus new ones that get added in the future!) don't get tripped up by duplicate page_ids is tricky. I could override the JdbcTemplate code to ensure the sql queries always include a book_id parameter, but that involves manually parsing SQL code (especially tricky with subqueries) and seems hacky. Is there a more robust solution to enforce this? Some trigger, stored procedure, constraint?