MySQL:資料庫入門篇2

來源:互聯網
上載者:User

標籤:limit   dmi   des   from   auth   mys   table   order by   複雜條件   

#移除主鍵時需要先解除遞增,才能解除主鍵

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)

MySQL:資料庫入門篇2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.