操作系统为windows
1 启动关闭mysql服务
//windows
mysqld --console //开启mysql服务
mysqladmin -uroot shutdown //关闭mysql服务
//linux(RPM安装方式)
netstat -nlp //查看mysql服务的状态
service mysql start //开启mysql服务
service mysql stop //关闭mysql服务
//linux(非RPM安装方式)
netstat -nlp //查看mysql服务的状态
./mysqld_safe & //开启mysql服务
mysqladmin -uroot shutdown //关闭mysql服务
2 登录和退出mysql
mysql -uroot -p //登录mysql
quit //退出mysql
SQL语句主要有三类:DDL、DML、DCL
2 DDL(Data Definition Languages)
数据定义语言,这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象。常用的语句关键字有:create、drop、alter等
3 DML(Data Manipulation Language)
数据操纵语句,用于添加、删除、更新和查询数据库记录,并检查数据完整性。常用的语句关键字有:insert、delete、update、select 等
4 DCL(Data Control Language)
数据控制语句,用于控制不同数据段直接的许可和访问级别的语句。这些语句定义了的数据库、表、字段、用户的访问权限和安全级别。常用语句关键字有:grant、revoke等
3 DDL(对数据库对象操作)
3.1 创建数据库
create database testZS; //创建数据库
show databases; //查看有哪些数据库
use testZS; //使用哪个数据库
show tables; //某个数据库下面有哪些表
3.2 删除数据库
drop database testZS;
3.3 创建表
use testZS; //表建在哪个数据库里面
create table emp(
ename varchar(10),
hiredate date,
sal decimal(10,2),
deptno int(2)
);
3.4 删除表
drop table emp;
3.5 修改表
//修改表名
alter table emp rename emp1;
//查看表结构
desc emp;
//增加表字段
alert table emp add column age int(3);
//删除表字段
alert table emp drop column age;
//修改表字段名称
alter table emp change age age1 int(4);
//修改表字段数据类型
alter table emp modify ename varchar(20);
//改变字段排列顺序
alter table emp add birth date after ename;
alter table emp add age int(3) first;
4 DML(对数据库对象的数据操作)
4.1 插入记录
insert into emp(ename,hiredate,sal,deptno) values('zs','2018-01-01','2000',1);
insert into emp(ename,sal,deptno) values('zs1','2000',2);
//批量插入
insert into emp(ename,hiredate,sal,deptno) values('zs2','2018-01-01','2000',3),('zs3','2018-01-01','5000',4);
4.2 删除记录
delete from emp where sal='5000';
//多表删除 多个表的删除的条件需要对方的情况下
delete from emp a,dept b where a.deptno=b.deptno and a.deptno=3;
4.3 更新记录
update emp set sal=4000 where ename='zs3';
//多表更新 这种更新是更新的情况需要根据另一个表作为依据来更新
update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.deptno=b.deptno;
4.4 查询记录
//1 查询所有字段,所有记录(条)
select * from emp;
//2 查询某些字段,所有记录(条)
select ename,deptno from emp;
//3 查询不重复的记录
select distinct deptno from emp;
//4 多条件查询
select * from emp where deptno=1;
//5 排序 desc 降序 asc 升序 默认升序
select * from emp order by sal;
select * from emp order by sal desc,deptno asc;
//6 限制记录数量 偏移量0开始
select * from emp limit 2; //前2条记录
select * from emp limit 1,2; //从第2条开始的2条记录(包括第二条)
//7 聚合 (统计)
select [field1,field2,...,fieldn] fun_name
from tablename
[where where_contdition]
[group by field1,field2,...,fieldn]
[with rollup]
[having where_condition]
fun_name:聚合函数
group by 分组
with rollup 是否对分类聚合后的结果进行再汇总
having 对分类后的结果进行条件过滤
select * from emp;
select deptno,count(1) from emp group by deptno;
select deptno,count(1) from emp group by deptno with rollup;
select deptno,count(1) from emp group by deptno having count(1)>1;
//空字符串'' 和null是不一样的。如果你输入的时候没有特别指定,存入就是null,但是如果指定'',则就是空字符串。
//常用所有聚合函数
sum(expr):求和
avg(expr):求平均数
count(expr):计数器,返回SELECT语句检索到的行中非NULL值的数 COUNT(DISTINCT),
max(expr) 获取最大值
min(expr) 获取最小值
GROUP_CONCAT(expr):按照分组,将expr字符串按逗号分隔,组合起来
//8 记录联合 union(去重)、union all
//查询所有字段,所有记录(条)
select * from emp;
//查询某些字段,所有记录(条)
select ename,deptno from emp;
//1 查询不重复的记录
select distinct deptno from emp;
//2 多条件查询
select * from emp where deptno=1;
//3 排序 desc 降序 asc 升序 默认升序
select * from emp order by sal;
select * from emp order by sal desc,deptno asc;
//4 限制记录数量 偏移量0开始
select * from emp limit 2; //前2条记录
select * from emp limit 1,2; //从第2条开始的2条记录(包括第二条)
//5 聚合 (统计)
select [field1,field2,...,fieldn] fun_name
from tablename
[where where_contdition]
[group by field1,field2,...,fieldn]
[with rollup]
[having where_condition]
fun_name:聚合函数
group by 分组
with rollup 是否对分类聚合后的结果进行再汇总
having 对分类后的结果进行条件过滤
select * from emp;
select deptno,count(1) from emp group by deptno;
select deptno,count(1) from emp group by deptno with rollup;
select deptno,count(1) from emp group by deptno having count(1)>1;
//空字符串'' 和null是不一样的。如果你输入的时候没有特别指定,存入就是null,但是如果指定'',则就是空字符串。
//!=2 不包括null,但是包括空字符串
//常用所有聚合函数
sum(expr):求和
avg(expr):求平均数
count(expr):计数器,返回SELECT语句检索到的行中非NULL值的数 COUNT(DISTINCT),
max(expr) 获取最大值
min(expr) 获取最小值
GROUP_CONCAT(expr):按照分组,将expr字符串按逗号分隔,组合起来
//6 表连接
内链接 查询匹配的情况
外连接 查询匹配和不匹配的情况 左连接、右连接
//7 内链接
select ename,deptname from emp,dept where emp.deptno=dept.deptno;
//左连接
select ename,deptname from emp left join dept on emp.deptno=dept.deptno;
//右连接
select ename,deptname from emp right join dept on emp.deptno=dept.deptno;
//子查询
select * from emp where deptno in(select deptno from dept);
1 限制
2 count(1),count(字段),count(distinct)
count(1) 所有记录统计值(包含null)
count(字段) 字段不为null的记录统计值(不包含null)
count(distinct) 不重复的记录(不包含null)
3 with rollup
group_concat
5 DCL(对数据库对象的权限操作) DBA干的活。略
6 视图
//创建视图
create or replace view viewname as
select * from emp;
//from后面不能有子查询
//修改视图------一般不用
//更新视图-----一般不用
//删除视图
drop viewname;
//查询视图定义
show create view viewname;
7 常用运算符
至于:优先级,没必要,用()这玩意解决问题,清晰明了!
8 常用函数
8.1 聚合函数
8.2 字符串函数
注意:lpad和rpad(如何填充的)
注意:repeat
注意:substring(从1开始)
8.3 数值函数
8.4 日期和时间函数
日期:年月日
时间:时分秒
当前日期,当前时间,当前日期和时间
日期和unix时间戳转换
date_format
%U和%u (不懂)
date_add
8.5 控制流函数
8.6 其它
查看当前用户
附录:(都去mysql官网上找吧~)
1 所有聚合函数:略
2 mysql中所有运算符:略
3 mysql中所有通配符:百分号、下划线和escape
mysql中的正则表达式:略