Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Created March 9, 2016 20:01
Show Gist options
  • Save brennanMKE/51c6ba6466bd18c3295d to your computer and use it in GitHub Desktop.
Save brennanMKE/51c6ba6466bd18c3295d to your computer and use it in GitHub Desktop.
MySQL Upsert
DROP TABLE IF EXISTS `things`;
CREATE TABLE `things` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nbr` int(11) NOT NULL,
`thing` varchar(255) NOT NULL,
`createdAt` datetime NOT NULL DEFAULT NOW(),
`updatedAt` datetime NOT NULL DEFAULT NOW() ON UPDATE NOW(),
PRIMARY KEY (`id`),
UNIQUE KEY `nbr_unique` (`nbr`)
);
INSERT INTO `things` (nbr,thing) VALUES (1, 'A Thing') ON DUPLICATE KEY UPDATE thing = 'A Thing';
SELECT * FROM `things`;
INSERT INTO `things` (nbr,thing) VALUES (1, 'Another Thing') ON DUPLICATE KEY UPDATE thing = 'Another Thing';
SELECT * FROM `things`;
DELETE FROM `things`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment