如何使用自动递增的ID将多行插入一个表,然后插入另一个表? MySQL的

时间:2012-08-03 18:44:49

标签: mysql sql database

所以我有两个由'skillid'键链接的表:

skills
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| skillid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| skillname | varchar(30) | NO   |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

students_skills
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| ssid      | int(11) | NO   | PRI | NULL    | auto_increment |
| studentid | int(11) | NO   | MUL | NULL    |                |
| skillid   | int(11) | NO   | MUL | NULL    |                |
+-----------+---------+------+-----+---------+----------------+

我正在尝试在表技能中插入多行,然后根据创建的ID将这些行插入student_skills。我一直在研究使用LAST_INSERT_ID()函数:

INSERT INTO skills (skillid , skillname)
    VALUES(NULL,'being grateful for help'); # generate ID by inserting NULL
INSERT INTO students_skills (ssid, studentid, skillid)
    VALUES(LAST_INSERT_ID(),'1', '2'); # use ID in second table

但我无法弄清楚如何在一个mysql表中同时为多行执行此操作。当我简单地为每一行复制上面的4行时,我收到错误。

  

错误:#1452 - 无法添加或更新子行:外键   约束失败(empologystudents_skills,CONSTRAINT   students_skills_ibfk_2外键(skillid)参考skills   (skillid))

我是否在正确的路线上?我也研究了连接,但这种方法对我来说更有意义。

感谢您提供任何帮助或有用的链接。

2 个答案:

答案 0 :(得分:3)

您必须确保使用多行插入语法,以便LAST_INSERT_ID()保持一致,即使您自动递增另一列:

INSERT INTO skills VALUES (NULL, 'test');

假设生成的skillid1,您可以执行以下操作:

INSERT INTO student_skills VALUES
(NULL, 1, LAST_INSERT_ID()),
(NULL, 2, LAST_INSERT_ID()),
(NULL, 3, LAST_INSERT_ID()),
(NULL, 4, LAST_INSERT_ID());

LAST_INSERT_ID()返回的值在所有四行中始终保持不变(1)。

但是,如果您将多个插入作为独立语句执行,LAST_INSERT_ID()将会更改,因为它将包含每个插入的生成的自动递增值:

INSERT INTO student_skills VALUES (NULL, 1, LAST_INSERT_ID());
INSERT INTO student_skills VALUES (NULL, 2, LAST_INSERT_ID());
INSERT INTO student_skills VALUES (NULL, 3, LAST_INSERT_ID());
INSERT INTO student_skills VALUES (NULL, 4, LAST_INSERT_ID());

其中LAST_INSERT_ID()是前一个插入的生成ID。


看看这个SQLFiddle Demo

答案 1 :(得分:1)

由于students_skills.ssidAUTO_INCREMENT列,因此您的第二个插入内容看起来不对。您似乎想要以下内容:

INSERT INTO skills (skillid , skillname)
    VALUES(NULL,'being grateful for help'); # generate ID by inserting NULL
INSERT INTO students_skills (ssid, studentid, skillid)
    VALUES(NULL,'1', LAST_INSERT_ID()); # use ID in second table

查看

的输出会很有帮助
SHOW CREATE TABLE skills;
SHOW CREATE TABLE students_skills;

查看FOREIGN KEYs

更新显示输出

+--------+------------------------------------------------------------------------------
| Table  | Create Table                                                                
+--------+------------------------------------------------------------------------------
| skills | CREATE TABLE `skills` (
  `skillid` int(11) NOT NULL AUTO_INCREMENT,
  `skillname` varchar(30) NOT NULL,
  PRIMARY KEY (`skillid`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 |
+--------+------------------------------------------------------------------------------

+-----------------+---------------------------------------------------------------------
| Table           | Create Table                                                        
+-----------------+---------------------------------------------------------------------
| students_skills | CREATE TABLE `students_skills` (
  `ssid` int(11) NOT NULL AUTO_INCREMENT,
  `studentid` int(11) NOT NULL,
  `skillid` int(11) NOT NULL,
  PRIMARY KEY (`ssid`),
  KEY `studentid` (`studentid`),
  KEY `skillid` (`skillid`),
  CONSTRAINT `students_skills_ibfk_1` FOREIGN KEY (`studentid`) REFERENCES `students` (`studentid`),
  CONSTRAINT `students_skills_ibfk_2` FOREIGN KEY (`skillid`) REFERENCES `skills` (`skillid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 |
+-----------------+--------------------------------------------------------------------