欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL:数据库入门篇2

程序员文章站 2022-04-24 08:56:38
#移除主键时需要先解除递增,才能解除主键 alter table info modify id int null , drop PRIMARY key 一.用户权限 1.创建用户 create user 'hanshe'@'127.0.0.1' IDENTIFIED by '123'; -- 创建用 ......

#移除主键时需要先解除递增,才能解除主键

alter table info modify id int null , drop PRIMARY key

一.用户权限
1.创建用户
create user 'hanshe'@'127.0.0.1' IDENTIFIED by '123'; -- 创建用户
2.移除用户
drop user 'hanshe'@'127.0.0.1' ; -- 移除用户
3.修改用户
RENAME user 'hanshe'@'127.0.0.1' to 'hanxiaoqiang'@'192.168.0.1' -- 修改用户
4.查看授权
show GRANTS for 'hanshe'@'127.0.0.1';-- 查看用户 权限
5.授权
GRANT select,update ON db1.info to 'hanshe'@'127.0.0.1';-- 授权
GRANT all PRIVILEGES on *.* to 'hanshe'@'127.0.0.1'; -- 授权所有权限
6.移除授权
REVOKE all PRIVILEGES on *.* FROM 'hanshe'@'127.0.0.1'; -- 移除权限
7.开放外部访问权限

create user 'test'@'%' identified by '123';

GRANT all PRIVILEGES on *.* to 'test'@'%';

FLUSH PRIVILEGES; -- 刷新权限


二.修改用户密码
1.方式一:使用 mysqladmin 命令
mysqladmin -u用户名 -p原密码 password 新密码;

2.方式二:直接设置密码
set password for 'hanshe'@'%' = password('166')

3.方式三: 直接修改
update mysql.user set password = password('123') where user ='hanshe' and host ='%'

flush PRIVILEGES;
5.7 版本
update mysql.user set authentication_string = password('123') where user ='hanshe' and host ='%';

flush PRIVILEGES;

三.忘记密码怎么办(本地使用数据库)
1.关闭mysql服务
2.重新启动mysql服务并跳过权限表
3.直接通过mysql登录
4.修改密码
5.刷新


四单表查询
1.聚合函数
select sum(name),avg(age),max(age),min(age),count(name) FROM person;

2.分组
select sum(salary),dept_id from person GROUP BY dept_id

select sum(salary) as w ,dept_id from person GROUP BY dept_id HAVING w >20000

-- 查询每个部门的平均薪资 并且看看这个部门的员工都有谁?
select avg(salary),dept_id,GROUP_CONCAT(name) from person GROUP BY dept_id


#查询平均薪资大于10000的部门, 并且看看这个部门的员工都有谁?

select avg(salary),dept_id,GROUP_CONCAT(name) from person GROUP BY dept_id HAVING
avg(salary) >10000

3.分页

select * from person LIMIT 8,4
ps: limit (起始条数),(查询多少条数);


4.SQL 语句关键字的执行顺序

执行顺序: FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY ->limit 

五. 多表联合查询
select * from person p,dept d where p.dept_id = d.did -- 笛卡尔乘积
-- 多表联合查询
-- select * from person p,dept d where p.dept_id = d.did -- 笛卡尔乘积


-- -- 左连接查询
-- select * from person LEFT JOIN dept on person.dept_id = dept.did;
--
-- -- 右连接查询
-- select * from person RIGHT JOIN dept on person.dept_id = dept.did;
--
-- -- 内连接查询
-- select * from person INNER JOIN dept on person.dept_id = dept.did;


-- 全连接
select * from person LEFT JOIN dept on person.dept_id = dept.did
UNION
select * from person RIGHT JOIN dept on person.dept_id = dept.did;


select * from person LEFT JOIN dept on person.dept_id = dept.did
UNION all
select * from person RIGHT JOIN dept on person.dept_id = dept.did;


六、 复杂条件查询
-- 1. 查询出 教学部 年龄大于20岁,并且工资小于4000的员工,按工资倒序排列.
-- (要求:分别使用多表联合查询和内连接查询)

select did from dept where dname ='教学部';

select * from person where age>20 and
dept_id =(select did from dept where dname ='教学部') and salary <10000 ORDER by salary DESC

-- 2.查询每个部门中最高工资和最低工资是多少,显示部门名称

select MAX(salary),min(salary),dname from person
LEFT JOIN dept ON person.dept_id = dept.did GROUP BY dept_id


七.子语句查询
1.使用结果集作为表名查询
select * from (SELECT * from person) as aaa

-- 2.求最大工资那个人的姓名和薪水

select max(salary) from person;

select* from person where salary = (select max(salary) from person);

-- 3. 求工资高于所有人员平均工资的人员

select avg(salary) from person;

select * from person where salary >(select avg(salary) from person)