私はこのようなデータベーススキームを持っています。
create table Photographer(
id int primary key references Person(id) on update cascade on delete cascade,
livesIn int not null references Location(id) on update cascade on delete no action
);
create table Specialty(
photographer int references Photographer(id) on update cascade on delete cascade,
type enum('portrait','landscape','sport'),
primary key(photographer, type)
);
create table Photo(
id int primary key,
takenAt timestamp not null,
takenBy int references Photographer(id) on update cascade on delete no action,
photographedAt int references Location(id) on update cascade on delete no action,
type enum('portrait','landscape','sport')
);
写真家が写真家の専門分野の1つとしてtを持っている場合、写真家がタイプtの写真を撮る資格があるように、データベースを制約する必要があります。
私の試みはうまくいきませんでした。
/*Attempt 1*/
ALTER TABLE Photo ADD CONSTRAINT constraint_1
CHECK (Photo.type in (SELECT Specialty.type FROM Specialty,Photo,Photographer
WHERE Photo.takenBy = Photographer.id
AND Photographer.id = Specialty.photographer
AND Specialty.type = Photo.type));
/*Attempt 2 using exists instead of 'in'*/
ALTER TABLE Photo ADD CONSTRAINT constraint_1
CHECK (exists (SELECT * FROM Specialty,Photo,Photographer
WHERE Photo.takenBy = Photographer.id
AND Photographer.id = Specialty.photographer
AND Specialty.type = Photo.type));
私がここで間違っていることは特にありますか?