#目录 :
##一 . SQL预备
##二 . DQL查询语言####1 . 基础查询
####2 . 条件查询
####3 . 模糊查询
####4 . 排序查询
####5 . 常见函数######5.1 单行函数
######5.2 分组函数(聚合函数)
####6 . 分组查询
####7 . 连接查询
######7.1 内连接
######7.2 外连接
####8 . 子查询
######8.1 where或having后的子查询
######8.2 select后的子查询
######8.3 from后的子查询
######8.4 exists后的子查询(相关子查询)
####9 . 分页查询
####10 . 联合查询
##三 . DML操作语言
####1 . 插入语句
####2 . 修改语句
####3 . 删除语句
##四 . DDL定义语言
####1 . 库的创建
####2 . 库的修改
####3 . 库的删除
####4 . 表的创建
######4.1 字段类型
######4.2 约束
####5 . 表的修改
####6 . 表的删除
####7 . 表的复制
一 . S Q L 预备 一 . SQL预备 一.SQL预备1
- DB : 数据库保存一组有组织的数据容器
- SQL : 结构化查询语言
启动服务 net start mysql
停止服务 net stop mysql
登录 mysql -h 主机名 -P 端口号 -u 用户名 -p密码
退出 exit
show databases;
use 库名;
show tables;
show tables from 库名;不换库
select database();
create table 表名(字段1 字段类型,字段2 字段类型);
desc 表名;
select * from 表名;
select version();查看版本
- SQL 语法不区分大小写,关键字大写,表名字段名小写
- 命令以分号结尾
- – 注释
- /* 多行注释*/
二 . D Q L 查询语言 二 . DQL查询语言 二.DQL查询语言2
###1 . 基础查询
select distinct 字段1 as 别名1,字段2 as 别名2 ... from 表名; -- as可省略
select concat(字段1,字段2,字段3) as 别名 from 库名;
select ifnull(字段名, 返回值) as 别名;
###2 . 条件查询
- 条件运算符 > < = >= <= <>
- 逻辑运算符 and or not
###3 . 模糊查询
- like – 配合通配符
- % – 任意个字符
- _ – 单个字符
- \ – 转义
- escape – 指定转义
- [not] between 区间头 and 区间尾 – (含首尾,不可交换区间首尾)
- in (值1,值2…)
- is [not] null
- <=> – 安全等于
###4 . 排序查询
order by 排序字段1(可以是表达式,别名) asc | desc (默认asc),排序字段2 [desc]
###5 . 常见函数
####5.1 单行函数
#####5.11 字符类
- concat() – 拼接
- length() – 字节长度
- upper() – 转大写
- lower() – 转小写
- substr(原字符串,初始索引,截取长度) – 截子串
- instr(原字符串,子串) – 子串第一次出现索引,无则0
- trim() – 去前后空格
- trim(指定字符 from 原字符串) – 去前后指定字符
- lpad(原字符串,总长度,指定填充符) – 左填充
- rpad() – 右填充
- replace(原字符串,要换字符串,替换字符串) – 替换
#####5.12 数学类 - round(值,精度) – 四舍五入
- ceil() – 向上取整
- floor() – 向下取整
- truncate() – 截断
- mod() – 模
- rand() (0,1] – 随机数
#####5.13 日期类 - now() – 当前系统日期+时间
- curdate() – 当前系统日期
- curtime() – 当前系统时间
- year() month() monthname() day() hour() minute() second()
– 年 月 英文月 日 时 分 秒 - str_to_date() – 日期转换
- date_format() – 日期格式化
- datediff(被减数日期,减数日期) – 日期差天数
#####5.14 系统类 - version() – 当前版本
- database() – 当前库
- user() – 当前用户
- password(‘字符’) – 加密
- md5(‘字符’) – md5加密
#####5.15 条件分支
if(真值表达式,真执行,假执行) 条件执行
case 条件表达式
when 常量1 then 返回值1
when 常量2 then 返回值2
...
else 返回值n+1
end
case
when 条件1 then 返回值1
when 条件2 then 返回值2
...
else 返回值n+1
end
####5.2 分组函数(聚合函数)
sum([distinct] 数值) -- 非空和
avg(数值) -- 非空均值
max(字段) -- 非空最大值
min(字段) -- 非空最小值
count([distinct] 字段) -- 非空计数
count(*|1) -- 快速统计总行数
###6 . 分组查询
select 分组函数,分组字段...
where 分组前筛选条件,数据源和字段为原始表
group by 分组字段...
having 分组后筛选条件,数据源为分组后的结果集,字段为select后的字段
order by 排序字段 [desc];
###7 . 连接查询(多表查询)
- 笛卡尔积 : 多表直接查询结果行数为
行1 * 行2 * ...
####7.1 内连接
#####7.11 等值连接
select 别名1.字段1,别名2字段2
from 表1 别名1,表2 别名2(表改了别名,select若带表名则必须用别名)...
where 别名1.字段1=别名2.字段2
and ...
and 筛选条件
group by 分组字段
having 筛选条件
order by 排序字段;
#####7.12 非等值连接
select 字段1,字段2
from 表1 别名1,表2 别名2
where 别名1.字段11 between 别名2.字段21 and 别名2.字段22
and ...;
#####7.13 自连接
select 别名1.字段1,别名1.字段2...,别名2.字段1,别名2.字段2...
from 表名 别名1,表名 别名2;
7.15 内连接 inner join (表交集)
select 字段1,字段2...
from 表1 别名1
[inner] join 表2 别名2
on 连接条件1(等值条件|不等值条件)
[inner] join 表3 别名3
on 连接条件2
...
where 筛选条件
group by 分组字段
having 筛选条件
order by 排序字段
...;
####7.2外连接(主表并集)
#####7.21 左连接 left [outer] join
select 字段1,字段2...
from 主表 别名1
left join 从表 别名2
on 连接条件
where 别名2.id is null
...;
#####7.22 右连接 right [outer] join (交换连接表顺序等同于左连接)
select 字段1,字段2...
from 从表 别名1
right join 主表 别名2
on 连接条件
where 别名1.id is null
...;
#####7.23 全外连接 full [outer] join (全表并集)
select 字段1,字段2
from 表1 别名1
full join 表2 别名2
on 连接条件
where 别名1.字段1 is null or 别名2.字段2 is null
...;
#####7.24 交叉连接 cross join (笛卡尔积)
select 字段1,字段2
from 表1 别名1
cross join 表2 别名2;
###8 . 子查询
- select 仅标量子查询(单行单列)
- from 仅表子查询(多行多列)
- where | having 标量子查询
- 列子查询(单列)
- 行子查询(单行)
- exists 表子查询
####8.1 where或having后的子查询
#####8.11 标量子查询(搭配> < = >= <= <>)
select 字段
from 表名
where 字段 > < = >= <= <>(
select 字段 from 表 where 字段=值
)
group by 字段
having 字段 > < = >= <= <> (标量子查询);
#####8.12 列子查询(搭配in,not in,any,some,all)
select 字段
from 表名
where 字段 [not] in(
select [distinct] 字段
from 表名
where 条件
)
...;
in() 等价于 =any()
not in() 等价于 <>all()
>any() 等价于 >min()
>all() 等价于 >max()
<any() 等价于 <max()
<all() 等价于 <min()
#####8.13 行子查询
select 字段
from 表名
where (字段1,字段2...) = > < >= <= <> (
select min(字段1),max(字段2)...
from 表名
...
)
...;
####8.2 select后的子查询
select 字段2,(
select 字段1 from 表1 where 筛选条件
)
from 表2;
####8.3 from后的子查询
select 别名1.*,别名2.字段
from (
select 字段1,字段2
from 表1
group by 分组字段
) 别名1
inner join 表2 别名2
on 表1.字段1 between 字段3 and 字段4
...;
####8.4 exists后的子查询(相关子查询)
select exists(select...);
-- 子查询结果存在,查询结果为1
-- 子查询结果不存在,查询结果为0
select 字段
from 表
where exists(select...);
###9 . 分页查询
select ...
...
limit offset(起始索引从0开始,0可省略),size(条目个数);
limit (page-1)*size size -- 分页公式
###10 . 联合查询
select ...
...
union [all] -- 带all不去重
select ... -- 每个查询列数必须相同,以第一个select字段命名
...
union
...;
三
.
D
M
L
操作语言
三 . DML操作语言
三.DML操作语言3
###1 . 插入语句
insert into 表名(字段1,字段2...)
values(值1,值2...),(...),(...)...;
非空字段必须插值,顺序不限,字段名可省略
insert into 表名 select...;
insert into 表名 set 字段1=值1,字段2=值2...;
###2 . 修改语句
update 表名 set 字段1=新值1,字段2=新值2... where 筛选条件;
update 表1 别名1
inner | left | right join 表2 别名2
on 连接条件
set 字段1=值1,字段2=值2...
where 筛选条件;
###3 . 删除语句
delete from 表名 where 筛选条件;
delete [别名1] , [别名2]
from 表1 别名1
inner | left | right join表2 别名2
on 连接条件
where 筛选条件;
- delete无法删除自增长断点,有返回值,可以回滚,truncate相反
truncate table 表名;
四
.
D
D
L
定义语言
四 . DDL定义语言
四.DDL定义语言4
###1 . 库的创建
create database if not exists 库名;
###2 . 库的修改
rename database 库名 to 新库名;
alter database 库名 character set 字符集;
###3 . 库的删除
drop database if exists 库名;
###4 . 表的创建
create table 表名(字段名1 字段类型 [长度] [约束],...);
####4.1 字段类型
#####4.11 整型
- unsigned – 设置无符号类型 , 默认有符号长度11
- zerofill – 不够长度填充0
#####4.12 浮点型 - float(M,D) , double(M,D)
- **dec(M,D) , decimal(M,D)**默认(10,0)
- – M有效数字长度 , 小数部位长度 , 均可省略
#####4.13 字符型 - char(字符数) , varchar(字符数) , text , blob
#####4.14 枚举型 - enum(枚举列表) – 任选一个插入
- set(集合列表) – 任选任意个插入
#####4.15 日期和时间类型 - date – 只保存日期
- datetime – 范围大
- timestamp – 时间戳,受时区影响
- time – 只保存时间
- year – 只保存年
####4.2 约束 - 待补充
###5 . 表的修改
####5.1 修改表字段名
alter table 表名 change [column] 旧字段 新字段 新字段类型;
####5.2修改表类型或约束
alter table 表名 modify column 字段 字段类型或约束;
####5.3添加新字段
alter table 表名 add column 新字段 字段类型;
####5.4删除字段
alter table 表名 drop column 字段名;
####5.5修改表名
alter table 表名 rename to 新表名;
###6 . 表的删除
drop table if exists 表名;
###7 . 表的复制
####7.1 仅复制表结构
create table 新表名 like 原表名;
####7.2 表的结构和数据都复制
create 新表名 select 字段 from 原表名where...;
####7.3 复制表的部分字段
create table 新表名 select 字段 from 原表名 where 0;