1

I have 3 tables

create table movies( movie_id int PRIMARY KEY AUTO_INCREMENT, budget decimal(10,3) NOT NULL, name varchar(20) NOT NULL); create table actor( actor_id int primary key auto_increment, name varchar(13) not null, salary decimal(12,2) not null); create table movie_actor( movie_id int not null references movies (movie_id), actor_id int not null references actor (actor_id), constraint movie_actor_pk primary key (movie_id,actor_id)) 

.I have inserted data into them.

mysql> select * from movies; +----------+----------+------------+ | movie_id | budget | name | +----------+----------+------------+ | 1 | 1000.000 | rambo | | 2 | 2000.000 | rocky | | 3 | 8000.000 | terminator | +----------+----------+------------+ 3 rows in set (0.00 sec) mysql> select * from actor; +----------+-----------+--------+ | actor_id | name | salary | +----------+-----------+--------+ | 100 | sylvester | 100.00 | | 101 | arnold | 90.00 | +----------+-----------+--------+ 2 rows in set (0.00 sec) mysql> select * from movie_actor; +----------+----------+ | movie_id | actor_id | +----------+----------+ | 1 | 100 | | 2 | 100 | | 3 | 101 | | 39 | 10109 | +----------+----------+ 4 rows in set (0.00 sec) 

Its InnoDB engine My question is movie_id 39 in movie_actor table should not been allowed to be inserted into movie_actor table as per REFERENTIAL INTEGRITY,correct?

1 Answer 1

3

To have referential integrity you must set the foreign key constraint:

create table movie_actor( movie_id int not null references movies (movie_id), actor_id int not null references actor (actor_id), constraint movie_actor_pk primary key (movie_id,actor_id), constraint foreign key (movie_id) references movies (movie_id), constraint foreign key (actor_id) references actor (actor_id) ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Clodoaldo .It worked.But a small correction,there should be a comma before every constraint

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.