MySQL基础语句
登录、退出
登录mysql
1 | mysql -h 主机地址 -u root -p |
退出mysql
1 | exit / quit |
操作数据库
查询所有数据库的名称
1 | show databases; |
创建数据库
1 | create database 数据库名称; |
删除数据库
1 | drop database 数据库名称; |
使用数据库
1 | use 数据库名称; |
操作表
查询某个数据库中所有的表名称
1 | show tables; |
查询表结构
1 | desc 表名称; |
创建表
1 | create table 表名称 |
数据库类型
1 | 1.int:整数类型 //age int |
修改表
1 | alter table 表名称 rename to 新的表名称; //修改表名称 |
删除表
1 | drop table 表名称; //删除表 |
增删查改
添加数据
1 | insert into 表名称(列名1,列名2,...,列名n) values(值1,值2,...,值n); |
删除数据
1 | delete from 表名称 where 条件; |
修改数据
1 | update 表名称 set 列名1 = 值1,列名2 = 值2,...,列名n = 值n where 条件; |
查询数据
1 | select 字段列表 from 表名列表 where 条件列表 group by 分组字段 having 分组之后的条件 order by 排序 limit 分页限定; |
一、基础查询
1 | 1.select name,age from student; //多个字段的查询 |
二、条件查询
- where子句后跟条件
- 运算符
>、<、>=、<=、=、<>select * from student where id > 10;
between ... and ...select * from student where age between 20 and 30;
in (集合)select * from student where age in (22,18,25);
like- 占位符
_代表单个任意字符,%代表多个任意字符 select * from student where name like '马%';select * from student where name like '_化%';select * from student where name like '___';select * from student where name like '%马%';
- 占位符
is nulland / &&or / ||not / !
三、排序查询
- 语法:order by 子句
- order by 排序字段 排序方式
- 升序:ASC(默认),降序:DESC
select * from student order by math asc, english asc;
四、聚合函数
- count:计算个数
select count(name) from student;select count(*) from student;
- max:计算最大值
select max(math) from student;
- min:计算最小值
select min(math) from student;
- sum:计算和
select sum(math) from student;
- avg:计算平均值
select avg(math) from student;
五、分组查询
- 语法:group by 分组字段 having 分组之后的条件
- 分组查询示例
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 where math > 70 group by sex having count(id) > 2;select sex, avg(math), count(id) 人数 from student where math > 70 group by sex having 人数 > 2;
六、分页查询
- 语法:limit 开始的索引,每页查询的条数;(针对mysql)
- 分页查询示例
select * from student limit 0,3; //查询第一页的3条数据select * from student limit 1,3; //查询第二页的3条数据
约束
1、主键约束:primary key
1 | 1、添加 |
2、非空约束:not null
1 | 1、创建表时添加约束 |
3、唯一约束:unique
1 | 1、添加 |
4、外键约束:foreign key
1 | 1、创建表时可以添加外键 |
多表查询
1、内连接查询
- 隐式内连接:使用where条件消除无用的数据
select * from emp,dept where emp.dept_id = dept.id;select emp.name,emp.gender,dept.name from emp,dept where emp.dept_id = dept.id;
- 显示内连接
- 语法:
select 字段列表 from 表名1 inner join 表名2 on 条件; select * from emp [inner] join dept on emp.dept_id = dept.id;
- 语法:
2、外连接查询
- 左外连接
- 语法:
select 字段列表 from 表1 left [outer] join 表2 on 条件; select t1.*,t2.name from emp t1 left join dept t2 on t1.dept_id=t2.id;- 查询的是左表所有数据以及其交集部分
- 语法:
- 右外连接
- 语法:
select 字段列表 from 表1 right [outer] join 表2 on 条件; - 查询的是右表所有数据以及其交集部分
- 语法:
3、子查询
- 概念:查询中嵌套查询,称嵌套查询为子查询
- 情况1:子查询的结果是单行单列的
子查询可以作为条件,使用运算符去判断,运算符 > < >= <= = - 情况2:子查询的结果是多行单列的,使用运算符in来判断
- 情况3:子查询的结果是多行多列的,子查询可以作为一张虚拟表
1 | #子查询情况1 |
事务
数据库隔离级别
- READ UNCOMMITTED(读未提交)
- READ COMMITTED(读已提交)
- REPEATABLE READ(可重复读)
- SERIALIZABLE(可串行化)
查看数据库隔离级别
1 | select @@tx_isolation; // 当前会话 |
mysql数据库事务开关(临时有效)
1 | set autocommit = 1; // 开启自动提交事务 |
修改数据库的事务级别
1 | set global transaction isolation level read uncommitted; //全局的 |
事务操作
1 | -- 开启事务 |