MySql引用完整性

MySql引用完整性,mysql,Mysql,我有三张桌子 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, sal

我有三张桌子

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插入movie_actor表,对吗?

要具有引用完整性,必须设置外键约束:

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

谢谢Clodoaldo。它很有效。但是一个小的修正,在每个约束之前应该有一个逗号