mysql> show databases; +--------------------+ | Database | +--------------------+ | course | | information_schema | | monitor | | mysql | | performance_schema | | sys | +--------------------+ 6 rows in set (0.03 sec) mysql> use course; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +------------------+ | Tables_in_course | +------------------+ | course | | dept | | students | | teacher | +------------------+ 4 rows in set (0.01 sec) mysql> desc dept; +-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | dept_name | varchar(64) | YES | | NULL | | +-----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec) mysql> insert into dept(dept_name,id) values('a',1); Query OK, 1 row affected (0.04 sec) mysql> delete from dept; Query OK, 1 row affected (0.06 sec) mysql> insert into dept(dept_name,id) values("a",1); Query OK, 1 row affected (0.02 sec) mysql> select * from dept; +----+-----------+ | id | dept_name | +----+-----------+ | 1 | a | +----+-----------+ 1 row in set (0.00 sec) mysql> create table dept_bak(id int primary key auto_increment,dept_name varchar(64)); Query OK, 0 rows affected (0.07 sec) mysql> insert into dept_bak values(1,'a'),(2,'b'); Query OK, 2 rows affected (0.04 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from dept_bak; +----+-----------+ | id | dept_name | +----+-----------+ | 1 | a | | 2 | b | +----+-----------+ 2 rows in set (0.00 sec) mysql> select * from dept; +----+-----------+ | id | dept_name | +----+-----------+ | 1 | a | +----+-----------+ 1 row in set (0.00 sec) mysql> delete from dept; Query OK, 1 row affected (0.08 sec) mysql> insert into dept select * from dept_bak; Query OK, 2 rows affected (0.05 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from dept; +----+-----------+ | id | dept_name | +----+-----------+ | 1 | a | | 2 | b | +----+-----------+ 2 rows in set (0.00 sec) mysql> desc dept_bak; +-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | dept_name | varchar(64) | YES | | NULL | | +-----------+-------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec) mysql> alter table dept_bak add name varchar(10); Query OK, 0 rows affected (0.13 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> select * from dept_bak; +----+-----------+------+ | id | dept_name | name | +----+-----------+------+ | 1 | a | NULL | | 2 | b | NULL | +----+-----------+------+ 2 rows in set (0.00 sec) mysql> select * from dept; +----+-----------+ | id | dept_name | +----+-----------+ | 1 | a | | 2 | b | +----+-----------+ 2 rows in set (0.00 sec) mysql> delete from dept; Query OK, 2 rows affected (0.09 sec) mysql> insert into dept select id,dept_name from dept_bak; Query OK, 2 rows affected (0.06 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> delete from dept; Query OK, 4 rows affected (0.03 sec) mysql> insert into dept(dept_name) select concat(id,dept_name) from dept_bak; Query OK, 2 rows affected (0.03 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from dept; +----+-----------+ | id | dept_name | +----+-----------+ | 6 | 1a | | 7 | 2b | +----+-----------+ 2 rows in set (0.01 sec)
• 当insert语句中使用on duplicate key update子句时,如果碰到当前插入的数据违反主键或唯一键的唯一性约束,则Insert会转变成update语句修改对应的已经存在表中的这条数据。比如如果a字段有唯一性约束且已经含有1这条记录,则以下两条语句的执行结果相同
• INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; • UPDATE table SET c=c+1 WHERE a=1;
• On duplicate key update子句后面可以跟多个修改,用逗号隔开
• 上述例子中如果b字段也有唯一性约束,则与此语句的执行结果相同,但一般应该避免出现对应多条的情况
• UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
• mysql> create table students2(sid int primary key,sname varchar(20),sex int); • Insert into students2 values(1,’aaa’,1); ##插入成功 • Insert into students2 values(1,’bbb’,0); ##插入失败 • mysql> insert into students2 values(1,'bbb',0); • ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY‘ • insert into students2 values(1,‘bbb’,0) on duplicate key update sname=‘bbb’; ##执行成功 • +-----+-------+------+ • | sid | sname | sex | • +-----+-------+------+ • | 1 | bbb | 1 | • insert into students2 values(1,‘ccc’,0) on duplicate key update sname=‘ccc’,sex=0; ##执行成功
mysql> create table students2(sid int primary key,sname varchar(20),sex int); Query OK, 0 rows affected (0.11 sec) mysql> Insert into students2 values(1,'aaa',1); Query OK, 1 row affected (0.19 sec) mysql> desc students2 -> ; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | sid | int(11) | NO | PRI | NULL | | | sname | varchar(20) | YES | | NULL | | | sex | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> Insert into students2 values(1,'bbb',0); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> insert into students2 values(1,'bbb',0) on duplicate key update sname='bbb'; Query OK, 2 rows affected (0.05 sec) mysql> select * from students2; +-----+-------+------+ | sid | sname | sex | +-----+-------+------+ | 1 | bbb | 1 | +-----+-------+------+ 1 row in set (0.00 sec)