数据库的操作
- 查看有哪些数据库
show databases; - 用哪个数据库
use db_name; - 创建数据库
create database [if not exists] db_name; - 删除数据库
drop database [if exists] db_name; - 查看当前用的数据库是哪个
select database(); - 查看当前数据库下创建了哪些表
show tables
表操作
1. 建表
drop table if exists stu_test;
create table stu_test(
id int,
sn int comment '学号',//comment 是注释的意思 可以不写
name varchar(20) comment '姓名',
qq_mail varchar(20) comment '邮箱'
);
2. 删表
drop table if exists table_name;
3. 查看表结构
desc table_name;或者describe table_name;
4. 表的增删查改 CRUD
新增(Create) INSERT
① 单行全列插入
insert into stu_test values(1,2017001,'chen','chen@qq.com');
② 指定列插入
insert into stu_test (id,sn) values(3,2017003);
insert into stu_test values(3,2017003);这样就不可以 必须要指定要插入的列名
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NONRTrLk-1598438620621)(en-resource://database/4327:1)]](/https://i-blog.csdnimg.cn/blog_migrate/3ea8e2dd65466ae9ad1cd8e0d5f965e5.png#pic_center)
③ 多行数据+指定列插入
insert into stu_test (id,sn) values
(3,2017003),
(4,2017004),
(5,2017005);
查询(Retrieve) select
SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...
① 全列查询select * from stu_test;
不过通常不建议使用 * 进行全列查询
– 1. 查询到的列越多,意味着要传输的数据量越大
– 2. 可能会影响到索引的使用
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-deVgyZBD-1598438620623)(en-resource://database/4329:1)]](/https://i-blog.csdnimg.cn/blog_migrate/2cf58b7e0ecc03d152242b48d32f442f.png#pic_center)
② 指定列查询 select id,name from stu_test;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7OkFC2L8-1598438620625)(en-resource://database/4331:1)]](/https://i-blog.csdnimg.cn/blog_migrate/be2e2e6a5559821bc759e7eebfc94bdd.png#pic_center)
③ 特定显示
比如说想要将1显示成2001
select id+2000,name from stu_test;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aqvebkML-1598438620626)(en-resource://database/4333:1)]](/https://i-blog.csdnimg.cn/blog_migrate/00d641906c91d5b2a6a2721d06474f9f.png#pic_center)
④ 别名 (未查询结果中的列指定别名,表示返回的结果集中,以别名作为该列的名称)
-- 从table_name中查找column并且再返回的结果集中将alias_name作为该列的名称
SELECT column [AS] alias_name [...] FROM table_name;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sy0MSlHg-1598438620627)(en-resource://database/4339:1)]](/https://i-blog.csdnimg.cn/blog_migrate/77915393cc87ba8dd205870a58c026ab.png#pic_center)
⑤ 去重(distinct)


⑥ 排序 order by
– a. 没有注明的情况下都是默认升序ASC
– b. null数据排序的时候,视为比任何数据都笑,升序出现在最上面,降序出现在最下面
-- ASC 为升序(从小到大)
-- DESC 为降序(从大到小)
-- 默认为 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
--可以对多个字段进行排序,排序优先级随书写的顺序
-- 查询同学各门成绩,依次按 数学降序,英语升序,语文升序的方式显示
SELECT name, math, english, chinese FROM exam_result
ORDER BY math DESC,english,chinese;


⑦ 条件查询:WHERE
注意: where条件可以使用表达式,但不能使用别名
- 基本查询 (>,>=,<.<=,=,<=>,!=,<>)
注意:
a. = , <=>都是等于,但是前者null不安全(比如null=null的结果就是null),后者null安全(null<=>null的结果是true/1),所以建议用<=>
b. != , <> 都是不等于
-- 查询英语不及格的
select * from score where english<60;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VUHzDsWK-1598438620634)(en-resource://database/4345:1)]](/https://i-blog.csdnimg.cn/blog_migrate/706dd36a31c2299cc08e12e806a5263b.png#pic_center)
- AND 和 OR
--英语 和 数学都=100
select * from score where english=100 and math=100;
--英语 或 数学都=100
select * from score where english=100 or math=100;
and的优先级高于or,在同时使用的时候,注意要用小括号包裹住先执行的部分
-- 查询数学和英语都>70的同学中,语文>80的同学
select * from score where chinese > 80 or math > 70 and english > 70;
-- 查询语文>80 或 数学>70的同学中,英语>70的同学
select * from score where (chinese > 80 or math > 70) and english > 70;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qUzJjFIr-1598438620634)(en-resource://database/4347:1)]](/https://i-blog.csdnimg.cn/blog_migrate/7866a19a095df9fd6587394375e010e8.png#pic_center)
- 范围查询
- between …and…
--查询英语成绩在90-100之间的同学
select * from score where english between 90 and 100;
-- 或者AND也可以实现
select * from score where english>=90 and english<=100;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jJ3DJWE0-1598438620635)(en-resource://database/4349:1)]](/https://i-blog.csdnimg.cn/blog_migrate/86c21aa5af22c304685793ec80dd2ba0.png)
2. in
-- 查询数学成绩是68.78.88的同学
select * from score where math in (68,78,88);
-- 或者OR也可以实现
select * from score where math=68 or math=78 or math=88;
- 模糊查询 LIKE
模糊查询 like一般用在字符串匹配上
%可代表多个字符 _ 只能代表一个字符
-- %匹配任意多个字符(包括0个)
select * from stu_test where name like 'c%';
-- _严格匹配一个任意的字符
select * from stu_test where name like 'c_'
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bNzgfBAb-1598438620636)(en-resource://database/4351:1)]](/https://i-blog.csdnimg.cn/blog_migrate/c18b80385318039fe4409b87540169ce.png#pic_center)
- null的查询 IS [NOT] NULL
-- 查询qq邮箱已知的同学
select * from stu_test where qq_mail is not null;
-- 查询qq邮箱未知的同学
select * from stu_test where qq_mail is null;
⑧ 分页查询 LIMIT
– 从0开始筛选n条结果
select ... from table_name [where ...][order by ...] limit n;
– 从s开始筛选n条结果(注意不包含第s条数据哦)
-- 第一种
select ... from table_name [where ...][order by ...] limit s,n;

– 从n开始筛选s条结果(注意这两个语句的区别)
-- 第二种(比较推荐)
select ... from table_name [where ...][order by ...] limit s offset n;
【例】:按id进行分页,每页3条记录,分别显示1,2,3页
– 第一页
select * from stu_test order by id limit 3 offset 0;
select * from stu_test order by id limit 0,3;
– 第二页
select * from stu_test order by id limit 3 offset 3;
select * from stu_test order by id limit 3,3;
– 第三页
select * from stu_test order by id limit 3 offset 6;
select * from stu_test order by id limit 6,3;
修改 Update
update table_name set column = expr [,column = expr...] [where...][order by...][limit...]
-- 将所有同学的英语成绩变为原来的两倍
update stu_test set english = english*2;
-- 将dai同学的数学成绩变为100分
update stu_test set math=100 where name='dai';
-- 将数学成绩倒数前三的3个同学数学成绩+10
update stu_test set math=math+10 order by math limit 3;
删除
delete from table_name [where...][order by...][limit...]
-- 删除dai同学的考试成绩
delete from stu_test where name='dai';
-- 删除整张表的数据
delete from stu_test;
本文详细介绍SQL数据库的基本操作,包括数据库的创建、删除、查询、更新和删除等核心功能,以及表的增删改查和数据的条件筛选、排序、分页等高级技巧。

813

被折叠的 条评论
为什么被折叠?



