DCL
管理用户
-
查询用户
use masql; select * from user;
-
创建用户
create user'用户名@主机名' identified by '密码';
-
修改用户密码
alter user '用户名@主机名' identified with mysql_native_password by '新密码';
-
删除用户
drop user '用户名@主机名';
举例如下
-- 创建用户ticast 只能再当前主机iocalhost访问,密码123456 create user 'itcast'@'localhost' identified by '123456'; -- 修改用户密码为1234 alter user 'itcast'@'localhost' identified with mysql_native_password by '1234'; -- 删除用户 drop user 'itcast'@'localhost';
权限控制
-
查询权限
show grants for '用户名'@'主机名';
-
授予权限
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';
-
撤销权限
revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';
举例说明
-- 查询权限 show grants for 'itcast'@'localhost' ; -- 授予权限 grant all on test.* to 'itcast'@'localhost'; -- 撤销用户权限 revoke all on test.* from 'itcast'@'localhost';
函数
字符串函数
举例如下
-- 字符串拼接
select concat('hello ','mysqol');
-- lower 转小写
select lower('Hello');
-- upper 转大写
select upper('Hello');
-- lpad 左填充
select lpad('01',5,'-');
-- rpad 右填充
select rpad('02',6,'_');
-- 去掉头尾部的空格
select trim(' 成功 ');
-- 返回字符串str从start位置起n个位置长度
select substr('1234565454',4,'3');
数值函数
举例如下
-- ceil 向上取整
select ceil(0.25);
-- floor 向下取整
select floor(4.99);
-- 返回x/y的模
select mod(7,3);
-- rand 返回0-1内的随机数
select rand();
-- 求参数x的四舍五入值,保留y位小数
select round(4.59897845,2);
日期函数
举例如下
-- 返回当前日期
select curdate();
-- 返回当前时间
select curtime();
-- 返回当前日期和时间
select now();
-- 返回指定date的年份
select year(now());
-- 返回指定date的月份
select month(now());
-- 返回指定date的日期
select day(now());
-- 返回起始时间date1到终止时间date2中间的天数
select datediff('2022-2.28','2021-6-30');
-- date1加上一个时间后的时间
select date_add(now(),INTERVAL 90 year );
流程函数
-- case 查询员工工作地址,如果是北京上海就返回一线城市,其他返回二线城市
select
name,
(case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end) as '工作地址'
from emp;
学生成绩判断
select
id,
name,
(case when math >=85 then '优秀' when math>=60 then '及格' else '不及格' end ) '数学成绩',
(case when chinese>=85 then '优秀' when chinese>=60 then '及格' else '不及格' end ) '语文成绩',
(case when english >=85 then '优秀' when english>=60 then '及格' else '不及格' end ) '英语成绩'
from score;