当前位置:   article > 正文

Mysql数据库insert、replace、update、delete、truncate、drop、select语句介绍_mysql update replace

mysql update replace

目录

一.插入语句insert

1.插入单条数据

2.插入多条数据

二.替换语句replace

1.格式一

2.格式二,将其他表的字段复制到本表字段

3.格式三

三.修改语句update

四.删除语句delete、truncate、drop

1.delete from 表名 查找条件;

2.truncate table 表名;

3.drop table 表名;

五.查询语句select

1.基础语法

2.select配合算术表达式

3.配合as定义字段别名

4.处理重复记录

5.where条件查找

6.select子查询


一.插入语句insert

注意:into可以省略,特殊字符需要使用'\'转义

1.插入单条数据

(1)insert into 表名 values (字段1的值,字段2的值);

  1. mysql8.0 [SLB]>desc t1;
  2. +-------+----------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------+----------+------+-----+---------+-------+
  5. | id | int | YES | | NULL | |
  6. | name | char(20) | YES | | NULL | |
  7. +-------+----------+------+-----+---------+-------+
  8. 2 rows in set (0.00 sec)
  9. mysql8.0 [SLB]>insert t1 values (1,"sulibao");
  10. Query OK, 1 row affected (0.01 sec)
  11. mysql8.0 [SLB]>select * from t1;
  12. +------+---------+
  13. | id | name |
  14. +------+---------+
  15. | 1 | sulibao |
  16. +------+---------+
  17. 1 row in set (0.00 sec)

(2)insert 表名 set 字段=值,字段值;

  1. mysql8.0 [SLB]>insert t1 set id=2,name="lixinjin";
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql8.0 [SLB]>select * from t1;
  4. +------+----------+
  5. | id | name |
  6. +------+----------+
  7. | 1 | sulibao |
  8. | 2 | lixinjin |
  9. +------+----------+
  10. 2 rows in set (0.00 sec)

2.插入多条数据

insert 表名(字段1,字段2) values (字段1的值,字段2的值),(字段1的值,字段2的值),(字段1的值,字段2的值);

  1. mysql8.0 [SLB]>insert t1(id,name) values (3,"aaa"),(4,"bbb"),(5,"ccc");
  2. Query OK, 3 rows affected (0.00 sec)
  3. Records: 3 Duplicates: 0 Warnings: 0
  4. mysql8.0 [SLB]>select * from t1;
  5. +------+----------+
  6. | id | name |
  7. +------+----------+
  8. | 1 | sulibao |
  9. | 2 | lixinjin |
  10. | 3 | aaa |
  11. | 4 | bbb |
  12. | 5 | ccc |
  13. +------+----------+
  14. 5 rows in set (0.00 sec)

二.替换语句replace

注意:into可以省略,表在没有设置主键时使用replace命令,只会新增一条命令,replace不生效。使用replace语句向表插入新记录,如果新记录的主键值或者唯一性约束的字段值与已有记录相同,则已有记录先被删除(注意:已有记录删除时也不能违背外键约束条件),再插入新记录。

1.格式一

replace 表名 values(字段1的值,字段2的值) ;

  1. #有主键的t2
  2. mysql8.0 [SLB]>select * from t2;
  3. +----+----------+
  4. | id | name |
  5. +----+----------+
  6. | 1 | sulibao |
  7. | 2 | lixinjin |
  8. +----+----------+
  9. 2 rows in set (0.00 sec)
  10. mysql8.0 [SLB]>replace t2 values(2,"lioahang");
  11. Query OK, 2 rows affected (0.00 sec)
  12. mysql8.0 [SLB]>select * from t2;
  13. +----+----------+
  14. | id | name |
  15. +----+----------+
  16. | 1 | sulibao |
  17. | 2 | lioahang |
  18. +----+----------+
  19. 2 rows in set (0.00 sec)
  20. #无主键的t1
  21. mysql8.0 [SLB]>select * from t1;
  22. +------+----------+
  23. | id | name |
  24. +------+----------+
  25. | 1 | sulibao |
  26. | 2 | lixinjin |
  27. | 3 | aaa |
  28. | 4 | bbb |
  29. | 5 | ccc |
  30. +------+----------+
  31. 5 rows in set (0.00 sec)
  32. mysql8.0 [SLB]>replace t1 values(5,"666");
  33. Query OK, 1 row affected (0.00 sec)
  34. mysql8.0 [SLB]>select * from t1;
  35. +------+----------+
  36. | id | name |
  37. +------+----------+
  38. | 1 | sulibao |
  39. | 2 | lixinjin |
  40. | 3 | aaa |
  41. | 4 | bbb |
  42. | 5 | ccc |
  43. | 5 | 666 |
  44. +------+----------+
  45. 6 rows in set (0.00 sec)

2.格式二,将其他表的字段复制到本表字段

replace 表名(字段列表) select 字段列表 from 查找条件;

  1. mysql8.0 [SLB]>select * from t1;
  2. +------+----------+
  3. | id | name |
  4. +------+----------+
  5. | 1 | sulibao |
  6. | 2 | lixinjin |
  7. | 3 | aaa |
  8. | 4 | bbb |
  9. | 5 | ccc |
  10. | 5 | 666 |
  11. +------+----------+
  12. 6 rows in set (0.00 sec)
  13. mysql8.0 [SLB]>replace t2(id,name) select id,name from t1 where id=4;
  14. Query OK, 1 row affected (0.00 sec)
  15. Records: 1 Duplicates: 0 Warnings: 0
  16. mysql8.0 [SLB]>select * from t2;
  17. +----+----------+
  18. | id | name |
  19. +----+----------+
  20. | 1 | sulibao |
  21. | 2 | lioahang |
  22. | 4 | bbb |
  23. +----+----------+
  24. 3 rows in set (0.00 sec)

3.格式三

replace 表名 set 字段=值,字段=值;

  1. mysql8.0 [SLB]>replace t2 set id=4,name="lixinjin";
  2. Query OK, 2 rows affected (0.00 sec)
  3. mysql8.0 [SLB]>select * from t2;
  4. +----+----------+
  5. | id | name |
  6. +----+----------+
  7. | 1 | sulibao |
  8. | 2 | lioahang |
  9. | 4 | lixinjin |
  10. +----+----------+
  11. 3 rows in set (0.00 sec)

三.修改语句update

修改符合查找条件的字段,可指定修改多个字段

update 表名 set 修改字段=值 查找条件;

  1. mysql8.0 [SLB]>select * from t1;
  2. +------+----------+
  3. | id | name |
  4. +------+----------+
  5. | 1 | sulibao |
  6. | 2 | lixinjin |
  7. | 3 | aaa |
  8. | 4 | bbb |
  9. | 5 | ccc |
  10. | 5 | 666 |
  11. +------+----------+
  12. 6 rows in set (0.00 sec)
  13. mysql8.0 [SLB]>update t1 set id=6 where name="666";
  14. Query OK, 1 row affected (0.00 sec)
  15. Rows matched: 1 Changed: 1 Warnings: 0
  16. mysql8.0 [SLB]>select * from t1;
  17. +------+----------+
  18. | id | name |
  19. +------+----------+
  20. | 1 | sulibao |
  21. | 2 | lixinjin |
  22. | 3 | aaa |
  23. | 4 | bbb |
  24. | 5 | ccc |
  25. | 6 | 666 |
  26. +------+----------+
  27. 6 rows in set (0.00 sec)

四.删除语句delete、truncate、drop

1.delete from 表名 查找条件;

删除数据,保留表结构,可以恢复,数据量大时就很

  1. mysql8.0 [SLB]>select * from t2;
  2. +----+-----------+
  3. | id | name |
  4. +----+-----------+
  5. | 1 | sulibao |
  6. | 2 | lioahang |
  7. | 4 | lixinjin |
  8. | 5 | lixinjin |
  9. | 6 | sulibao |
  10. | 8 | lixinjin1 |
  11. +----+-----------+
  12. 6 rows in set (0.00 sec)
  13. mysql8.0 [SLB]>delete from t2 where name="lioahang";
  14. Query OK, 1 row affected (0.01 sec)
  15. mysql8.0 [SLB]>select * from t2;
  16. +----+-----------+
  17. | id | name |
  18. +----+-----------+
  19. | 1 | sulibao |
  20. | 4 | lixinjin |
  21. | 5 | lixinjin |
  22. | 6 | sulibao |
  23. | 8 | lixinjin1 |
  24. +----+-----------+
  25. 5 rows in set (0.00 sec)

2.truncate table 表名;

 删除所有数据,保留表结构,不可以恢复,一次全部删除所有数据,速度相对delete较快

3.drop table 表名;

直接删除表数据和表结构,速度最快

五.查询语句select

1.基础语法

select */具体字段 from 表名;

  1. mysql8.0 [SLB]>select id,name from t2;
  2. +----+-----------+
  3. | id | name |
  4. +----+-----------+
  5. | 1 | sulibao |
  6. | 4 | lixinjin |
  7. | 5 | lixinjin |
  8. | 6 | sulibao |
  9. | 8 | lixinjin1 |
  10. +----+-----------+
  11. 5 rows in set (0.00 sec)

2.select配合算术表达式

(1)对数值型数据列、变量、常量可以使用算数操作符创建表达式(+、-、*、/)

注意:

"+"默认只有运算符功能,对于转换成功的值直接相加,转换不成功就将字符型数值视为0,有一方为null值结果就为null值(null值:空值是指不可用、未对其分配值,空值不等于零或空格,任意数据类型都支持空值)

(2)对日期型数据列、变量、常量可以使用部分算数操作符创建表达式(+、-)

(3)可以在列和常量之间、多列之间进行运算

(4)优先级: 乘法和除法的优先级高于加法和减法,同级运算的顺序是从左到右,可以使用括号强行改变优先级

  1. mysql8.0 [SLB]>select * from t3;
  2. +----+------+----------+
  3. | id | day | permoney |
  4. +----+------+----------+
  5. | 1 | 18 | 300 |
  6. | 2 | 19 | 300 |
  7. | 3 | 25 | 450 |
  8. +----+------+----------+
  9. 3 rows in set (0.00 sec)
  10. mysql8.0 [SLB]>select id,day,permoney*day from t3;
  11. +----+------+--------------+
  12. | id | day | permoney*day |
  13. +----+------+--------------+
  14. | 1 | 18 | 5400 |
  15. | 2 | 19 | 5700 |
  16. | 3 | 25 | 11250 |
  17. +----+------+--------------+
  18. 3 rows in set (0.00 sec)

(5)安全等于运算符号<=>

用于比较数值大小,真1假0

  1. mysql8.0 [SLB]>select 2<=>2;
  2. +-------+
  3. | 2<=>2 |
  4. +-------+
  5. | 1 |
  6. +-------+
  7. 1 row in set (0.00 sec)
  8. mysql8.0 [SLB]>select 2<=>3;
  9. +-------+
  10. | 2<=>3 |
  11. +-------+
  12. | 0 |
  13. +-------+
  14. 1 row in set (0.00 sec)

判断是否为空

  1. mysql8.0 [SLB]>insert t3(id,day) values(4,26);
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql8.0 [SLB]>select * from t3;
  4. +----+------+----------+
  5. | id | day | permoney |
  6. +----+------+----------+
  7. | 1 | 18 | 300 |
  8. | 2 | 19 | 300 |
  9. | 3 | 25 | 450 |
  10. | 4 | 26 | NULL |
  11. +----+------+----------+
  12. 5 rows in set (0.00 sec)
  13. mysql8.0 [SLB]>select id,day from t3 where permoney<=>null;
  14. +----+------+
  15. | id | day |
  16. +----+------+
  17. | 4 | 26 |
  18. +----+------+
  19. 2 rows in set (0.00 sec)

3.配合as定义字段别名

  1. mysql8.0 [SLB]>select id as "工号",day "工期",permoney "每日工资" from t3;
  2. +--------+--------+--------------+
  3. | 工号 | 工期 | 每日工资 |
  4. +--------+--------+--------------+
  5. | 0 | NULL | NULL |
  6. | 1 | 18 | 300 |
  7. | 2 | 19 | 300 |
  8. | 3 | 25 | 450 |
  9. | 4 | 26 | NULL |
  10. +--------+--------+--------------+
  11. 5 rows in set (0.00 sec)

4.处理重复记录

(1)查看重复

  1. mysql8.0 [SLB]>select * from t2;
  2. +----+-----------+
  3. | id | name |
  4. +----+-----------+
  5. | 1 | sulibao |
  6. | 4 | lixinjin |
  7. | 5 | lixinjin |
  8. | 6 | sulibao |
  9. | 8 | lixinjin1 |
  10. +----+-----------+
  11. 5 rows in set (0.00 sec)
  12. mysql8.0 [SLB]>select name from t2;
  13. +-----------+
  14. | name |
  15. +-----------+
  16. | sulibao |
  17. | lixinjin |
  18. | lixinjin |
  19. | sulibao |
  20. | lixinjin1 |
  21. +-----------+
  22. 5 rows in set (0.00 sec)

(2)distinct清除重复行,可以指定字段范围

  1. mysql8.0 [SLB]>select distinct name from t2;
  2. +-----------+
  3. | name |
  4. +-----------+
  5. | sulibao |
  6. | lixinjin |
  7. | lixinjin1 |
  8. +-----------+
  9. 3 rows in set (0.00 sec)

5.where条件查找

(1)where的字符串和日期值需要用单引号括起来,日期值格式敏感

(2)where比较运算符

比较运算符含义
>,<大于,小于
>=,<=大于等于,小于等于
=,!=等于,不等于

(3)is null查找空记录

  1. mysql8.0 [SLB]>select * from t3 where permoney is null;
  2. +----+------+----------+
  3. | id | day | permoney |
  4. +----+------+----------+
  5. | 4 | 26 | NULL |
  6. +----+------+----------+
  7. 1 rows in set (0.01 sec)

(4)between and查找范围内的记录

  1. mysql8.0 [SLB]>select * from t3 where permoney between 400 and 500;
  2. +----+------+----------+
  3. | id | day | permoney |
  4. +----+------+----------+
  5. | 3 | 25 | 450 |
  6. +----+------+----------+
  7. 1 row in set (0.00 sec)

(5)in查找恰好匹配记录

  1. mysql8.0 [SLB]>select * from t3 where permoney in(300);
  2. +----+------+----------+
  3. | id | day | permoney |
  4. +----+------+----------+
  5. | 1 | 18 | 300 |
  6. | 2 | 19 | 300 |
  7. +----+------+----------+
  8. 2 rows in set (0.01 sec)

(6)like模糊匹配,“like”表示0或多个字符,“_”表示任意单个字符

  1. mysql8.0 [SLB]>select * from t3 where day like "1%";
  2. +----+------+----------+
  3. | id | day | permoney |
  4. +----+------+----------+
  5. | 1 | 18 | 300 |
  6. | 2 | 19 | 300 |
  7. +----+------+----------+
  8. 2 rows in set (0.00 sec)

(7)where逻辑运算符

逻辑运算符用法含义
and

条件都需要满足

mysql8.0 [SLB]>select * from t3 where day<19 and permoney >=300;
+----+------+----------+
| id | day  | permoney |
+----+------+----------+
|  1 |   18 |      300 |
+----+------+----------+
1 row in set (0.00 sec)

or

条件满足一个即可

mysql8.0 [SLB]>select * from t3 where day<19 or permoney >=300;
+----+------+----------+
| id | day  | permoney |
+----+------+----------+
|  1 |   18 |      300 |
|  2 |   19 |      300 |
|  3 |   25 |      450 |
+----+------+----------+
3 rows in set (0.00 sec)

not

取反输出

mysql8.0 [SLB]>select * from t3 where not day<20;
+----+------+----------+
| id | day  | permoney |
+----+------+----------+
|  3 |   25 |      450 |
|  4 |   26 |     NULL |
+----+------+----------+
2 rows in set (0.00 sec)
 

(8)联合查询

对两个结果求并集,union去重,union all 不去重

  1. mysql8.0 [SLB]>select * from t1 where name in("sulibao","lixinjin") union select * from t2 where id in(1,6);
  2. +------+----------+
  3. | id | name |
  4. +------+----------+
  5. | 1 | sulibao |
  6. | 2 | lixinjin |
  7. | 6 | sulibao |
  8. +------+----------+
  9. 3 rows in set (0.00 sec)
  10. mysql8.0 [SLB]>select * from t1 where name in("sulibao","lixinjin") union all select * from t2 where id in(1,6);
  11. +------+----------+
  12. | id | name |
  13. +------+----------+
  14. | 1 | sulibao |
  15. | 2 | lixinjin |
  16. | 1 | sulibao |
  17. | 6 | sulibao |
  18. +------+----------+
  19. 4 rows in set (0.00 sec)

(9)排序显示

通过order by指定根据哪个字段进行排序,asc升序(默认),desc降序

  1. mysql8.0 [SLB]>select * from t3 order by day desc;
  2. +----+------+----------+
  3. | id | day | permoney |
  4. +----+------+----------+
  5. | 4 | 26 | NULL |
  6. | 3 | 25 | 450 |
  7. | 2 | 19 | 300 |
  8. | 1 | 18 | 300 |
  9. | 0 | NULL | NULL |
  10. +----+------+----------+
  11. 5 rows in set (0.00 sec)
  12. mysql8.0 [SLB]>select * from t3 order by day asc;
  13. +----+------+----------+
  14. | id | day | permoney |
  15. +----+------+----------+
  16. | 0 | NULL | NULL |
  17. | 1 | 18 | 300 |
  18. | 2 | 19 | 300 |
  19. | 3 | 25 | 450 |
  20. | 4 | 26 | NULL |
  21. +----+------+----------+
  22. 5 rows in set (0.00 sec)

(10)where regexp 正则表达式

  1. mysql8.0 [SLB]>select * from t1 where name regexp '^su';
  2. +------+---------+
  3. | id | name |
  4. +------+---------+
  5. | 1 | sulibao |
  6. +------+---------+
  7. 1 row in set (0.04 sec)
  8. mysql8.0 [SLB]>select * from t1 where name regexp '^a';
  9. +------+------+
  10. | id | name |
  11. +------+------+
  12. | 3 | aaa |
  13. +------+------+
  14. 1 row in set (0.00 sec)

6.select子查询

在匹配条件里再写select语句

  1. mysql8.0 [SLB]>select * from t6;
  2. +------+--------+------+
  3. | age | weight | id |
  4. +------+--------+------+
  5. | 18 | 75.5 | 1 |
  6. | 19 | 75.5 | 2 |
  7. | 25 | 90 | 3 |
  8. +------+--------+------+
  9. 3 rows in set (0.00 sec)
  10. mysql8.0 [SLB]>
  11. mysql8.0 [SLB]>
  12. mysql8.0 [SLB]>select weight from t6 where age> avg(age);
  13. ERROR 1111 (HY000): Invalid use of group function
  14. mysql8.0 [SLB]>select weight from t6 where age>any(select avg(age) from t6);
  15. +--------+
  16. | weight |
  17. +--------+
  18. | 90 |
  19. +--------+
  20. 1 row in set (0.01 sec)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/404852
推荐阅读
相关标签
  

闽ICP备14008679号