【MySQL】-數據庫語句

'''3.1-查看版本和顯示當前時間'''
sql = "select version();"
sql = "select now();"
'''3.2-數據庫操作'''
show database;
use database_Name;database_Name爲數據庫名
select database();--database()爲數據庫的內置函數
create database 數據庫名 charset="utf8";
drop database;刪除數據庫
'''3.2-數據庫表操作'''
show tables;
desc 表名;--查看錶結構
'''3.2.1創建表'''
create table table_name(
	column1 datatype contrai,
	column2	datatype,
	...
	columnN datatype,
	PRIMARY KEY(one ore more columns)
);
'''3.2.2-修改表'''
--修改表-添加字段
alter table table_name add column2	datatype;
alter table students add birth datetime;
--修改表-修改字段-重命名
alter table table_name change 原名 新名 類型及約束;
alter table students change birth birthday datetime no null;
--修改表-修改字段-不重名版
alter table table_name modify 原名 類型及約束
alter table students modify birthday date not null;
--修改表-刪除字段
alter table 表名 drop 列名;
alter table students drop birthday;
'''3.3.3-刪除表及其他操作'''
--刪除表
drop table 表名;
drop table students;
--查看錶的創建語句
show create table 表名;
show create table students;
--重命名錶
rename table 舊錶名 to 新表名;
rename table students to stu;
'''3.3.4-表數據增刪改查'''
--查看所有列
select * from 表名;
select * from student;
--查詢指定列
select column1,column2,column3 from 表名;
select id,name from student;
--查詢,使用as給表起別名
select 字段名 from 表名 as 新表名;
select name from student as s;--給表起別名
select name as n from student;--給列起別名
--消除重複行
select distinct column1,... from 表名;
select distinct name from student;
--增加
insert into 表名 values(...);
insert into student values(null,"郭靖","襄陽城","男");--此處第一個爲自增長列,用null代替
--部分列插入,值的書序與給出的列對應
insert into 表名(column1,...) values(value1,...);
insert into student(name,address,gender) values("風清揚","後山洞","男");
--多行插入
insert into 表名(cloumn1,...) values(...),(...),...;
insert into student(name,address,gender) values("蕭峯","丐幫","男"),("蕭遠山","大遼","男"),("小龍女","活死人墓","女");
--修改
update 表名 set column1=values1,column2=values2... where 條件
update student set name="黃蓉",gender="女" where name="蕭遠山"
--刪除
delete from table_name where 條件;
delete from student where id=5;
--邏輯刪除,本質上爲更新
update table_name set isdelete = 0;
--清空數據(會把權標都清空,且自增長鍵爲1)
truncate table_name;

'''3.3數據庫備份與恢復'''
mysqldump -uroot -p 數據庫名 >shuju.sql--數據庫備份
--連接mysql,創建新的數據庫,準備恢復數據
--退出mysql鏈接,執行下面命令
mysql -uroot -p 新數據庫名<shuju.sql

'''3.4條件查詢'''
--查詢編號大於3的學生信息
select * from student where id>3;
--查詢姓名不是“小明”的學生信息
select * from student where name!="小明";
--查詢編號大於3的女生
select * from student where id>3 and gender="女";
--模糊查詢,查詢姓劉的學生
select * from student where name like "劉%";
--查詢姓劉切名字爲兩個字的學生信息
select * from student where name like "劉_";
--範圍查詢
select * from student where id in(1,3,8);
--查詢編號爲1-8的學生信息
select * from student where id between 1 and 8;
--查詢編號爲3-6的男生信息
select * from student where (id between 3 and 6) and gender="男";
--空判斷。注意null與""是不相同的,查詢沒有填寫身高的學生
select * from students where height is null;
--查詢填寫了身高的男生信息
select * from students where height is not null and gender="男";

'''3.5排序'''
--查詢男生信息,按照ID降序排列
select * from student order by id desc;
--顯示所有的學生信息,先按照年齡從大--》小排序,當年齡相同時按照身高降序
select * from student order by age desc,height desc;

'''3.6聚合函數'''
--查詢學生總數,使用count(*)
select count(*) from student;
--查詢女生的編號最大值
select max(id) from student where gender="女";
--查詢男生的編號最小值
select min(id) from student where gender="男";
--查詢男生的總年齡
select sum(age) from student where gender="男";
--查詢女生的平均年齡
select avg(age) from student where gender="女";
--四捨五入,查詢女生的平均年齡並保存兩位小數
select round(avg(age),2) from student where gender="女";

'''4、高級查詢'''
'''4.1-分組-group by'''
--根據性別分組
select gender from student group by gender;
--group by + group_concat(),根據分組結果使用group_concat()放置每一組的某字段的值集合
select gender,group_concat(name) from student group by gender;
--使用group_concat()和group by顯示相同名字的人的id號
'''4.2-group by +group_concat()'''
select name,group_concat(id) from student group by name;
'''結果如下:
name	group_concat(id)
哪吒	1
小龍女	6,9
蕭峯	4,7
蕭遠山	8
郭靖	2
風清揚	3
黃蓉	5
'''
--基於上面例子按照id排序,並且設置分隔符爲_
select name,group_concat(id order by id desc separator '_') as '合集' from student group by name;
'''結果如下:
name	合集
哪吒	1
小龍女	9_6
蕭峯	7_4
蕭遠山	8
郭靖	2
風清揚	3
黃蓉	5
'''
'''####函數介紹###
1、concat():用於連接兩個或多個數組。該方法不會改變現有的數組,而僅僅會返回被連接數組的一個副本。
2、concat_ws():和concat()一樣,將多個字符串連接成一個字符串,但是可以一次性指定分隔符~(concat_ws就是concat with separator)
	語法:concat_ws(separator, str1, str2, ...)
3、group_concat需要配合group by使用才能達到效果
'''
'''4.3-group by +聚合函數'''
--統計不同性別的人的平均年齡
select gender,avg(age) from student group by gender;
--統計不同性別的人的個數
select gender count(*) from student group by gender;

'''4.4 group by +having '''
--查詢不同性別人的數量,輸出數量大於2的性別
select gender,count(*) from student group by gender haing count(*)>2
'''4.5 group by + with roll up'''
--with rollup 的作用是在最後新增一行,來記錄當前列裏所有記錄的和
select gender,count(*) from student group by gender with rollup;
'''結果如下:
gender	count(*)
女		3
男		6
		9 
最後一行的 9,是累加的值
'''

'''4.4、分頁'''
--select * from 表名 limit start,count;
--查詢前三行男生信息
select * from student where gender="男" limit 0,3;
--查詢第N頁的數據
select * from student where id>3 limit (n-1)*m,m;

'''5、鏈接查詢'''
'''5.1 內連接查詢'''
--內連接查詢:查詢的結果爲兩個表匹配到的數據,即兩個表的公共數據
--查詢班級表與學生表
selct * from student inner join classes on students.cls_id=classes.id;
--查詢學生姓名及班級姓名
select s.name,c.name from student as s inner join classes as c on s.cls_id=c.id;

'''5.2 左連接查詢'''
--查詢的結果爲兩個表匹配到的數據,對於右表中不存在的值用null來填充
select * from student as s left join classes as c on s.cls_id=c.id;

'''5.3 右連接查詢'''
--查詢的結果爲兩個表匹配到的數據,對於左表中不存在的值用null來填充
select * from student as s right join classes as c on s.cls_id=c.id;

'''5.4 子查詢'''
--標量子查詢,子查詢的是一個數據(一行一列)
select * from student where age>(select avg(age) from student);
--查詢班級內還有學生的班級名稱(一列多行)
select name from classes where id in (select class_id from student);
--查詢班級年齡最大,身高最高的學生信息
select * from student where (height,age)=(select max(height),max(age) from student);


'''6 視圖'''
'''
視圖就是一條select語句執行的結果。
視圖就是若干張基本表的引用,一張虛表,查詢語句執行的結果,不存儲具體的數據(基本數據變化後,視圖也跟着變化)
'''

--定義視圖;建議視圖名命名時以"v_"開頭
create view 視圖名 as select語句;
--查看視圖
show tables;--也會顯示出所有的視圖來
--使用視圖
select * from 視圖表名;
--刪除視圖
drop view 視圖名;

'''7、事務'''
'''
所謂事務,它是一個操作序列,要麼都執行完畢,要麼都不執行。它是一個不可分割的單位。
事務的四大特徵:ACID:原子性(atomicity)、一致性(conssistency)、隔離性(isolation)、持久性(durability)
MySQL默認打開自動提交事務的方法,可以通過 SET AUTOCOMMIT=0;0 禁止自動提交;1 開啓自動提交;
'''
--顯示開啓事務
begin  --開啓事務方法一
start transaction  --開啓事務方法二
--回滾事務
rollback 
--提交事務
commit

'''8、索引'''
'''
索引是一種特殊的文件,有兩中索引:單列索引和多列索引.索引能夠大大提高查詢速度
一般是根據我們查詢語句的where 語句後面的字段列上建立索引
比喻:建立索引的表格就像是一輛蘭博基尼跑車,而不建立索引的表格就像是一個人力三輪車
索引的理解可以參考這篇文章 https://www.jb51.net/article/133625.htm
'''
--創建索引
create index 索引名稱 on 表名(字段名(字段長度))
--查看索引
show index from 表名;
--刪除索引
drop index  索引名稱 on 表名;

'''9、運行時間監控'''
--設置時間監控
SET RPFILING=0;--0 關閉時間監控;1 打開時間監控
--查看時間監控
SHOW PROFILES;	

'''10、MySQL賬戶管理'''
'''
在生產環境下操作數據庫時,絕對不可以使用root賬戶連接,而是創建特定的賬戶,授予這個賬戶特定的操作權限,然後連接進行操作,主要的操作就是數據的crud
MySQL賬戶體系:根據賬戶所具有的權限的不同,MySQL的賬戶可以分爲以下幾種
服務實例級賬號:,啓動了一個mysqld,即爲一個數據庫實例;如果某用戶如root,擁有服務實例級分配的權限,那麼該賬號就可以刪除所有的數據庫、連同這些庫中的表
數據庫級別賬號:對特定數據庫執行增刪改查的所有操作
數據表級別賬號:對特定表執行增刪改查等所有操作
字段級別的權限:對某些表的特定字段進行操作
存儲程序級別的賬號:對存儲程序進行增刪改查的操作
賬戶的操作主要包括創建賬戶、刪除賬戶、修改密碼、授權權限等
'''
--修改權限
grant 權限名稱 on 數據庫 to 賬戶@主機 with grant option;
--修改密碼
--使用root登錄,修改mysql數據庫的user表
--使用password()函數進行密碼加密
update user set authentication_string=password("新密碼") where user="用戶名";
--修改密碼後需要刷新權限:
flush privileges;
--刪除賬戶
--使用root登錄,然後將mysql數據庫中user表中所對應的要刪除的賬戶給刪除掉,然後刷新權限即可
delete from user where user="要刪除的賬戶名";

'''附錄:FAQ'''
1、提示您在使用安全的update模式,只能通過主鍵對錶進行修改
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.
SET SQL_SAFE_UPDATES = 0;0 不啓用;1 啓用

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章