Created
March 28, 2013 22:25
-
-
Save andrewkdouglas/5267344 to your computer and use it in GitHub Desktop.
Using LAST_INSERT_ID() to get the last inserted AUTO_INCREMENT value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `A` ( | |
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , | |
`name` VARCHAR(45) NULL , | |
`name` VARCHAR(45) NULL , | |
PRIMARY KEY (`id`)); | |
CREATE TABLE `B` ( | |
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT , | |
`aId` BIGINT UNSIGNED NOT NULL , | |
`info` VARCHAR(128) NULL , | |
PRIMARY KEY (`id`) , | |
INDEX `B_ibfk_1_idx` (`aId` ASC) , | |
CONSTRAINT `B_ibfk_1` | |
FOREIGN KEY (`aId` ) | |
REFERENCES `A` (`id` )); | |
INSERT INTO `A` | |
(`name`, | |
`thing`) | |
VALUES | |
( | |
'Some record', | |
'Useful information' | |
); | |
SET @id = LAST_INSERT_ID(); | |
INSERT INTO `B` | |
(`aId`, | |
`info`) | |
VALUES | |
( | |
@id, | |
'Other useful thing' | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment