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

时间:2022-08-24 21:39:22

so I have two tables linked by the key 'skillid':

所以我有两个表由关键'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    |                |
+-----------+---------+------+-----+---------+----------------+

I'm trying to insert multiple rows into the table skills, and then insert these into student_skills based on the ID that was created. I've been looking into using the LAST_INSERT_ID() function:

我正在尝试在表技能中插入多行,然后根据创建的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

But I couldn't figure out how to do this for multiple rows at once in one mysql table. I get an error when i simply duplicate the above 4 lines for every row.

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

ERROR: #1452 - Cannot add or update a child row: a foreign key constraint fails (empology.students_skills, CONSTRAINT students_skills_ibfk_2 FOREIGN KEY (skillid) REFERENCES skills (skillid))

错误:#1452 - 无法添加或更新子行:外键约束失败(empology.students_skills,CONSTRAINT students_skills_ibfk_2 FOREIGN KEY(skillid)REFERENCES技能(技能))

Am I on the right lines or not? I looked into joins also but this method made more sense to me.

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

Thanks for any help or useful links.

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

2 个解决方案

#1


4  

You have to make sure to use multiple-row insert syntax so that the LAST_INSERT_ID() stays consistent even though you're auto-incrementing another column:

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

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

Say the skillid generated was 1, you can then do:

假设生成的技能是1,那么你可以这样做:

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

The value returned by LAST_INSERT_ID() will consistently stay the same (1) throughout all four rows.

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

However, if you execute multiple inserts as standalone statements, LAST_INSERT_ID() will change as it will instead contain the generated auto-incremented value of each insert:

但是,如果您将多个插入作为独立语句执行,则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());

Where LAST_INSERT_ID() is the generated id of the immediate previous insert.

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


Take a look at this SQLFiddle Demo

看看这个SQLFiddle演示

#2


2  

Since students_skills.ssid is an AUTO_INCREMENT column, your second insert looks wrong. It seems you want the following:

由于students_skills.ssid是AUTO_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

It would be helpful to see the output of

看看输出会很有帮助

SHOW CREATE TABLE skills;
SHOW CREATE TABLE students_skills;

to see the FOREIGN KEYs.

看到FOREIGN KEYs。

UPDATE TO SHOW OUTPUTS

更新显示输出

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

#1


4  

You have to make sure to use multiple-row insert syntax so that the LAST_INSERT_ID() stays consistent even though you're auto-incrementing another column:

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

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

Say the skillid generated was 1, you can then do:

假设生成的技能是1,那么你可以这样做:

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

The value returned by LAST_INSERT_ID() will consistently stay the same (1) throughout all four rows.

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

However, if you execute multiple inserts as standalone statements, LAST_INSERT_ID() will change as it will instead contain the generated auto-incremented value of each insert:

但是,如果您将多个插入作为独立语句执行,则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());

Where LAST_INSERT_ID() is the generated id of the immediate previous insert.

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


Take a look at this SQLFiddle Demo

看看这个SQLFiddle演示

#2


2  

Since students_skills.ssid is an AUTO_INCREMENT column, your second insert looks wrong. It seems you want the following:

由于students_skills.ssid是AUTO_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

It would be helpful to see the output of

看看输出会很有帮助

SHOW CREATE TABLE skills;
SHOW CREATE TABLE students_skills;

to see the FOREIGN KEYs.

看到FOREIGN KEYs。

UPDATE TO SHOW OUTPUTS

更新显示输出

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