作為一名Java程式設計師或者作為後端開發工程師,MySQL資料庫可以說是Java開發人員必備的技能之一。想成為一名優秀後端工程師及往後架構師發展,是必須要和資料庫打交道的,對資料庫一定都要非常熟悉,因為我們大部分業務性能瓶頸都來自資料庫層,能否寫出高效SQL是提高性能的關鍵所在。因此,我個人覺得資料庫是程式設計師必須具備的技能。
MySQL基本命令
1、登錄資料庫系統:mysql -u root -p 回車下一步輸入密碼即可登錄
2、查詢現有資料庫:show databases
3、創建資料庫:create database [資料庫名]
4、刪除資料庫:drop database [需要刪除的資料庫名]
5、創建表:先選擇資料庫(use 資料庫名)再create table [表名]
6、創建表的主鍵:create table [表名] id int primary key,name varchar(10),age int(150); 注意逐漸語法為primary key
7、 查看表結構:describe [表名]
8、查看詳細結構:show create table [表名]
修改表語句
1、修改表名:alter table [舊錶名] rename [新表名]
2、修改欄位:alter table [表名] change [舊屬性名] [新的屬性名] [新的屬性類型]
3、新增欄位:alter table [表名] add [新屬性名] [新屬性類型]
4、刪除欄位:alter table [表名] drop [屬性名]
5、刪除表:drop table [表名]
SQL基本語句
1、查詢所有欄位:select * from [表名]
2、如按年齡排序升降(asc升序和desc降序): select * from [表名] where age>20 order by age desc;
3、查詢帶「IN」關鍵字,如年齡20到30:select * from [表名] where age in <20,30>;即可查詢
4、帶between and的範圍查詢:select * from [表名] where age between 20 and 25;意思是查詢年齡20到25歲的範圍
5、帶like的字符匹配查詢:select * from [表名] where username(欄位名) like '%科';
解釋通配符:%代表任意長度的字符串,可為0、a_b表示查詢以a開頭及b結尾、「_」代表任意字符。
6、查詢空值:select * from [表名] username(欄位名) is unll;
7、帶「or」的多查詢:select * from [表名] where age between 20 and 30 or username like'%科比%';
8、去重查詢:select distinct [欄位名] from [表名]
分組查詢
1、結合group_concat()查詢:select [欄位名],group_concat(欄位名) from [表名] group by [欄位名]
2、結合聚合函數數使用:select [欄位名],count(欄位名) from [表名] group by [欄位]
聚合函數查詢
1、count(記錄數):select count(age) from [表名]
2、求和:select sum(age) from [表名]
3、求平均:select avg(age) from [表名]
4、求最大值:select max(age) from [表名]
5、求最小值:select min(age) from [表名]
筆者從事大數據、Java後端開發的,如果你也是正在考慮學習或者這學習中遇到什麼問題,可以評論區留言或者私信,後續會更新關於大數據、Java開發的技術文章。
如想要獲得更多mysql教程?關注並私信我,並私聊發送「mysql」即可獲取mysql的精品教程。