sql语句基础总结
2020-06-30 本文已影响0人
mutang
数据库的操作
-
链接数据库
mysql -uroot -p mysql -uroot -proot
-
退出数据库
exit/quit/ctrl+c(windows) exit/quit/ctrl+c(mac)
-
sql语句最后需要分号: ,不区分大小写
-
显示数据库版本
select version();
-
查看所有数据库
show databases;
-
显示时间
select now();
-
创建数据库
create database `python-04`; create database pythonNew charset=utf8; -- 注意: -- 诸如此类写法:'python-04'必须用tab键上面的符号括起来,用以表示整体,否则,会报错 -- 创建数据库时,指定编码集utf-8的写法,只能写utf8
-
查看创建数据库的语句
show create database pythonNew;
-
使用数据库
use pythonnew;
-
查看当前数据库
select database();
-
删除数据库:
drop database python04;
数据表的操作
-
查看所有的数据表
show tables;
-
创建表
create table xxx(id int, name varchar(30)); create table yyy(id int primark key auto_increment not null, name varchar(30)); -- 约束条件的顺序不限
-
查看数据表的名字
desc xxx;
-
查看创建数据表的名字
show create table 表名字;
-
修改表——添加字段
alter table 表名 add 列名 类型;
-
修改表——不重命名版
alter table 表名 modify 列名 类型及约束;
-
修改表——重命名版
alter table 表名 change 原名 新名 类型及约束;
-
修改表——删除字段
alter table 表名 drop 列名;
-
删除数据表
drop table 表名;
增删查改(crud)
-
增加:
-
全列插入
insert [into] 表名 values(...)
==注意==
-
主键自增长字段(列) 可以用0 null default来占位
-
| gender | enum('男','女','中性','保密') | YES | | 保密 | |
-- 枚举中 的 下标从1 开始 1---“男” 2--->"女"....
-
-
部分插入
insert into 表名(列名1……) values (值1……)
-
多行插入
insert into 表名(列名1,列名2) values(值1,值2),(值1,值2),…… -- 多行插入的部分插入 insert into 表名(列名1……) values(值1……),(……),…… -- 多行插入的全部插入
-
-
修改:
update 表名 set 列1 = 值1,列2 = 值2,…… where 条件;
-
查询基本使用
-
查询所有列
select * from 表名;
-
定条件查询
-
查询指定列
select 列1,列2 from 表名;
-
起别名(为列 、表):as
==注意==:别名起了,访问时必须用别名访问,否则,报错
-
-
删除:
-
物理删除(是真的没有了)
delete from 表名 where 条件;
-
逻辑删除:用一个字段来表示 这条信息是否已经不能再使用了
alter table students add is_delete bit default 0; update students set is_delete=1 where id = 6;
-
查询:
-
查询所有:
==distinct== 消除重复行
select distinct gender from students;
-
条件查询:
-
比较运算符:
!= 或< > -- 不等于
-
逻辑运算符
and or not
-
模糊查询(like)
% -- 替换一个或多个,也可以是零个 _ -- 替换一个
-
正则查询(rlike 正则表达式)
-
范围查询:
in (1, 3, 8) -- 表示在一个非连续的范围内 not in (……) -- 不非连续的范围值内 between …… and …… -- 表示在一个连续的范围内 not between …… and …… -- 表示不在一个连续的范围内 -- 错误示例: select * from students where age not (between 18 and 25); -- 错 -- 正确写法 select * from students where age not between 18 and 25); -- 对
-
空与非空
is null -- 空 is not null -- 非空
-
-
排序
order by 字段 asc -- 从小到大,即升序,默认值 desc -- 从大到小,即降序 order by 多字段
-
聚合函数
count -- 总数 max -- 最大值 min -- 最小值 sum -- 求和 avg -- 平均值 round(数值,小数位) -- 四舍五入,保留小数位 round(avg(*), 2)
-
分组
group by 字段 -- 错误示例: select * from students group by gender; -- 分析: -- select 后面跟分组后的唯一字段,因为分组后形成多个分组后的行,不唯一的字段可能有多个,无法确定
group_concat(字段……) -- 连接分组后字段里内容,默认直接连接,为防止连接紧密,不好分辨,使用分隔符连接 -- 例如: select gender, group_concat(name, "_", age, " ", id) from students where gender=1 group by gender;
having -- 用于分组后的约束
==having 和 where 的使用区别==
- 误区:不要错误的认为having和group by 必须配合使用。
- having是从前筛选的字段再筛选,而where是从数据表中的字段直接进行的筛选的
-
执行顺序:
==S-F-W-G-H-O 组合==
-
分页
limit start,count -- start 相当于下标,(第N页 - 1)* 每页的个数,每页的个数 -- count 个数
-
连接查询
inner join -- 查询的结果为两个表匹配到的数据 left join -- 查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充 right join -- 查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充
-
自关联
-
-子查询(实际过程子查询的速度稍慢)
-- 标量子查询
总结:
SELECT select_expr [,select_expr,...] [
FROM tb_name
[WHERE 条件判断]
[GROUP BY {col_name | postion} [ASC | DESC], ...]
[HAVING WHERE 条件判断]
[ORDER BY {col_name|expr|postion} [ASC | DESC], ...]
[ LIMIT {[offset,]rowcount | row_count OFFSET offset}]
]
- 完整的select语句
select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit start,count
- 执行顺序为:
- from 表名
- where ....
- group by ...
- select distinct *
- having ...
- order by ...
- limit start,count