SQL基础语法—insert语句
1 insert语句
insert语句用于插入数据到表中,其基本语法有以下三种:
Syntax:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
{VALUES | VALUE} (value_list) [, (value_list)] ...
[ON DUPLICATE KEY UPDATE assignment_list]
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
SET assignment_list
[ON DUPLICATE KEY UPDATE assignment_list]
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
SELECT ...
[ON DUPLICATE KEY UPDATE assignment_list]
-
其中
select...values
和insert...set
两种语句都是将指定的数据插入到现成的表中,而insert...select
语句是将另外表中数据查出来并插入到现成的表中 -
partiion
子句代表可以将数据插入到指定的表分区中 -
table_name
代表将数据插入到的目标表 -
col_name
代表要插入指定数据的目标表列,如果是多列则用逗号隔开,如果目标表中的某些列没有在insert语句中指定,则这些列会插入默认值,当然可以使用default显式指定插入默认值 -
values
中除了可以指定确定的数值之外,开可以使用表达式expr
insert into tbl_name (col1,col2) values(15,col1*2) ##正确
insert into tbl_name (col1,col2) values(col1*2,15) ##错误
案例演示:
## 修改sid字段,添加auto_increment属性
mysql> alter table students modify sid int auto_increment;
Query OK, 2 rows affected (0.23 sec)
Records: 2 Duplicates: 0 Warnings: 0
## 修改gender字段,添加默认值0
mysql> alter table students modify gender int default 0;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
## 向students表中插入一条数据
mysql> insert into students(sname) values("bbb");
Query OK, 1 row affected (0.04 sec)
mysql> insert into students(sid,sname) values(5,"ccc");
Query OK, 1 row affected (0.02 sec)
mysql> insert into students(sname) values("ddd");
Query OK, 1 row affected (0.07 sec)
mysql> insert into students(sid,sname) values(4,"dd");
Query OK, 1 row affected (0.10 sec)
mysql> insert into students values(3*3,'fff',1);
Query OK, 1 row affected (0.09 sec)
mysql> select * from students;
+-----+--------+--------+
| sid | sname | gender |
+-----+--------+--------+
| 1 | viktor | 1 |
| 2 | aaa | NULL |
| 3 | bbb | 0 |
| 4 | dd | 0 |
| 5 | ccc | 0 |
| 6 | ddd | 0 |
| 9 | fff | 1 |
+-----+--------+--------+
7 rows in set (0.00 sec)
inset...values
语句不光可以插入一条数据,也可以插入多条数据
insert into tbl_name(a,b,c) values(1,2,3)(4,5,6)
insert into students values(7,'abc'),(8,'bcd')
-
low_priority
关键词代表如果有其他链接正在读取目标表数据,则此insert语句需要等待读取完成 -
low_priority
和high_priority
关键词仅在MylSAM
,MEMORY
,MERGE
三种存储引擎下才生效 -
ignore
关键词代表insert语句如果违反主键和伪意见的约束条件,则不报错而只产生警告信息,违反的行被丢弃,而不是整个语句回退;在数据类型转换有问题时如果有ignore则只产生警告信息,而不是语句回退
mysql> select * from students;
+-----+--------+--------+
| sid | sname | gender |
+-----+--------+--------+
| 1 | viktor | 1 |
| 2 | aaa | NULL |
| 3 | bbb | 0 |
| 4 | dd | 0 |
| 5 | ccc | 0 |
| 6 | ddd | 0 |
| 9 | fff | 1 |
+-----+--------+--------+
7 rows in set (0.00 sec)
mysql> insert ignore into students values(1,'aa',1),(7,'cc',0);
Query OK, 1 row affected, 1 warning (0.10 sec)
Records: 2 Duplicates: 1 Warnings: 1
2 insert... select语句
用于从另外的表中查出记录并插入到目标表中
insert into tbl_temp2(fld_id)
select tbl_temp1.fld_order_id
from tbl_temp1 where tbl_temp1.fld_order_id > 100;
当目标表和select语句中的表相同时,则会先将select语句的结果存放在临时表中,再插入到目标表中(注意执行顺序)
mysql> desc student2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid | int(11) | NO | | NULL | |
| sname | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc students;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| sid | int(11) | NO | PRI | NULL | auto_increment |
| sname | varchar(20) | NO | UNI | NULL | |
| gender | int(11) | YES | | 0 | |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
## concat用来合并两个字段
mysql> insert into student2(sid,sname) select sid,concat(sid,sname) from students where sid=1;
Query OK, 1 row affected (0.07 sec)
Records: 1 Duplicates: 0 Warnings: 0
## 当两个表中的字段不匹配时,插入会出现错误
mysql> insert into student2 select * from students;
ERROR 1136 (21S01): Column count doesn't match value count at row 1
##选定两个字段进行插入
mysql> insert into student2 select sid,sname from students;
Query OK, 8 rows affected (0.07 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from student2;
+-----+---------+
| sid | sname |
+-----+---------+
| 1 | 1viktor |
| 2 | aaa |
| 3 | bbb |
| 7 | cc |
| 5 | ccc |
| 4 | dd |
| 6 | ddd |
| 9 | fff |
| 1 | viktor |
+-----+---------+
9 rows in set (0.00 sec)
3 insert on duplicate key update语句
当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;
简而言之,该语句可以解决插入的数据与表之间的冲突。
案例演示:
mysql> insert into student2(sid,sname) select sid,sname from students where sid<3;
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student2;
+-----+--------+
| sid | sname |
+-----+--------+
| 1 | viktor |
| 2 | aaa |
+-----+--------+
2 rows in set (0.00 sec)
## 创建sname字段的unique索引
mysql> create unique index idx_1 on student2(sname);
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid | int(11) | NO | | NULL | |
| sname | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> insert into student2(sid,sname) select sid,sname from students where sid=1;
ERROR 1062 (23000): Duplicate entry 'viktor' for key 'idx_1'
mysql> insert into student2(sid,sname) select sid,sname from students where sid=1 on duplicate key update sname='dabric';
Query OK, 2 rows affected (0.01 sec)
Records: 1 Duplicates: 1 Warnings: 0
mysql> select * from student2;
+-----+--------+
| sid | sname |
+-----+--------+
| 1 | dabric |
| 2 | aaa |
+-----+--------+
2 rows in set (0.00 sec)
mysql> insert into student values(1,'bb',0) on duplicate key update sname='b'sex=1;
4 练习
- 将如下数据插入到dept表中
1,'computer science'; 2,'education'; 4,'accounting'
- 根据
create table...like
语句创建teacher_backup表,并插入如下数据:
1,'susan'; 1; 2,'ruth'; 4; 3,'vivian', 4
- 将teacher_backup表的数据通过
insert...select
语句插入到teacher表中