Du har udeladt alle fremmednøgler på bognavnet.
Det er derfor, jeg svarer med et komplet forbedret sæt tabeldefinitioner, det her handler om fremmednøgler, ikke? Shure, du gav et afklebet eksempel.
Problemet at løse var, at optegnelser i reading_event_discussion
skal handle om temaer, der findes i den bog:
drop table book cascade;
drop table book_theme;
drop table reading_event cascade;
drop table reading_event_discussion;
create table book (
name text primary key -- new, a must because it is FK in reading_event
);
insert into book (name) values ('game of thrones'),('Database design');
create table book_theme (
bookname text references book(name), -- new
themename text
);
insert into book_theme (bookname, themename) values
('game of thrones', 'ambition'), ('game of thrones', 'power');
create table reading_event (
i SERIAL primary key,
venue text,
bookread text references book(name) -- FK is new
);
insert into reading_event (venue, bookRead) VALUES
('Municipal Library', 'game of thrones');
-- this is the solution: extended reference check
create or replace function themecheck (i integer, th text) returns boolean as $$
select
(th in (select themename from book_theme bt
join reading_event re on i=re.i and re.bookRead=bt.bookname))
$$ language sql;
create table reading_event_discussion (
i integer references reading_event(i),
themeDiscussed text check (themecheck (i, themeDiscussed))
);
-- Test statements:
-- just check data
select * from reading_event;
-- this should be ok
insert into reading_event_discussion values (1,'ambition'),(1,'power');
-- this must be refused
insert into reading_event_discussion values (1,'databases');
Så løsningen er at skrive en brugerdefineret kontrolfunktion. Dette kan ikke overføres til andre databasesystemer.
Man kan skrive denne funktion på flere sprog (plpgsql, pltcl, ...), men SQL-funktioner kan indlejres i en forespørgsel og kan være hurtigere.