一,操作数据库,数据库表(DDL)
1.1,有关数据库database
创建
create database [if not exists] 数据库名 [character set utf8];
# 创建数据库,带有中括号的可以不写,一般有默认
查询
show databases;
# 查看MySQL数据库里面存在的所有数据库,其实navicat旁边可视化也都是能看见的
show create database 数据库名; # 查看某个创建数据库的语句
修改
alter database 数据库名 character set 字符集名称; # 修改数据库字符集
删除
drop database [if exists] 数据库名; # 删除对应数据库,带中括号的可以不写
使用
use 数据库名; # use语句用于选择一个数据库, 只有选择了数据才能操作其里面的所有对象
select database(); # 查询正在使用的数据库名称
1.2,操作表
创建(create)
create tabale 表名(列名1 数据类型1,列名2 数据类型2, ... 列名n 数据类型n);
# 最后的列后面不需要接","
查询(retrieve)
show tables; # 查询某个数据库中所有的表名称
desc 表名; # 查询表结构
show full columns from 表名; # 查看表结构(更加详细)
show create table 表名; # 查看创建表的SQL语句
修改(update)
alter table 表名 rename to 新的表名; # 修改表名
alter table 表名 character set 字符集名称; # 修改表的字符集
alter table 表名 add 列名 数据类型; # 添加一列
alter table 表名 change 列名 新列名 新数据类型; # 修改列名称
alter table 表名 modify 列名 新数据类型; # 修改数据类型
alter table 表名 drop 列名; # 删除列
删除
drop table [if exists] 表名; # 中括号里面的可以不写
复制表
create table 表名 like 被复制的表名; # 此处复制的只是表结构,不带内容
create table 表名 select语句; # 复制创建选定内容的表,也可以用来全部复制
1.3,增删改表中的数据
添加数据
insert into 表名(列名1,列名2...列名n) values(值1,值2...值n);
# 1,列名和值要一一对应
# 2,如果表名后不定义列名表示给所有列添加值
# 3,除了数字类型外,其他的数据都需要用单引号或者双引号引起来
insert [into] 表名 select语句;
# 字段名等需要能够匹配上
删除数据
delete from 表名 [where条件];
# 1,如果不加条件表示删除所有记录
# 2,如果要删除所有记录不推荐使用这个因为有多少记录就会执行多少次删除操作,推荐 如下:
truncate table 表名; # 效率更高,先删除表,然后再创建结构一样但是没有记录数据的表
修改数据
update 表名 set 列名1=值1,列名2=值2... [where条件];
# 如果不加任何附加条件则表示对应的所有数据都修改了
二,查询表中的记录(DQL)
2.1,语法:
语句书写顺序:
select 字段列表
from 表名列表
where 条件列表
group by 分组字段
having 分组之后的条件
order by 排序
limit 分页限制
语句执行顺序:
1,from
2,where
3,group by
4,计算聚合
5,having
6,计算表达式
7,order by
8,limit
9,select
2.2,基础查询
1,多个字段的查询
select 字段1,字段2...from 表名; # 需要显示全部字段的话就用符号 *
2,去重查询
distinct
3,计算列
一般使用四则运算计算一些列的值,只是真是数值型数据
4,起别名
as # as是可以省略不写的
2.3,条件查询
1,where子句后面跟条件
2,运算符
(1) >,<,>=,<=,!=,<>
(2) between... and
(3) in(集合)
(4) like: 模糊查询
_:单个占位符
%:多个任意字符
(5) is null, is not null # 是空和不是空
(6) and 或 &&, or 或 ||
(7) not 或 !
3,使用demo
select * from student where age>=20;
select * from student where age<>20;
select * from student where age!=20;
select * from student where age>=20 and age<=30;
select * from student where age>=20 && age<=30;
select * from student where age between 20 and 30;
select * from student where age=18 or age=22 || age=25;
select * from student where age in(18,22,25);
select * from student where english is null;
select * from student where english is not null;
select * from sthdent where english=null; # 错误的使用方式,null值不能用=或!=判断
select * from student where name like "马%"; # 查找姓马的同学信息
select * from student where name like "_化%"; # 查找名字第二个字是化的
select * from student where name like "%德%"; # 查找名字里面带有德字的
select * from student where name like "___"; # 查找名字是三个字的
2.4,排序查询
1,语法: order by 子句
order by 排序字段1 排序方式1,排序字段2 排序方式2...
2,排序方式:
(1) asc: 升序,默认的
(2) desc: 降序
3,注意:如果多个排序条件,则当前面的条件值一样时,才会判断第二条件
2.5,聚合函数:将一列数据作为一个整体进行纵向的计算
1,count: 计算个数
(1) 一般选择非空的列: 主键
(2) count(*)
2,max(): 计算最大值 min():计算最小值 sum():计算和 avg():求平均值
注意: 聚合函数的计算排除了null值
解决方案: 选择不包含null的数据
2.6,分组查询
1,语法: group by 分组字段;
2,注意:
(1) 分组之后查询的字段: 分组字段,聚合函数
(2)where 和 having的区别
(1 where是在分组之前进行限定,如果不满足条件则不参与分组. having是在分组之后进行
限定,如果不满足结果则不会被查询出来
(2 where后不可以跟聚合函数,having可以进行聚合函数的判断
demo:
select sex,avg(math) from student group by sex;
select sex,avg(math),count(id) from student group by sex;
select sex,avg(math),count(id) from student where math>70 group by sex;
select sex,avg(math),count(id) from student group by sex having count(id)>2;
2.7,分页查询
1,语法: Limit 起始下标,长度; (其实下标没有指定默认从0开始,0表示表中第一条记录)
2,demo:
select * from student limit 3; - 排在前三的记录
select * from student limit 1,3; - 排在第二的记录开始取3个记录
select * from student limit 2,6; - 排在再三的记录开始取6个记录
3,limit是MySQL的一个方言
下标位置是从0开始算起的,0对应排在第一的记录,2表示排在第二的记录,类似于python里面的下标索引
8681

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



