标签:
1
|
INSERT INTO ON ... DUPLICATE KEY UPDATE ... |
1
2
3
4
5
6
7
8
9
10
11
12
|
[sql] CREATE TABLE `test_table` ( `id` int (11) NOT NULL AUTO_INCREMENT , `var1` varchar (100) CHARACTER SET utf8 DEFAULT NULL , `var2` tinyint(1) NOT NULL DEFAULT ‘0‘ , `var3` varchar (100) character set utf8 default NULL , `value1` int (11) NOT NULL DEFAULT ‘1‘ , `value2` int (11) NULL DEFAULT NULL , `value3` int (5) DEFAULT NULL , PRIMARY KEY (`Id`), UNIQUE INDEX `index_var` (`var1`, `var2`, `var3`) ) ENGINE=MyISAM DEFAULT CHARACTER SET =latin1 AUTO_INCREMENT=1; |
1
2
3
4
5
6
|
[sql] INSERT INTO `test_table` (`var1`, `var2`, `var3`, `value1`, `value2`, `value3`) VALUES ( ‘abcd‘ , 0, ‘xyz‘ , 1, 2, 3) ON DUPLICATE KEY UPDATE `value1` = `value1` + 1 AND `value2` = `value2` + 2 AND `value3` = `value3` + 3; |
1
2
3
4
5
6
7
8
9
10
11
12
|
[sql] CREATE TABLE `test_table` ( `id` int (11) NOT NULL AUTO_INCREMENT , `var1` varchar (1024) CHARACTER SET utf8 DEFAULT NULL , `var2` tinyint(1) NOT NULL DEFAULT ‘0‘ , `var3` varchar (1024) character set utf8 default NULL , `value1` int (11) NOT NULL DEFAULT ‘1‘ , `value2` int (11) NULL DEFAULT NULL , `value3` int (5) DEFAULT NULL , PRIMARY KEY (`Id`), UNIQUE INDEX `index_var` (`var1`, `var2`, `var3`) ) ENGINE=MyISAM DEFAULT CHARACTER SET =latin1 AUTO_INCREMENT=1; |
1
2
|
[sql] Specified key was too long; max key length is 1000 bytes |
1
2
3
4
5
6
7
8
9
10
11
|
[sql] CREATE TABLE `test_table` ( `id` int (11) NOT NULL AUTO_INCREMENT , `var1` varchar (1024) CHARACTER SET utf8 DEFAULT NULL , `var2` tinyint(1) NOT NULL DEFAULT ‘0‘ , `var3` varchar (1024) character set utf8 default NULL , `value1` int (11) NOT NULL DEFAULT ‘1‘ , `value2` int (11) NULL DEFAULT NULL , `value3` int (5) DEFAULT NULL , PRIMARY KEY (`Id`) ) ENGINE=MyISAM DEFAULT CHARACTER SET =latin1 AUTO_INCREMENT=1; |
1
2
3
4
5
6
|
[sql] INSERT INTO table (primarykey, field1, field2, ...) SELECT key , value1, value2, ... FROM dual WHERE not exists ( select * from table where primarykey = id); |
1
2
3
4
5
|
[sql] INSERT INTO `test_table` SELECT 0, ‘abcd‘ , 0, ‘xyz‘ , 1, 2, 3 FROM dual WHERE NOT EXISTS ( SELECT * FROM `test_table` WHERE `var1` = ‘abcd‘ AND `var2` = 0 AND `var3` = ‘xyz‘ ); |
1
2
3
4
|
[sql] UPDATE `test_table` SET `value1` = `value1` + 1, `value2` = `value2` + 2, `value3` = `value3` + 3 WHERE `val1` = ‘abcd‘ AND `val2` = 0 AND `val3` = ‘xyz‘ ; |
on duplicate key update之多列唯一索引
标签:
原文地址:http://www.cnblogs.com/aliceQin/p/4791643.html