1
DROP TABLE IF EXISTS t1, t2;
2
CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B));
3
INSERT t1 VALUES (1,2,10), (3,4,20);
4
INSERT t1 VALUES (5,6,30) ON DUPLICATE KEY UPDATE c=c+100;
10
INSERT t1 VALUES (5,7,40) ON DUPLICATE KEY UPDATE c=c+100;
16
INSERT t1 VALUES (8,4,50) ON DUPLICATE KEY UPDATE c=c+1000;
22
INSERT t1 VALUES (1,4,60) ON DUPLICATE KEY UPDATE c=c+10000;
28
INSERT t1 VALUES (1,9,70) ON DUPLICATE KEY UPDATE c=c+100000, b=4;
29
ERROR 23000: Duplicate entry '4' for key 'b'
36
INSERT t1 VALUES (1,2,10), (3,4,20);
37
INSERT t1 VALUES (5,6,30), (7,4,40), (8,9,60) ON DUPLICATE KEY UPDATE c=c+100;
44
INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0;
51
INSERT t1 VALUES (2,1,11), (7,4,40) ON DUPLICATE KEY UPDATE c=c+VALUES(a);
52
SELECT *, VALUES(a) FROM t1;
59
explain extended SELECT *, VALUES(a) FROM t1;
60
id select_type table type possible_keys key key_len ref rows filtered Extra
61
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00
63
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c`,values(`test`.`t1`.`a`) AS `VALUES(a)` from `test`.`t1`
64
explain extended select * from t1 where values(a);
65
id select_type table type possible_keys key key_len ref rows filtered Extra
66
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 100.00 Using where
68
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c` from `test`.`t1` where values(`test`.`t1`.`a`)
70
create table t1(a int primary key, b int);
71
insert into t1 values(1,1),(2,2),(3,3),(4,4),(5,5);
79
insert into t1 values(4,14),(5,15),(6,16),(7,17),(8,18)
80
on duplicate key update b=b+10;
82
info: Records: 5 Duplicates: 2 Warnings: 0
93
replace into t1 values(5,25),(6,26),(7,27),(8,28),(9,29);
95
info: Records: 5 Duplicates: 4 Warnings: 0
108
CREATE TABLE t1 (a INT, b INT, c INT, UNIQUE (A), UNIQUE(B));
109
INSERT t1 VALUES (1,2,10), (3,4,20);
110
INSERT t1 SELECT 5,6,30 FROM DUAL ON DUPLICATE KEY UPDATE c=c+100;
116
INSERT t1 SELECT 5,7,40 FROM DUAL ON DUPLICATE KEY UPDATE c=c+100;
122
INSERT t1 SELECT 8,4,50 FROM DUAL ON DUPLICATE KEY UPDATE c=c+1000;
128
INSERT t1 SELECT 1,4,60 FROM DUAL ON DUPLICATE KEY UPDATE c=c+10000;
134
INSERT t1 SELECT 1,9,70 FROM DUAL ON DUPLICATE KEY UPDATE c=c+100000, b=4;
135
ERROR 23000: Duplicate entry '4' for key 'b'
142
INSERT t1 VALUES (1,2,10), (3,4,20);
143
CREATE TABLE t2 (a INT, b INT, c INT, d INT);
144
INSERT t2 VALUES (5,6,30,1), (7,4,40,1), (8,9,60,1);
145
INSERT t2 VALUES (2,1,11,2), (7,4,40,2);
146
INSERT t1 SELECT a,b,c FROM t2 WHERE d=1 ON DUPLICATE KEY UPDATE c=t1.c+100;
153
INSERT t1 SET a=5 ON DUPLICATE KEY UPDATE b=0;
160
INSERT t1 SELECT a,b,c FROM t2 WHERE d=2 ON DUPLICATE KEY UPDATE c=c+VALUES(a);
161
ERROR 23000: Column 'c' in field list is ambiguous
162
INSERT t1 SELECT a,b,c FROM t2 WHERE d=2 ON DUPLICATE KEY UPDATE c=t1.c+VALUES(t1.a);
163
SELECT *, VALUES(a) FROM t1;
172
create table t1 (a int not null unique) engine=myisam;
173
insert into t1 values (1),(2);
174
insert ignore into t1 select 1 on duplicate key update a=2;
179
insert ignore into t1 select a from t1 as t2 on duplicate key update a=t1.a+1 ;
184
insert into t1 select 1 on duplicate key update a=2;
189
insert into t1 select a from t1 on duplicate key update a=a+1 ;
190
ERROR 23000: Column 'a' in field list is ambiguous
191
insert ignore into t1 select a from t1 on duplicate key update a=t1.a+1 ;
192
ERROR 23000: Column 't1.a' in field list is ambiguous
195
a BIGINT(20) NOT NULL DEFAULT 0,
198
INSERT INTO t1 ( a ) SELECT 0 ON DUPLICATE KEY UPDATE a = a + VALUES (a) ;
206
INSERT INTO t1 VALUES (45, 1) ON DUPLICATE KEY UPDATE b =
207
IF(VALUES(b) > t1.b, VALUES(b), t1.b);
211
INSERT INTO t1 VALUES (45, 2) ON DUPLICATE KEY UPDATE b =
212
IF(VALUES(b) > t1.b, VALUES(b), t1.b);
216
INSERT INTO t1 VALUES (45, 1) ON DUPLICATE KEY UPDATE b =
217
IF(VALUES(b) > t1.b, VALUES(b), t1.b);
222
CREATE TABLE t1 (i INT PRIMARY KEY, j INT);
223
INSERT INTO t1 SELECT 1, j;
224
ERROR 42S22: Unknown column 'j' in 'field list'
226
CREATE TABLE t1 (i INT PRIMARY KEY, j INT);
227
CREATE TABLE t2 (a INT, b INT);
228
CREATE TABLE t3 (a INT, c INT);
229
INSERT INTO t1 SELECT 1, a FROM t2 NATURAL JOIN t3
230
ON DUPLICATE KEY UPDATE j= a;
232
CREATE TABLE t1 (i INT PRIMARY KEY, j INT);
233
CREATE TABLE t2 (a INT);
234
INSERT INTO t1 VALUES (1, 1);
235
INSERT INTO t2 VALUES (1), (3);
236
INSERT INTO t1 SELECT 1, COUNT(*) FROM t2 ON DUPLICATE KEY UPDATE j= a;
237
ERROR 42S22: Unknown column 'a' in 'field list'
239
SET SQL_MODE = 'TRADITIONAL';
240
CREATE TABLE t1 (a INT PRIMARY KEY, b INT NOT NULL);
241
INSERT INTO t1 (a) VALUES (1);
242
ERROR HY000: Field 'b' doesn't have a default value
243
INSERT INTO t1 (a) VALUES (1) ON DUPLICATE KEY UPDATE a = b;
244
ERROR HY000: Field 'b' doesn't have a default value
245
INSERT INTO t1 (a) VALUES (1) ON DUPLICATE KEY UPDATE b = b;
246
ERROR HY000: Field 'b' doesn't have a default value
250
CREATE TABLE t1 (f1 INT AUTO_INCREMENT PRIMARY KEY,
251
f2 VARCHAR(5) NOT NULL UNIQUE);
252
INSERT t1 (f2) VALUES ('test') ON DUPLICATE KEY UPDATE f1 = LAST_INSERT_ID(f1);
253
SELECT LAST_INSERT_ID();
256
INSERT t1 (f2) VALUES ('test') ON DUPLICATE KEY UPDATE f1 = LAST_INSERT_ID(f1);
257
SELECT LAST_INSERT_ID();
261
SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
263
`id` int(11) PRIMARY KEY auto_increment,
264
`f1` varchar(10) NOT NULL UNIQUE
266
INSERT IGNORE INTO t1 (f1) VALUES ("test1")
267
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
268
INSERT IGNORE INTO t1 (f1) VALUES ("test1")
269
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
270
SELECT LAST_INSERT_ID();
276
INSERT IGNORE INTO t1 (f1) VALUES ("test2")
277
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
282
INSERT IGNORE INTO t1 (f1) VALUES ("test2")
283
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
284
SELECT LAST_INSERT_ID();
291
INSERT IGNORE INTO t1 (f1) VALUES ("test3")
292
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
293
SELECT LAST_INSERT_ID();
303
`id` int(11) PRIMARY KEY auto_increment,
304
`f1` varchar(10) NOT NULL UNIQUE
306
INSERT IGNORE INTO t1 (f1) VALUES ("test1")
307
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
308
SELECT LAST_INSERT_ID();
314
INSERT IGNORE INTO t1 (f1) VALUES ("test1"),("test4")
315
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id);
316
SELECT LAST_INSERT_ID();
325
`id` int(11) PRIMARY KEY auto_increment,
326
`f1` varchar(10) NOT NULL UNIQUE,
327
tim1 timestamp default '2003-01-01 00:00:00' on update current_timestamp
329
INSERT INTO t1 (f1) VALUES ("test1");
330
SELECT id, f1 FROM t1;
333
REPLACE INTO t1 VALUES (0,"test1",null);
334
SELECT id, f1 FROM t1;
340
id INT AUTO_INCREMENT PRIMARY KEY,
341
c1 CHAR(1) UNIQUE KEY,
344
INSERT INTO t1 (c1) VALUES ('A'), ('B'), ('C');
350
INSERT INTO t1 (c1) VALUES ('A'), ('X'), ('Y'), ('Z')
351
ON DUPLICATE KEY UPDATE cnt=cnt+1;
362
id INT AUTO_INCREMENT PRIMARY KEY,
366
INSERT INTO t1 (id,c1) VALUES (1,10);
370
CREATE TABLE t2 (id INT, c1 INT);
371
INSERT INTO t2 VALUES (1,NULL), (2,2);
372
INSERT INTO t1 (id,c1) SELECT 1,NULL
373
ON DUPLICATE KEY UPDATE c1=NULL;
374
ERROR 23000: Column 'c1' cannot be null
378
INSERT IGNORE INTO t1 (id,c1) SELECT 1,NULL
379
ON DUPLICATE KEY UPDATE c1=NULL, cnt=cnt+1;
381
Warning 1048 Column 'c1' cannot be null
382
Error 1048 Column 'c1' cannot be null
386
INSERT IGNORE INTO t1 (id,c1) SELECT * FROM t2
387
ON DUPLICATE KEY UPDATE c1=NULL, cnt=cnt+1;
389
Warning 1048 Column 'c1' cannot be null
390
Error 1048 Column 'c1' cannot be null
396
create table t1(f1 int primary key,
397
f2 timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP);
398
insert into t1(f1) values(1);
399
select @stamp1:=f2 from t1;
402
insert into t1(f1) values(1) on duplicate key update f1=1;
403
select @stamp2:=f2 from t1;
406
select if( @stamp1 = @stamp2, "correct", "wrong");
407
if( @stamp1 = @stamp2, "correct", "wrong")