[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-unH48KSp-1606130351587)(img/tu_17.png)]
我们把Sql介绍完成了, 那下面就通过DDL操作数据库
create database 数据库名 [character set 字符集][collate 校对规则] 注: []意思是可选的意思
字符集(charset):是一套符号和编码。
创建一个day16的数据库(默认字符集)
create database web14_1;
创建一个day16_2的数据库,指定字符集为gbk(了解)
create database web14_2 character set gbk;
show databases;
show create database 数据库名;
show create database web14_1;
drop database 数据库名;
drop database web14_2;
alter database 数据库名 character set 字符集;
alter database web14_1 character set gbk;
注意:
use 数据库名; //注意: 在创建表之前一定要指定数据库. use 数据库名
use web14_1;
select database();
create database 数据库的名字;
drop database 数据库的名字;
use 数据库的名字; -- 切换到某个数据库
select database(); -- 查看正在使用哪个数据库
我们第四章已经把数据库的CRUD讲解完了,下面我们就学习创建表
create table 表名(
字段名 字段类型 [约束],
字段名 字段类型 [约束],
........
字段名 字段类型 [约束]
);
整型 一般使用int 或者bigint
浮点/双精度型
字符串
一般使用varchar(n) 节省空间; 如果长度(eg:身份证)是固定的话 可以使用char(n) 性能高一点
关于大文件
日期
约束 | 约束关键字 |
---|---|
主键 | primary key |
唯一 | unique |
非空 | not null |
约束种类:
not null: 非空 ; eg: username varchar(40) not null username这个字段不能为空,必须要有数据
unique:唯一约束, 后面的数据不能和前面重复; eg: cardNo char(18) unique; cardNo字段不能出现重复的数据
primary key;主键约束(非空+唯一); 一般用在表的id列上面. 一张表基本上都有id列的, id列作为唯一标识的
注意:
id列:
create table student(
id int primary key auto_increment,
name varchar(40),
sex int
);
create table 表名(
列 类型 【约束】,
列 类型 【约束】
);
我们把表创建好了, 下面就来介绍查看表
show tables;
语法
desc 表名;
练习: 查看student表的定义结构
desc student;
我们表创建好了, 如果要增加一列,要删除一列呢? 那下面就来讲解修改表
alter table student add grade varchar(20) not null;
alter table student modify sex varchar(10);
alter table student change grade class varchar(20);
alter table student drop class;
rename table student to teacher
表创建好了, 我们还可以删除。 掌握表的删除
语法
drop table 表名;
把teacher表删除
drop table teacher;
drop table 表名;
create table product(
pid int primary key auto_increment, //只有设置了auto_increment id列才可以赋值为null
pname varchar(40),
price double,
num int
);
方式一: 插入指定列, 如果没有把这个列进行列出来, 以null进行自动赋值了.
eg: 只想插入pname, price , insert into t_product(pname, price) values(‘mac’,18000);
insert into 表(列,列..) values(值,值..);
注意: 如果没有插入了列设置了非空约束, 会报错的
方式二: 插入所有的列
insert into 表 values(值,值…);
eg:
insert into product values(null,‘苹果电脑’,18000.0,10);
insert into product values(null,‘华为5G手机’,30000,20);
insert into product values(null,‘小米手机’,1800,30);
insert into product values(null,‘iPhonex’,8000,10);
insert into product values(null,‘苹果电脑’,8000,100);
insert into product values(null,‘iPhone7’,6000,200);
insert into product values(null,‘iPhone6s’,4000,1000);
insert into product values(null,‘iPhone6’,3500,100);
insert into product values(null,‘iPhone5s’,3000,100);
insert into product values(null,‘方便面’,4.5,1000);
insert into product values(null,‘咖啡’,11,200);
insert into product values(null,‘矿泉水’,3,500);
命令行插入中文数据报错:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y0m5BNYZ-1606130351588)(img/tu_3.png)]
关闭服务, net stop MySql
在数据库软件的安装目录下面, 修改配置文件 my.ini中客户端的编码为gbk
重新打开命令行,开启服务, net start MySql
语法
insert into 表名(列,列) values(值,值)
insert into 表名 values(值,值,值....)
注意
我们数据插入成功了, 还可以对已有的数据进行更新。
update 表名 set 列 =值, 列 =值,... [where 条件]
update product set price = 5000;
UPDATE product set price = 18000 WHERE name = 'Mac';
UPDATE product set price = 17000,num = 5 WHERE name = 'Mac';
UPDATE product set price = price+2 WHERE name = '方便面';
update 表 set 列 = 值, 列=值, 列=值 [where 条件]
delete from 表 [where 条件] 注意: 删除数据用delete,不用truncate
练习
delete from product where pname = 'Mac';
delete from product where price < 5001;
delete from product;
truncate table 表;
delete from 表 【where 条件】
truncate table 表;
delete 和truncate区别【面试题】
工作里面的删除
工作里面一般使用逻辑删除用的多
我们上面讲解了对数据的增删改, 下面就来重点讲解数据的简单查询.
select [*][列名 ,列名][列名 as 别名 ...] [distinct 字段] from 表名 [where 条件]
select * form 表
select * from product;
select 列名,列名,列名... from 表
select pname, price from product;
SELECT DISTINCT 字段名,字段名,... FROM 表名; //要数据一模一样才能去重
select distinct price from product;
注意点: 去重针对某列, distinct前面不能先出现列名
select 列名 as 别名 ,列名 from 表 //列别名 as可以不写
select 别名.* from 表 as 别名 //表别名(多表查询, 明天会具体讲)
select pname , price as 价格 from product;
select pname ,price+10 from product;
注意
select ... from 表 where 条件
//取出表中的每条数据,满足条件的记录就返回,不满足条件的记录不返回
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6a7mAXJg-1606130351590)(img/tu_2.png)]
eg: where price between 1000 and 3000 相当于 1000<=price<=3000
-- 查询id为1,3,5,7的
select * from t_product where id = 1
select * from t_product where id = 3
select * from t_product where id = 5
select * from t_product where id = 7
select * from t_product where id in(1,3,5,7)
name like '张%' --查询姓张的用户, 名字的字数没有限制
name like '张_' --查询姓张的用户 并且名字是两个的字的
where 条件1 and 条件2 and 条件3
where 条件1 or 条件2 or 条件3
select * from product where price > 3000;
select * from product where pid = 1;
select * from product where pid <> 1;
select * from product where price between 3000 and 6000;
select * from product where id in (1,5,7,15);
select * from product where pname like 'iPho%';
select * from product where price > 3000 and num > 20;
select * from product where pid = 1 or price < 3000;
select [*],[列名,列名],[列名 as 别名],[distinct 列名],[列(+,-..)] from 表名 [where 条件]
有时候我们需要对查询出来的结果排序显示,那么就可以通过ORDER BY
子句将查询出的结果进行排序。排序可以根据一个字段排,也可以根据多个字段排序,排序只是对查询的结果集排序,并不会影响表中数据的顺序。
# 创建学生表(有sid,学生姓名,学生性别,学生年龄,分数列,其中sid为主键自动增长)
CREATE TABLE student(
sid INT PRIMARY KEY auto_increment,
sname VARCHAR(40),
sex VARCHAR(10),
age INT,
score DOUBLE
);
INSERT INTO student VALUES(null,'zs','男',18,98.5);
INSERT INTO student VALUES(null,'ls','女',18,96.5);
INSERT INTO student VALUES(null,'ww','男',15,50.5);
INSERT INTO student VALUES(null,'zl','女',20,98.5);
INSERT INTO student VALUES(null,'tq','男',18,60.5);
INSERT INTO student VALUES(null,'wb','男',38,98.5);
INSERT INTO student VALUES(null,'小丽','男',18,100);
INSERT INTO student VALUES(null,'小红','女',28,28);
INSERT INTO student VALUES(null,'小强','男',21,95);
SELECT 字段名 FROM 表名 [WHERE 条件] ORDER BY 字段名 [ASC|DESC]; //ASC: 升序,默认值; DESC: 降序
SELECT * FROM student ORDER BY score DESC
SELECT 字段名 FROM 表名 [WHERE 字段=值] ORDER BY 字段名1 [ASC|DESC], 字段名2 [ASC|DESC];
SELECT * FROM student ORDER BY score DESC, age DESC
order by 列 asc/desc, 列 asc/desc;
asc: 升序【默认值】
desc: 降序
应用场景
商城里面 根据价格, 销量, 上架时间, 评论数…
社交里面 根据距离排序
之前我们做的查询都是横向查询,它们都是根据条件一行一行的进行判断,而使用聚合函数查询是==纵向查询,它是对一列的值进行计算,然后返回一个结果值==。聚合函数会忽略空值NULL
聚合函数 | 作用 |
---|---|
max(列名) | 求这一列的最大值 |
min(列名) | 求这一列的最小值 |
avg(列名) | 求这一列的平均值 |
count(列名) | 统计这一列有多少条记录 |
sum(列名) | 对这一列求总和 |
SELECT 聚合函数(列名) FROM 表名 [where 条件];
-- 求出学生表里面的最高分数
-- 求出学生表里面的最低分数
-- 求出学生表里面的分数的总和(忽略null值)
-- 求出学生表里面的平均分
-- 统计学生的总人数 (忽略null)
-- 语法格式:select 聚合函数(列) from 表名 [where 条件];
-- max(列名) 获取列的最大值
-- min(列名) 获取列的最小值
-- sum(列名) 获取列的总和
-- avg(列名) 获取列的平均值
-- count(列名) 获取列的个数
-- ifnull(列名,默认值) 判断如果该列的位置的值为null,就取后面的默认值,如果不为null,则取该值本身
-- 求出学生表里面的最高分数
select max(score) from student;
-- 求出学生表里面的最低分数
select min(score) from student;
-- 求出学生表里面的分数的总和(忽略null值) 把小红的28分改为了null
select sum(score) from student; -- 698
-- 求出学生表里面的平均分
select avg(score) from student; -- 忽略null值 87.25 少算了一个小红
select avg(ifnull(score,0)) from student; -- 77.55555555555556
-- 统计学生的总人数 (忽略null)
select count(score) from student; -- 8
select count(age) from student; -- 9
select count(*) from student; -- 9
注意: 聚合函数会忽略空值NULL
我们发现对于NULL的记录不会统计,建议如果统计个数则不要使用有可能为null的列,但如果需要把NULL也统计进去呢?我们可以通过 IFNULL(列名,默认值) 函数来解决这个问题. 如果列不为空,返回这列的值。如果为NULL,则返回默认值。
select 聚合函数(列) from 表名;
聚合函数
注意事项
分组查询是指使用 GROUP BY语句对查询信息进行分组
GROUP BY怎么分组的? 将分组字段结果中相同内容作为一组,如按性别将学生分成两组
GROUP BY将分组字段结果中相同内容作为一组,并且返回每组的第一条数据,所以单独分组没什么用处。分组的目的就是为了统计,一般分组会跟聚合函数一起使用
SELECT 字段1,字段2... FROM 表名 [where 条件] GROUP BY 列 [HAVING 条件];
-- 根据性别分组, 统计每一组学生的总人数
SELECT sex, count(*) FROM student GROUP BY sex
SELECT sex, count(*) FROM student GROUP BY sex HAVING count(*) > 5
group by 分组字段 [having 条件]
注意事项
单独分组 没有意义, 返回每一组的第一条记录
分组的目的一般为了做统计使用, 所以经常和聚合函数一起使用
分组查询如果不查询出分组字段的值,就无法得知结果属于那组
在分组里面, 如果select后面的列没有出现在group by后面 ,展示这个组的这个列的第一个数据
where和having的区别【面试】
子名 | 作用 |
---|---|
where 子句 | 1) 对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,即先过滤再分组。2) where后面不可以使用聚合函数 |
having字句 | 1) having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,即先分组再过滤。2) having后面可以使用聚合函数 |
LIMIT是限制的意思,所以LIMIT的作用就是限制查询记录的条数. 经常用来做分页查询
select ... from .... limit 起始行数,查询的记录条数.
LIMIT a,b; |
---|
a起始行数,从0开始计数,如果省略,默认就是0; a=(当前页码-1)*b; |
b: 返回的行数 |
select * from 表名 limit (页码-1)*每页显示的总条数,每页显示的总条数;
第一页: a=0, b=4;
第二页: a=4, b=4;
第三页: a=8, b=4;
....
limit a,b;
a:从哪里开始查询, 从0开始计数 【a=(当前页码-1)*b】
b: 一页查询的数量【固定的,自定义的】
应用场景
如果数据库里面的数据量特别大, 我们不建议一次查询出来. 为了提升性能和用户体验, 使用分页
select...from...[where...][group by...having...][order by...][limit...]
select...from...
select...from...where...
select...from...group by...having...
select...from...order by...
select...from...limit...
- 能够使用SQL语句操作数据库
创建: create database 数据库名;
删除: drop database 数据库名;
查询:
show databases;
show create database 数据库名;
修改:
alter database 数据库名 character set 字符集;
其他:
use 数据库名;
select database();
- 能够使用SQL语句操作表结构
创建:
create table 表名(
字段名 字段类型 [约束],
字段名 字段类型 [约束],
...
字段名 字段类型 [约束]
);
字段类型:
数值: int,bigint,float,double
字符: char(长度),varchar(长度)
日期: date,datetime
约束:
primary key: 注解 (auto_increment:自增)
unique: 唯一
not null: 不能为空
删除:
drop table 表名;
查询:
show tables;
desc 表名;
修改:
alter table 表名 add 字段名 字段类型 字段约束;
alter table 表名 modify 字段名 字段类型 字段约束;
alter table 表名 change 旧字段名 新字段名 字段类型 字段约束;
alter table 表名 drop 字段名;
rename table 旧表名 to 新表名;
- 能够使用SQL语句进行数据的添加修改和删除的操作
插入:
1.指定列插入
insert into 表名(列名,类名,...) values(值,值,...);
注意:
1.后面的值与前面的列需要一一对应(个数,类型,...)
2.非数值型的数据需要使用引号引起来(单引号,双引号)
3.未指定的列,默认会赋null值,所以如果该列设置了非空约束,就会报错
2.插入所有列
insert into 表名 values(值,值,...);
注意: 如果主键设置了自增,那么注解值可以赋值为null,系统会自动对主键进行自增操作
修改:
update 表名 set 列名 = 值,列名 = 值,... where 条件;
删除:
delete from 表名 where 条件
truncate table 表名;
- 能够使用SQL语句进行条件查询数据
select ... from 表名 where 条件;
条件:
比较运算
>,>=,<,<=,<>,=
between...and...
in()
like
...
逻辑运算
and
or
not
- 能够使用SQL语句进行排序
select ... from ... order by 字段 [asc|desc]; 默认升序
- 能够使用聚合函数
max(列名);
min(列名);
sum(列名);
avg(列名);
count(列名);
ifnull(列名,默认值);
- 能够使用SQL语句进行分组查询
select ... from ... group by 字段名 [having 条件];
- 能够使用SQL语句进行分页查询
select ... from ... limit 起始显示行数,显示的总条数;
select ... from ... [where 条件] [group by 字段] [having 条件] [order by 字段] [limit a,b]