Last active
January 24, 2017 12:58
-
-
Save jurv/46ae13e94461ae874a5c to your computer and use it in GitHub Desktop.
MySQL safe operations
This file contains hidden or 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
DROP PROCEDURE IF EXISTS addcol; | |
delimiter '//' | |
CREATE PROCEDURE addcol() BEGIN | |
IF NOT EXISTS( | |
( | |
SELECT * FROM information_schema.COLUMNS | |
WHERE TABLE_SCHEMA=DATABASE() | |
AND TABLE_NAME='my_table' | |
AND COLUMN_NAME='value4' | |
) | |
) THEN | |
ALTER TABLE my_table ADD `value4` INT NOT NULL DEFAULT 1 AFTER `value3`; | |
END IF; | |
END; | |
// | |
delimiter ';' | |
CALL addcol(); | |
DROP PROCEDURE IF EXISTS addcol; |
This file contains hidden or 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
DROP PROCEDURE IF EXISTS altercol; | |
delimiter '//' | |
CREATE PROCEDURE altercol() BEGIN | |
IF EXISTS( | |
( | |
SELECT * FROM information_schema.COLUMNS | |
WHERE TABLE_SCHEMA=DATABASE() | |
AND TABLE_NAME='my_table' | |
AND COLUMN_NAME='value4' | |
) | |
) THEN | |
ALTER TABLE `my_table` MODIFY `value4` VARCHAR(255) DEFAULT NULL; | |
END IF; | |
END; | |
// | |
delimiter ';' | |
CALL altercol(); | |
DROP PROCEDURE IF EXISTS altercol; |
This file contains hidden or 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 IF NOT EXISTS `my_table` ( | |
`id` INT(11) NOT NULL AUTO_INCREMENT, | |
`value1` VARCHAR(255) DEFAULT NULL, | |
`value2` VARCHAR(255) DEFAULT NULL, | |
`value3` VARCHAR(255) DEFAULT NULL, | |
KEY `id` (`id`) | |
); |
This file contains hidden or 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
/* Insert in first table */ | |
INSERT INTO `my_table` VALUES (NULL, 'My first line', 'and second col', 'then first value3', 'finally, last value'); | |
/* Retrieve the ID of the newly created line */ | |
SELECT @MAX:=MAX(id) FROM `my_table`; | |
/* Insert a new line with this */ | |
INSERT INTO `my_second_table` (`id`, `my_table_id`, `value1`) VALUES (NULL, @MAX, 'My first line on second table !'); |
This file contains hidden or 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
INSERT INTO `my_table`(`value1`,`value2`,`value3`,`value4`) | |
SELECT * FROM ( | |
SELECT 'My first line', 'and second col', 'then first value3', 'finally, last value') AS tmp | |
WHERE NOT EXISTS ( | |
SELECT `value1` | |
FROM `my_table` | |
WHERE `value1` = 'My first line' | |
) LIMIT 1; |
This file contains hidden or 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
INSERT INTO `my_table`(`value1`,`value2`,`value3`,`value4`) | |
SELECT * FROM ( | |
SELECT 'My first line' as col1, 'My first line' as col2, 'My first line' as col3, 'My first line' as col4) AS tmp | |
WHERE NOT EXISTS ( | |
SELECT `value1` | |
FROM `my_table` | |
WHERE `value1` = 'My first line' | |
) LIMIT 1; |
This file contains hidden or 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
SELECT @line:=`id` FROM `my_table` WHERE (value1 = 'My first line'); | |
INSERT INTO `my_second_table` (`my_table_id`, `value1`) | |
SELECT * FROM ( | |
SELECT @line, 'My first line on second table !') AS tmp | |
WHERE NOT EXISTS ( | |
SELECT `value1` | |
FROM `my_second_table` | |
WHERE (`value1` = 'My first line on second table !') | |
) | |
LIMIT 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment