INSERT

官方文档:https://dev.mysql.com/doc/refman/5.7/en/insert.html

  1. mysql> insert into t1 values(1); -- 插入一个值
  2. Query OK, 1 row affected (0.03 sec)
  3. mysql> insert into t1 values(2),(3),(-1); -- 插入多个值,MySQL独有
  4. Query OK, 3 rows affected (0.03 sec)
  5. Records: 3 Duplicates: 0 Warnings: 0
  6. mysql> insert into t1 select 8; -- insert XXX select XXX 语法,MySQ独有
  7. Query OK, 1 row affected (0.02 sec)
  8. Records: 1 Duplicates: 0 Warnings: 0
  9. mysql> create table t3 (a int, b int); -- 有多个列
  10. Query OK, 0 rows affected (0.15 sec)
  11. mysql> insert into t3 select 8; -- 没有指定列,报错
  12. ERROR 1136 (21S01): Column count does not match value count at row 1
  13. mysql> insert into t3(a) select 8; -- 指定列a
  14. Query OK, 1 row affected (0.04 sec)
  15. Records: 1 Duplicates: 0 Warnings: 0
  16. mysql> insert into t3 select 8, 9; -- 不指定列,但是插入值匹配列的个数和类型
  17. Query OK, 1 row affected (0.03 sec)
  18. Records: 1 Duplicates: 0 Warnings: 0
  19. mysql> select * from t3;
  20. +------+------+
  21. | a | b |
  22. +------+------+
  23. | 8 | NULL |
  24. | 8 | 9 |
  25. +------+------+
  26. 2 rows in set (0.00 sec)
  27. mysql> insert into t3(b) select a from t2; -- t2表中查询数据并插入到t3(a)中,注意指定列
  28. Query OK, 3 rows affected (0.03 sec)
  29. Records: 3 Duplicates: 0 Warnings: 0
  30. mysql> select * from t3;
  31. +------+------+
  32. | a | b |
  33. +------+------+
  34. | 8 | NULL |
  35. | 8 | 9 |
  36. | NULL | 5 |
  37. | NULL | 4 |
  38. | NULL | 3 |
  39. +------+------+
  40. 5 rows in set (0.00 sec)
  41. --
  42. -- 如果想快速增长表格中的数据,可以使用如下方法,使得数据成倍增长
  43. --
  44. mysql> insert into t3 select * from t3;
  45. Query OK, 5 rows affected (0.03 sec) -- 插入了5
  46. Records: 5 Duplicates: 0 Warnings: 0
  47. mysql> select * from t3;
  48. +------+------+
  49. | a | b |
  50. +------+------+
  51. | 8 | NULL |
  52. | 8 | 9 |
  53. | NULL | 5 |
  54. | NULL | 4 |
  55. | NULL | 3 |
  56. | 8 | NULL |
  57. | 8 | 9 |
  58. | NULL | 5 |
  59. | NULL | 4 |
  60. | NULL | 3 |
  61. +------+------+
  62. 10 rows in set (0.00 sec)
  63. mysql> insert into t3 select * from t3;
  64. Query OK, 10 rows affected (0.03 sec) -- 插入了10列,成倍增长
  65. Records: 10 Duplicates: 0 Warnings: 0
  66. mysql> select * from t3;
  67. +------+------+
  68. | a | b |
  69. +------+------+
  70. | 8 | NULL |
  71. | 8 | 9 |
  72. | NULL | 5 |
  73. | NULL | 4 |
  74. | NULL | 3 |
  75. | 8 | NULL |
  76. | 8 | 9 |
  77. | NULL | 5 |
  78. | NULL | 4 |
  79. | NULL | 3 |
  80. | 8 | NULL |
  81. | 8 | 9 |
  82. | NULL | 5 |
  83. | NULL | 4 |
  84. | NULL | 3 |
  85. | 8 | NULL |
  86. | 8 | 9 |
  87. | NULL | 5 |
  88. | NULL | 4 |
  89. | NULL | 3 |
  90. +------+------+
  91. 20 rows in set (0.00 sec)

UPDATE

官方文档:https://dev.mysql.com/doc/refman/5.7/en/update.html

  1. mysql> insert into t3 select 1,2;
  2. Query OK, 1 row affected (0.03 sec)
  3. Records: 1 Duplicates: 0 Warnings: 0
  4. mysql> select * from t3;
  5. +------+------+
  6. | a | b |
  7. +------+------+
  8. | 1 | 2 |
  9. +------+------+
  10. 1 row in set (0.00 sec)
  11. mysql> update t3 set a=10 where a=1;
  12. Query OK, 1 row affected (0.03 sec)
  13. Rows matched: 1 Changed: 1 Warnings: 0
  14. mysql> select * from t3;
  15. +------+------+
  16. | a | b |
  17. +------+------+
  18. | 10 | 2 |
  19. +------+------+
  20. 1 row in set (0.00 sec)
  21. --
  22. -- 关联后更新
  23. --
  24. mysql> select * from t1;
  25. +------+
  26. | a |
  27. +------+
  28. | 10 |
  29. | 4 | -- t2中的4相等
  30. | 1 |
  31. | 2 |
  32. | 3 | -- t2中的3相等
  33. | -1 |
  34. | 8 |
  35. +------+
  36. 7 rows in set (0.00 sec)
  37. mysql> select * from t2;
  38. +------+
  39. | a |
  40. +------+
  41. | 5 |
  42. | 4 | -- t1中的4相等
  43. | 3 | -- t1中的3相等
  44. +------+
  45. 3 rows in set (0.00 sec)
  46. mysql> update t1 join t2 on t1.a = t2.a set t1.a=100; -- 先得到t1.a=t2.a的结果集
  47. -- 然后将结果集中的t1.a设置为100
  48. Query OK, 2 rows affected (0.03 sec)
  49. Rows matched: 2 Changed: 2 Warnings: 0
  50. mysql> select * from t1;
  51. +------+
  52. | a |
  53. +------+
  54. | 10 |
  55. | 100 | -- 该行被更新成100
  56. | 1 |
  57. | 2 |
  58. | 100 | -- 该行被更新成100
  59. | -1 |
  60. | 8 |
  61. +------+
  62. 7 rows in set (0.00 sec)

DELETE

官方文档:https://dev.mysql.com/doc/refman/5.7/en/delete.html

  1. mysql> delete from t3 where a is null; -- 根据过滤条件删除
  2. Query OK, 12 rows affected (0.03 sec)
  3. mysql> select * from t3;
  4. +------+------+
  5. | a | b |
  6. +------+------+
  7. | 8 | NULL |
  8. | 8 | 9 |
  9. | 8 | NULL |
  10. | 8 | 9 |
  11. | 8 | NULL |
  12. | 8 | 9 |
  13. | 8 | NULL |
  14. | 8 | 9 |
  15. +------+------+
  16. 8 rows in set (0.00 sec)
  17. mysql> delete from t3; -- 删除整个表
  18. Query OK, 8 rows affected (0.03 sec)
  19. mysql> select * from t3;
  20. Empty set (0.00 sec)

REPLACE

官方文档:https://dev.mysql.com/doc/refman/5.7/en/replace.html

  1. mysql> create table t4(a int primary key auto_increment, b int);
  2. Query OK, 0 rows affected (0.15 sec)
  3. mysql> insert into t4 values(NULL, 10);
  4. Query OK, 1 row affected (0.02 sec)
  5. mysql> insert into t4 values(NULL, 11);
  6. Query OK, 1 row affected (0.03 sec)
  7. mysql> insert into t4 values(NULL, 12);
  8. Query OK, 1 row affected (0.03 sec)
  9. mysql> select * from t4;
  10. +---+------+
  11. | a | b |
  12. +---+------+
  13. | 1 | 10 |
  14. | 2 | 11 |
  15. | 3 | 12 |
  16. +---+------+
  17. 3 rows in set (0.00 sec)
  18. mysql> insert into t4 values(1, 100); -- 报错,说存在重复的主键记录 "1"
  19. ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
  20. mysql> replace into t4 values(1, 100); -- 替换该主键对应的值
  21. Query OK, 2 rows affected (0.03 sec) -- 两行记录受到影响
  22. mysql> select * from t4;
  23. +---+------+
  24. | a | b |
  25. +---+------+
  26. | 1 | 100 | -- 已经被替换
  27. | 2 | 11 |
  28. | 3 | 12 |
  29. +---+------+
  30. 3 rows in set (0.00 sec)
  31. -----
  32. -- replace的原理是:先delete,再insert
  33. -----
  34. mysql> replace into t4 values(5, 50); -- 没有替换对象时,类似插入效果
  35. Query OK, 1 row affected (0.03 sec) -- 只影响1
  36. mysql> select * from t4;
  37. +---+------+
  38. | a | b |
  39. +---+------+
  40. | 1 | 100 |
  41. | 2 | 11 |
  42. | 3 | 12 |
  43. | 5 | 50 | -- 插入了1
  44. +---+------+
  45. 4 rows in set (0.00 sec)
  46. --
  47. -- replace原理更明显的例子
  48. --
  49. mysql> create table t6
  50. -> (a int primary key,
  51. -> b int auto_increment, -- bauto_incrementint型数据
  52. -> c int, key(b));
  53. Query OK, 0 rows affected (0.15 sec)
  54. mysql> insert into t6 values(10, NULL, 100),(20,NULL,200); -- b自增长
  55. Query OK, 2 rows affected (0.02 sec)
  56. Records: 2 Duplicates: 0 Warnings: 0
  57. mysql> select * from t6;
  58. +----+---+------+
  59. | a | b | c |
  60. +----+---+------+
  61. | 10 | 1 | 100 | -- b1
  62. | 20 | 2 | 200 | -- b2
  63. +----+---+------+
  64. 2 rows in set (0.00 sec)
  65. mysql> replace into t6 values(10,NULL,150); -- a=10的替换掉
  66. Query OK, 2 rows affected (0.03 sec)
  67. mysql> select * from t6;
  68. +----+---+------+
  69. | a | b | c |
  70. +----+---+------+
  71. | 10 | 3 | 150 | -- 替换后b1变成了3,说明是先删除,再插入
  72. | 20 | 2 | 200 |
  73. +----+---+------+
  74. 2 rows in set (0.00 sec)
  75. -----
  76. --
  77. -- insert on duplicate效果和replace类似
  78. --
  79. mysql> insert into t4 values(1,1); -- 插入报错,存在key1的记录
  80. ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
  81. mysql> insert into t4 values(1,1) on duplicate key update b=1; -- 带上on duplicate参数
  82. -- SQL标准,不推荐
  83. Query OK, 2 rows affected (0.03 sec)
  84. mysql> select * from t4;
  85. +---+------+
  86. | a | b |
  87. +---+------+
  88. | 1 | 1 | -- 该行的b列从100被替换成1
  89. | 2 | 11 |
  90. | 3 | 12 |
  91. | 5 | 50 |
  92. +---+------+
  93. --
  94. -- insert ignore
  95. --
  96. mysql> insert ignore into t4 values(1,1); -- 忽略重复的错误
  97. Query OK, 0 rows affected, 1 warning (0.00 sec)
  98. mysql> show warnings;
  99. +---------+------+---------------------------------------+
  100. | Level | Code | Message |
  101. +---------+------+---------------------------------------+
  102. | Warning | 1062 | Duplicate entry '1' for key 'PRIMARY' |
  103. +---------+------+---------------------------------------+
  104. 1 row in set (0.00 sec)

其他知识点

更新有关系的值

  1. mysql> create table t5 (a int, b int);
  2. Query OK, 0 rows affected (0.14 sec)
  3. mysql> insert into t5 values(1,1);
  4. Query OK, 1 row affected (0.03 sec)
  5. mysql> select * from t5;
  6. +------+------+
  7. | a | b |
  8. +------+------+
  9. | 1 | 1 |
  10. +------+------+
  11. 1 row in set (0.00 sec)
  12. mysql> update t5 set a=a+1, b=a where a=1;
  13. Query OK, 1 row affected (0.02 sec)
  14. Rows matched: 1 Changed: 1 Warnings: 0
  15. mysql> select * from t5;
  16. +------+------+
  17. | a | b |
  18. +------+------+
  19. | 2 | 2 | -- SQL ServerOracle中得到的值是2, 1
  20. +------+------+
  21. 1 row in set (0.00 se

显示行号(RowNumber)

  1. --
  2. -- 方法一
  3. --
  4. mysql> use employees ;
  5. Reading table information for completion of table and column names
  6. You can turn off this feature to get a quicker startup with -A
  7. Database changed
  8. mysql> set @rn:=0; -- 产生 SESSION(会话)级别的变量
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql> select @rn:=@rn+1 as rownumber, emp_no, gender from employees limit 10; -- := 是赋值的意思
  11. +-----------+--------+--------+
  12. | rownumber | emp_no | gender |
  13. +-----------+--------+--------+
  14. | 11 | 10001 | M |
  15. | 12 | 10002 | F |
  16. | 13 | 10003 | M |
  17. | 14 | 10004 | M |
  18. | 15 | 10005 | M |
  19. | 16 | 10006 | F |
  20. | 17 | 10007 | F |
  21. | 18 | 10008 | M |
  22. | 19 | 10009 | F |
  23. | 20 | 10010 | F |
  24. +-----------+--------+--------+
  25. 10 rows in set (0.00 sec)
  26. --
  27. -- 方法二 (推荐)
  28. --
  29. mysql> select @rn1:=@rn1+1 as rownumber, emp_no, gender from employees, (select @rn1:=0) as a limit 10;
  30. +-----------+--------+--------+
  31. | rownumber | emp_no | gender |
  32. +-----------+--------+--------+
  33. | 1 | 10001 | M |
  34. | 2 | 10002 | F |
  35. | 3 | 10003 | M |
  36. | 4 | 10004 | M |
  37. | 5 | 10005 | M |
  38. | 6 | 10006 | F |
  39. | 7 | 10007 | F |
  40. | 8 | 10008 | M |
  41. | 9 | 10009 | F |
  42. | 10 | 10010 | F |
  43. +-----------+--------+--------+
  44. 10 rows in set (0.00 sec)
  45. -- MySQL 自定义变量,根据每一记录进行变化的
  46. mysql> select @rn1:=0;
  47. +---------+
  48. | @rn1:=0 |
  49. +---------+
  50. | 0 | -- 只有一行记录
  51. +---------+
  52. 1 row in set (0.00 sec)
  53. -- 相当于把employees和(select @rn1:=0)做了笛卡尔积,然后使用@rn1:=@rn + 1,根据每行进行累加
  54. --
  55. -- ":=" "="
  56. --
  57. mysql> set @a:=1; -- 赋值为1
  58. Query OK, 0 rows affected (0.00 sec)
  59. mysql> select @a;
  60. +------+
  61. | @a |
  62. +------+
  63. | 1 |
  64. +------+
  65. 1 row in set (0.00 sec)
  66. mysql> set @a:=10; -- 赋值为10
  67. Query OK, 0 rows affected (0.00 sec)
  68. mysql> select @a;
  69. +------+
  70. | @a |
  71. +------+
  72. | 10 |
  73. +------+
  74. 1 row in set (0.00 sec)
  75. mysql> select @a=9; -- 进行比较
  76. +------+
  77. | @a=9 |
  78. +------+
  79. | 0 | -- 返回为False
  80. +------+
  81. 1 row in set (0.00 sec)
  82. --
  83. -- 作业:通过子查询或者其他方式,计算出employees的行号
  84. --

作者:殷建卫 链接:https://www.yuque.com/yinjianwei/vyrvkf/mb1gfl 来源:殷建卫 - 架构笔记 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。