一、SQL语句
DQL:语句查询 select(查)
DML:对表数据进行增删改 creat delete update (增删改)
DDL:对表结构进行增删改
TCL:事务控制
DCL:授权、撤销权限
二、MySQL语句or命令
1.语句:
select xxx,xxx; 5 (select distinct //去除重复记录 xxx from 去重记录) (有group by,select只能+分组字段+分组函数)
from 列名 as a; 1 (as:改名,as可省略)
where 过滤条件; 2 (where后不能接分组函数)
group by xxx,xxx; 3
having 再次过滤; 4 (与group by一起用)
order by 列名+asc(升序) / desc(降序); 6 (排序)
2.命令:
查看数据库::show databases;
创建数据库: creat database <数据库名>;
删除数据库:drop database <数据库名>;
连接数据库: use <数据库名>;
查看当前数据库:select database();
查看数据库中的表:show tables;
创建 / 删除表:create / drop table <表名>;
表插入数据:insert into <表名>
删除表中数据:delete from 表名 where 表达式
二、连接
1. 内连接:①等值连接:select e.name,d.name from emp e join dept d on e.age==d.age;
②非等值连接:select e.name,d.name from emp e join dept d on e.age between max(d.age) and min(d.age);
③自连接:一张表看作两张表
2. 外连接:①右外连接:不仅把等值查出来,还能把右表匹配不上的数据查出来
②左外连接:不仅把等值查出来,还能把左表匹配不上的数据查出来
例如:select ? from A(左边)left/right join on B(右边)
3.多张表连接:select ? from A join on B join on C (A先和B连接,A再和C连接)
4.子查询(多条mysql语句嵌套):①在select语句 (嵌套select语句只能查询一条记录)
②在from语句 ③在where语句