mysql数据库中有两张表academy
,instructors
. 我想按以下顺序向每个表插入值(伪代码):
- 第一个 INSERT INTO
academy
值acad_name, address, street, city, state
- 第二个 INSERT INTO
instructors
值instructor_name
- 3rd INSERT INTO
academy
valuesinstructor_id
FROMinstructors
\this 记录应与相应的academy_id
第三次插入基于第二次插入的结果,instructor_id
即 auto_increment。但是我希望第三个插入对应于当前学院的值,之前插入的值我该怎么做?
CREATE TABLE IF NOT EXISTS `academy` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`academy_id` int(11) NOT NULL,
`instructor_id` int(11) DEFAULT NULL,
`street_address` varchar(50) NOT NULL,
`city` varchar(25) NOT NULL,
`state` varchar(25) NOT NULL,
PRIMARY KEY (`id`),
KEY `instructor_id` (`instructor_id`) // Foreign KEY
);
CREATE TABLE IF NOT EXISTS `instructors` (
`instructor_id` int(11) NOT NULL AUTO_INCREMENT,
`academy_id` int(11) NOT NULL
PRIMARY KEY (`instructor_id`),
KEY `academy_id` (`academy_id`) // Foreign KEY
);