1

我有 3 张桌子

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)) 

.我已将数据插入其中。

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)

它的InnoDB引擎我的问题是movie_actor表中的movie_id 39不应该被允许按照REFERENTIAL INTEGRITY插入到movie_actor表中,对吗?

4

1 回答 1

3

要具有参照完整性,您必须设置外键约束:

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)
    )
于 2012-10-11T12:02:01.600 回答