I have tables: authors and rewards (one to many relations). And I would like make next rules:
- reward has link to author_id;
- restrict deletion reward if it has author_id foreign key;
- when I delete the author, then I want to cascade deletion rows from rewards table for this author_id.
I have used
constraint foreign key (author_id)
references authors (author_id)
on delete cascade on update cascade
but anyway I can delete row or all rows from rewards. How can I make deletion from rewards only when I delete author?
Created tables:
create table authors (
author_id bigint not null auto_increment,
birth_date datetime,
first_name varchar(255),
last_name varchar(255),
sex varchar(10),
primary key (author_id)
);
create table rewards (
reward_id bigint not null auto_increment,
title varchar(255),
year integer not null,
author_id bigint not null,
primary key (reward_id),
constraint foreign key (author_id) references authors (author_id)
on delete cascade on update cascade
);
Then insert values:
Insert into authors(
birth_date, first_name, last_name, sex) values
('1941-05-24', 'Bob', 'Dylan', 'male'),
('1870-10-22', 'Ivan', 'Bunin', 'male');
Insert into rewards(year, title, author_id) values
(2016, 'Nobel Prize for Literature', 1),
(1933, 'Nobel Prize for Literature', 2),
(1903, 'Pushkin Prize', 2)
;