Skip to content

Instantly share code, notes, and snippets.

@andrewkdouglas
Created March 28, 2013 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewkdouglas/5267344 to your computer and use it in GitHub Desktop.
Save andrewkdouglas/5267344 to your computer and use it in GitHub Desktop.
Using LAST_INSERT_ID() to get the last inserted AUTO_INCREMENT value
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