- **MySQL表的增删改查**(基础)
- **配置默认字符集**
- **新增数据**
- **查询数据**
- **全列查询**
- **指定列查询**
- **带表达式查询**
- **起别名查询**
- **去重查询**
- **排序查询**
- **条件查询**
- **基础查询**
- **区间查询**
- **模糊查询**
- **NULL的查询**
- **分页查询**
- **修改数据**
- **删除数据**
- CRUD;create(新增),retrieve(检索),update(更新),delete(删除)。 ‘
插入一条记录:insert into student values(20,“haoshaokang”);
注意:这里一开始是不能插入中文字符串的,因为mysql数据库和服务器默认字符集是拉丁文,所以我们可以更改字符集为utf8,一种方式是创建数据库时设置字符集,比如:create database java104 character set utf8;
查看默认字符集命令:show variables like “character%”;
可以看到这里数据库和服务器默认字符集都是拉丁文
但是还有一种一劳永逸的方法:修改配置文件,把数据库和服务器默认字符集设置为utf8。配置文件名为:(my.ini) 路径:C:ProgramDataMySQLMySQL Server 5.7(此电脑路径)。
- 先备份配置文件
- 修改配置文件
-
保存配置文件
-
重启服务器
-
注意:之前创建的数据库还是用的之前的字符集,新创建的字符集才会使用utf8。
更改配置文件后,我们再来查一下默认字符集:
可见,数据库和服务器默认字符集都变成了utf8;
新增数据insert into student values (22,“郝少康”); (插入一条记录)
insert into student values (22,“郝少康”),(12,“李四”); (插入多条记录)。
insert into student(name) values(“郝少康”),(“李四”); (指定列插入多条记录);
- 在MySQL中每次插入一条记录,分多次插入比一次插入多条记录会慢上很多。
- 因为客户端每次发送请求给服务器,服务器解析SQL语句后,就开始执行操作,并把操作结果返回给客户端。
- 一条一条插就会有多个请求/响应。只插一次就只有一个请求/响应。
- 并且插入操作需要在数据库中进行一定的遍历操作,找到合适的位置。多次插入,服务器就要做多次的准备工作。
*select * from student; (全列查询)(代表通配符)
- 但是select * 操作可能有风险。因为如果数据库中的数据如果很多(几亿条),服务器需要从磁盘上找数据,然后再通过网络返回客户端,这个过程就有可能把磁盘IO吃满,或者把网络带宽吃满。那你这个服务器如果是线上服务器的话,其他用户可能就会体验不好。
- 万一执行了 select * 操作,可以ctrl+c取消操作。
select 列名,列名 from student; (指定列查询)
- select 所有操作结果都是呈现一个临时表,并不会改变数据库中的数据。
select id ,name,chinese+math+english from exam_result;
select id,name,math+10 from exam_result;
起别名查询select id,name,chinese+math+english as total from exam_result;
select id,name,chinese+math+english total from exam_result;
(有没有as都可以)
去重查询select distinct math from exam_result; (math列中去重)
select dintinct math,english from exam_result; (math和english都一样才去重)
select distinct * from exam_result; (所有列都相同才去重)
排序查询select * from exam_result order by math; (默认是升序排序)
select * from exam_result order by math desc; (降序排序)
select * from exam_result orde by math desc,chinese;
- 指定多列排序,如果第一列数据相等,则按第二列排序。
select name,chinese+math+english as total from exam_result order by total desc;
- 指定别名排序。
NULL和其他数据类型进行运算操作时,结果都是NULL
条件查询 基础查询- 根据条件筛选表中数据,并返回临时表。
select * from exam_result where math > 60;
select * from exam_result where chinese > english;
select name,chinese+math+english as total from exam_result where chinese+math+english < 200;
- where 后不能使用别名。
select * from exam_result where math > 60 and english > 70; (and代表逻辑与)
select * from exam_result where math > 60 or english > 70; (or代表逻辑或)
- and的优先级高于or
select * from exam_result where math between 60 and 80; (筛选数学在60到80之间的记录)(闭区间)
select * from exam_result where math in (60,78,63); (筛选数学是60,78,63其中一个的记录)
模糊查询使用通配符,%代替任意个任意字符,_代表一个任意字符
select * from exam_result where name like “孙%”; (以孙开头的名字)
select * from exam_result where name like “%孙” (以孙结尾的名字)
select from exam_result where name like “%孙%” (包含孙的名字)*
select * from exam_result where name like “孙_”;
NULL的查询select * from exam_result where math is not NULL;
select * from exam_result where math is NULL;
- is 和 is not 只能用来查询NULL。
- 使用=和NULL是不能正确匹配的 ,NULL=NULL的结果是NULL
- 使用<=>可以和NULL正确匹配,NULL<=>NULL结果是true
让查询结果,只取出其中一部分,从而降低开销,加快查询速度
select * from exam_result limit 3 offset 3; (限制查询三条记录,从第一条记录偏移3位置开始)。
select * from exam_result limit 3;(从第一条记录开始)。
- 查询操作也可以加入条件 [where] [order by] [limit]
update exam_result set math = 30 where name = “孙悟空”;
update exam_result set math = 50,chinese = 70 where name = “曹孟德”;
update exam_result set math = math-30 order by chinese+math+english limit 3; (总成绩倒数前三同学数学减30)
update exam_result set chinese = chinese - 10; (所有同学语文减10)
- 修改操作也可以加入条件 [where] [order by] [limit]
delete from exam_result where name = “孙悟空”;
delete from exam_result order by math limit 3; (删除数学倒数三名学生的记录)。
delete from exam_result; (删除整张表数据)
- 删除操作也可以设置条件 [where] [order by] [limit]



