INSERT
官方文档:https://dev.mysql.com/doc/refman/5.7/en/insert.html
mysql> insert into t1 values(1); -- 插入一个值
Query OK, 1 row affected (0.03 sec)
mysql> insert into t1 values(2),(3),(-1); -- 插入多个值,MySQL独有
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into t1 select 8; -- insert XXX select XXX 语法,MySQ独有
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> create table t3 (a int, b int); -- 有多个列
Query OK, 0 rows affected (0.15 sec)
mysql> insert into t3 select 8; -- 没有指定列,报错
ERROR 1136 (21S01): Column count does not match value count at row 1
mysql> insert into t3(a) select 8; -- 指定列a
Query OK, 1 row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into t3 select 8, 9; -- 不指定列,但是插入值匹配列的个数和类型
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from t3;
+------+------+
| a | b |
+------+------+
| 8 | NULL |
| 8 | 9 |
+------+------+
2 rows in set (0.00 sec)
mysql> insert into t3(b) select a from t2; -- 从t2表中查询数据并插入到t3(a)中,注意指定列
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from t3;
+------+------+
| a | b |
+------+------+
| 8 | NULL |
| 8 | 9 |
| NULL | 5 |
| NULL | 4 |
| NULL | 3 |
+------+------+
5 rows in set (0.00 sec)
--
-- 如果想快速增长表格中的数据,可以使用如下方法,使得数据成倍增长
--
mysql> insert into t3 select * from t3;
Query OK, 5 rows affected (0.03 sec) -- 插入了5列
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from t3;
+------+------+
| a | b |
+------+------+
| 8 | NULL |
| 8 | 9 |
| NULL | 5 |
| NULL | 4 |
| NULL | 3 |
| 8 | NULL |
| 8 | 9 |
| NULL | 5 |
| NULL | 4 |
| NULL | 3 |
+------+------+
10 rows in set (0.00 sec)
mysql> insert into t3 select * from t3;
Query OK, 10 rows affected (0.03 sec) -- 插入了10列,成倍增长
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from t3;
+------+------+
| a | b |
+------+------+
| 8 | NULL |
| 8 | 9 |
| NULL | 5 |
| NULL | 4 |
| NULL | 3 |
| 8 | NULL |
| 8 | 9 |
| NULL | 5 |
| NULL | 4 |
| NULL | 3 |
| 8 | NULL |
| 8 | 9 |
| NULL | 5 |
| NULL | 4 |
| NULL | 3 |
| 8 | NULL |
| 8 | 9 |
| NULL | 5 |
| NULL | 4 |
| NULL | 3 |
+------+------+
20 rows in set (0.00 sec)
UPDATE
官方文档:https://dev.mysql.com/doc/refman/5.7/en/update.html
mysql> insert into t3 select 1,2;
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from t3;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
+------+------+
1 row in set (0.00 sec)
mysql> update t3 set a=10 where a=1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t3;
+------+------+
| a | b |
+------+------+
| 10 | 2 |
+------+------+
1 row in set (0.00 sec)
--
-- 关联后更新
--
mysql> select * from t1;
+------+
| a |
+------+
| 10 |
| 4 | -- 和t2中的4相等
| 1 |
| 2 |
| 3 | -- 和t2中的3相等
| -1 |
| 8 |
+------+
7 rows in set (0.00 sec)
mysql> select * from t2;
+------+
| a |
+------+
| 5 |
| 4 | -- 和t1中的4相等
| 3 | -- 和t1中的3相等
+------+
3 rows in set (0.00 sec)
mysql> update t1 join t2 on t1.a = t2.a set t1.a=100; -- 先得到t1.a=t2.a的结果集
-- 然后将结果集中的t1.a设置为100
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from t1;
+------+
| a |
+------+
| 10 |
| 100 | -- 该行被更新成100
| 1 |
| 2 |
| 100 | -- 该行被更新成100
| -1 |
| 8 |
+------+
7 rows in set (0.00 sec)
DELETE
官方文档:https://dev.mysql.com/doc/refman/5.7/en/delete.html
mysql> delete from t3 where a is null; -- 根据过滤条件删除
Query OK, 12 rows affected (0.03 sec)
mysql> select * from t3;
+------+------+
| a | b |
+------+------+
| 8 | NULL |
| 8 | 9 |
| 8 | NULL |
| 8 | 9 |
| 8 | NULL |
| 8 | 9 |
| 8 | NULL |
| 8 | 9 |
+------+------+
8 rows in set (0.00 sec)
mysql> delete from t3; -- 删除整个表
Query OK, 8 rows affected (0.03 sec)
mysql> select * from t3;
Empty set (0.00 sec)
REPLACE
官方文档:https://dev.mysql.com/doc/refman/5.7/en/replace.html
mysql> create table t4(a int primary key auto_increment, b int);
Query OK, 0 rows affected (0.15 sec)
mysql> insert into t4 values(NULL, 10);
Query OK, 1 row affected (0.02 sec)
mysql> insert into t4 values(NULL, 11);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t4 values(NULL, 12);
Query OK, 1 row affected (0.03 sec)
mysql> select * from t4;
+---+------+
| a | b |
+---+------+
| 1 | 10 |
| 2 | 11 |
| 3 | 12 |
+---+------+
3 rows in set (0.00 sec)
mysql> insert into t4 values(1, 100); -- 报错,说存在重复的主键记录 "1"
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> replace into t4 values(1, 100); -- 替换该主键对应的值
Query OK, 2 rows affected (0.03 sec) -- 两行记录受到影响
mysql> select * from t4;
+---+------+
| a | b |
+---+------+
| 1 | 100 | -- 已经被替换
| 2 | 11 |
| 3 | 12 |
+---+------+
3 rows in set (0.00 sec)
-----
-- replace的原理是:先delete,再insert
-----
mysql> replace into t4 values(5, 50); -- 没有替换对象时,类似插入效果
Query OK, 1 row affected (0.03 sec) -- 只影响1行
mysql> select * from t4;
+---+------+
| a | b |
+---+------+
| 1 | 100 |
| 2 | 11 |
| 3 | 12 |
| 5 | 50 | -- 插入了1行
+---+------+
4 rows in set (0.00 sec)
--
-- replace原理更明显的例子
--
mysql> create table t6
-> (a int primary key,
-> b int auto_increment, -- b是auto_increment的int型数据
-> c int, key(b));
Query OK, 0 rows affected (0.15 sec)
mysql> insert into t6 values(10, NULL, 100),(20,NULL,200); -- b自增长
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t6;
+----+---+------+
| a | b | c |
+----+---+------+
| 10 | 1 | 100 | -- b为1
| 20 | 2 | 200 | -- b为2
+----+---+------+
2 rows in set (0.00 sec)
mysql> replace into t6 values(10,NULL,150); -- 将a=10的替换掉
Query OK, 2 rows affected (0.03 sec)
mysql> select * from t6;
+----+---+------+
| a | b | c |
+----+---+------+
| 10 | 3 | 150 | -- 替换后b从1变成了3,说明是先删除,再插入
| 20 | 2 | 200 |
+----+---+------+
2 rows in set (0.00 sec)
-----
--
-- insert on duplicate效果和replace类似
--
mysql> insert into t4 values(1,1); -- 插入报错,存在key为1的记录
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into t4 values(1,1) on duplicate key update b=1; -- 带上on duplicate参数
-- 非SQL标准,不推荐
Query OK, 2 rows affected (0.03 sec)
mysql> select * from t4;
+---+------+
| a | b |
+---+------+
| 1 | 1 | -- 该行的b列从100被替换成1
| 2 | 11 |
| 3 | 12 |
| 5 | 50 |
+---+------+
--
-- insert ignore
--
mysql> insert ignore into t4 values(1,1); -- 忽略重复的错误
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+---------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------+
| Warning | 1062 | Duplicate entry '1' for key 'PRIMARY' |
+---------+------+---------------------------------------+
1 row in set (0.00 sec)
其他知识点
更新有关系的值
mysql> create table t5 (a int, b int);
Query OK, 0 rows affected (0.14 sec)
mysql> insert into t5 values(1,1);
Query OK, 1 row affected (0.03 sec)
mysql> select * from t5;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
mysql> update t5 set a=a+1, b=a where a=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t5;
+------+------+
| a | b |
+------+------+
| 2 | 2 | -- SQL Server和Oracle中得到的值是2, 1
+------+------+
1 row in set (0.00 se
显示行号(RowNumber)
--
-- 方法一
--
mysql> use employees ;
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> set @rn:=0; -- 产生 SESSION(会话)级别的变量
Query OK, 0 rows affected (0.00 sec)
mysql> select @rn:=@rn+1 as rownumber, emp_no, gender from employees limit 10; -- := 是赋值的意思
+-----------+--------+--------+
| rownumber | emp_no | gender |
+-----------+--------+--------+
| 11 | 10001 | M |
| 12 | 10002 | F |
| 13 | 10003 | M |
| 14 | 10004 | M |
| 15 | 10005 | M |
| 16 | 10006 | F |
| 17 | 10007 | F |
| 18 | 10008 | M |
| 19 | 10009 | F |
| 20 | 10010 | F |
+-----------+--------+--------+
10 rows in set (0.00 sec)
--
-- 方法二 (推荐)
--
mysql> select @rn1:=@rn1+1 as rownumber, emp_no, gender from employees, (select @rn1:=0) as a limit 10;
+-----------+--------+--------+
| rownumber | emp_no | gender |
+-----------+--------+--------+
| 1 | 10001 | M |
| 2 | 10002 | F |
| 3 | 10003 | M |
| 4 | 10004 | M |
| 5 | 10005 | M |
| 6 | 10006 | F |
| 7 | 10007 | F |
| 8 | 10008 | M |
| 9 | 10009 | F |
| 10 | 10010 | F |
+-----------+--------+--------+
10 rows in set (0.00 sec)
-- MySQL 自定义变量,根据每一记录进行变化的
mysql> select @rn1:=0;
+---------+
| @rn1:=0 |
+---------+
| 0 | -- 只有一行记录
+---------+
1 row in set (0.00 sec)
-- 相当于把employees和(select @rn1:=0)做了笛卡尔积,然后使用@rn1:=@rn + 1,根据每行进行累加
--
-- ":=" 和 "="
--
mysql> set @a:=1; -- 赋值为1
Query OK, 0 rows affected (0.00 sec)
mysql> select @a;
+------+
| @a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> set @a:=10; -- 赋值为10
Query OK, 0 rows affected (0.00 sec)
mysql> select @a;
+------+
| @a |
+------+
| 10 |
+------+
1 row in set (0.00 sec)
mysql> select @a=9; -- 进行比较
+------+
| @a=9 |
+------+
| 0 | -- 返回为False
+------+
1 row in set (0.00 sec)
--
-- 作业:通过子查询或者其他方式,计算出employees的行号
--
作者:殷建卫 链接:https://www.yuque.com/yinjianwei/vyrvkf/mb1gfl 来源:殷建卫 - 架构笔记 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。