MySQL知识点复习
MYSQL學(xué)習(xí)
安裝:sudo apt install mysql-server
https://blog.csdn.net/qq_38505969/article/details/109957055
mysql -h 主機(jī)名 -p端口 -u 用戶(hù)名 -p密碼(注意p后面無(wú)空格)
或mysql -u 用戶(hù)名 -p密碼
或mysql -u 用戶(hù)名 -p
-u:用戶(hù)名,-p:密碼
退出:exit、quit、ctrl+d
查看mysql進(jìn)程:ps -aux | grep mysql
查看mysql服務(wù)狀態(tài):sudo service mysql status
停止mysql服務(wù):sudo service mysql stop
啟動(dòng)mysql服務(wù):sudo service mysql start
重啟mysql服務(wù):sudo service mysql restart
mysql配置文件路徑:/etc/mysql/mysql.conf.d/mysqld.cnf
查看當(dāng)前使用的數(shù)據(jù)庫(kù):select database();
刪除數(shù)據(jù)庫(kù):drop database 庫(kù)名;
查看數(shù)據(jù)庫(kù)創(chuàng)建信息:show create database 庫(kù)名;
修改數(shù)據(jù)庫(kù)字符集:alter database 庫(kù)名 charset=utf8;
簡(jiǎn)單創(chuàng)建數(shù)據(jù)表:create table user(id int,name char(7),age tinyint);
創(chuàng)建標(biāo)準(zhǔn)的數(shù)據(jù)表:create table person(id int(5) unsigned zerofill primary key auto_increment,name char(5) not null,height float(5,2) default 0.0,gender enum(‘男’,‘女’) default ‘男’);
查看表結(jié)構(gòu):desc 表名;
查看創(chuàng)建表的信息:show creat table 表名;
為表添加字段:alter table 表名 add weight float(5,2) not null;
為表修改類(lèi)型:alter table 表名 modify weight int(5);
修改表的字段名和約束:alter table 表名 change weight tizhong float(5,3) default 99.99;
當(dāng)字段類(lèi)型發(fā)生變化時(shí),如果原字段中有數(shù)據(jù),那么數(shù)字字符串和數(shù)字之間可以轉(zhuǎn)換
可同時(shí)修改多個(gè)字段和約束,中間用逗號(hào)分隔。
刪除表字段 alter table 表名 drop tizhong;
刪除表:drop table 表名;
查詢(xún):
? select * from person;
插入:
? insert into 表名 values(1,‘tom’,default,male),(2,‘Jack’,178,male); 如果不想提供數(shù)據(jù),也要使用default占位
? insert into 表名 (name) values(‘Lucy’),(‘Tom’) ;
? insert into 表名 (id,name,gender) values(5,‘Bulke’,1);
更新數(shù)據(jù):
? update 表名 set 字段名=值;
? update 表名 set 字段名=值 where 條件;
刪除數(shù)據(jù):
? delete from 表名;
? delete from 表名 where 條件;
別名:
? select id as 序號(hào),name as 姓名,gender as 性別 from person;(as 可以省略)
數(shù)據(jù)去重:
? select distinct name from person;
模糊查詢(xún):like,%(0或多個(gè)字符),_(一個(gè)字符),
? select * from person where name like ‘孫%’;
? select * from person where name like ‘孫_’;
? select * from person where name like ‘%孫%’;
范圍查詢(xún):between…and…(連續(xù)范圍內(nèi)),in(非連續(xù)的范圍)
? select * from person where height between 170 and 180;
? select * from person where id in (1,7,9);
空判斷:is null,is not null
? select * from person where height is null;
? 注意:where height is not null;和where not height is null;結(jié)果一樣但是執(zhí)行邏輯不一樣,后者是先把空數(shù)據(jù)取出來(lái)再反選。
排序:order by
? select * from 表名 order by 列名 asc:升序排列,不寫(xiě)asc默認(rèn)升序
? select * from 表名 order by 列名 desc:降序排列
? select * from 表名 order by 列1 desc,列2 asc:當(dāng)列一中出現(xiàn)相同值情況無(wú)法排序時(shí)使用列二排序
? where 條件 要寫(xiě)在order by 的前面
分頁(yè)查詢(xún):limit,start——索引起始位置,count——條數(shù)
? select * from 表名 limit start,count 從start+1開(kāi)始讀取count條數(shù)據(jù),start不寫(xiě)默認(rèn)為0,count不夠時(shí)有多少就顯示多少
? select * from student limit (n-1)*m,m 獲取第n頁(yè)數(shù)據(jù)
? select * from person where gender=‘女’ order by gender desc limit 5; 查詢(xún)性別為女的年齡最大的五個(gè)人
? where 條件——order by——limit
聚合函數(shù):用來(lái)對(duì)數(shù)據(jù)進(jìn)行統(tǒng)計(jì)和計(jì)算
? count(col):求指定列的總行數(shù)(不統(tǒng)計(jì)空值的行)、max(col)、min(col)、sum(col)、avg(col):求指定列的平均值
? round(),指定保留小數(shù)點(diǎn)的位數(shù),如round(avg(height),2)
? select count(height),max(height),min(height),sum(height),avg(height) from person;
? 一般都是使用count(*),可以將空值也統(tǒng)計(jì)進(jìn)去
ifnull(height,0):如果height為空,則用0代替,可與聚合函數(shù)連用,如count(ifnull(height,0));
分組查詢(xún):group by
? select gender from person group by gender; 通過(guò)什么分組就查詢(xún)什么——select A from 表名 group by A,此方法只能看到分組的類(lèi)別,看不到各組的成員情況
? group by 與group_concat()連用可以查看各組成員情況:select gender ,group_concat(name) from person group by gender;
? having過(guò)濾分組數(shù)據(jù)(having只能用于group by):select gender,group_concat(name) from person where age>50 group by gender having gender=‘女’; 顯示年齡大于50的女生成員姓名
? 與聚合函數(shù)連用:select gender,count(age),max(age),min(age),sum(age),avg(age) from person where age is not null group by gender;
? with rollup:select gender,count(age),max(age),min(age),sum(age),avg(age) from person where age is not null group by gender with rollup; 在最后記錄后面新增一行,顯示select查詢(xún)時(shí)聚合函數(shù)的統(tǒng)計(jì)和計(jì)算的結(jié)果
內(nèi)連接、左連接、右連接、自連接查詢(xún)時(shí)where必須寫(xiě)到連接語(yǔ)句后面
內(nèi)連接:inner join on 根據(jù)on后面的條件取兩個(gè)表的“交集”,on可省略
? select * from student inner join class; 這樣的話(huà)查詢(xún)出來(lái)的條數(shù)是student的條數(shù)于class條數(shù)之積
? select * from student inner join class on student.class_id=class.id; on 后跟條件
? select s.id,s.name,c.class_name class from student as s inner join class c on s.class_id=c.id; as可以省略
左外連接:left join on 以左表為主,根據(jù)條件查詢(xún)右表數(shù)據(jù),如果右表數(shù)據(jù)不存在則以null填充,on不可省略
? select 字段 from 左表 left join 右表 on 左表.字段1=右表.字段2;
右外連接:right join on 以右表為主,根據(jù)條件查詢(xún)左表數(shù)據(jù),如果左表數(shù)據(jù)不存在則以null填充,on不可省略
? select 字段 from 左表 right join 右表 on 左表.字段1=右表.字段2;
? 將左表和右表互換一下,right和left替換一下,可實(shí)現(xiàn)左連接查詢(xún)的結(jié)果和右連接查詢(xún)的結(jié)果相同
自連接:左表和右表是同一張表,內(nèi)連接、左連接、右連接都可以實(shí)現(xiàn)自連接
? select p.name 省名 c.name 市名 from areas p inner join areas c on p.id=c.pid; pid表示改地名的上級(jí)地名id
子查詢(xún):子查詢(xún)是一條完整的select語(yǔ)句,嵌入到主查詢(xún)中
? select * from student where age > (select avg(age) from student); 查詢(xún)年齡大于平均值的學(xué)生
? select name from class where id in (select distinct class_id from student where class_id is not null); 查詢(xún)學(xué)生都?xì)w屬于哪些班級(jí)
? select * from student where (age,height) = (select max(age),max(height) from student); 查找年齡最大且身高最高的學(xué)生
外鍵約束:foreign key(col) reference
? 對(duì)外鍵字段的值進(jìn)行更新和插入時(shí),會(huì)引用表中的字段進(jìn)行數(shù)據(jù)驗(yàn)證,如果數(shù)據(jù)不合法則會(huì)失敗,保證數(shù)據(jù)的有效性
? 一個(gè)表可以有多個(gè)外鍵
? 創(chuàng)建表時(shí)直接寫(xiě)入:create table Test(id int(5) not null primary key auto_increment,字段1 char(5),foreign key(字段1) references 表2(地段2)); 字段1和字段2類(lèi)型要一致,被參考的字段,即字段2一定是表2的主鍵
? 注意:定義外鍵后,要是想刪除被參考的那張表或者表中數(shù)據(jù),需要先把定義外鍵的表或者表中數(shù)據(jù)刪除,就好比如果想解散一家公司,要先解散員工。或者先把外鍵約束刪除了。
? 刪除外鍵約束:
? 1.獲取外鍵名稱(chēng)(系統(tǒng)自動(dòng)生成):show create table 表名; 查看外鍵名
? 2.刪除外鍵約束:alter table 表名 drop foreign key 外鍵名;
? 添加外鍵約束:如果字段1中存在無(wú)法與字段2匹配的數(shù)據(jù),則添加外鍵會(huì)失敗。
? alter table 表名 add constraint fk_class_id foreign key(字段1) references 表2(字段2); fk_class_id為外鍵名,constraint fk_class_id可以省略,省略后系統(tǒng)自動(dòng)生成外鍵名,外鍵名一般為fk_+字段名
將查詢(xún)結(jié)果作為數(shù)據(jù)插入到另一張表:
? insert into … select…; 不需要寫(xiě)values()
使用連接更新表中數(shù)據(jù):
? update 表1 inner/left/right join 表2 on…set…;
建表時(shí)通過(guò)查詢(xún)加入數(shù)據(jù):create table…select…
? create table 表名(id int primary key auto_increment,name char(10) not null) select name from 表名; 或者select 字段名 as name from 表名。id可以省略不寫(xiě),默認(rèn)從第二個(gè)字段開(kāi)始匹配,注意:select后跟的字段名必須和創(chuàng)建表時(shí)添加的字段名相同。
事務(wù):
? 事務(wù)是用戶(hù)定義的一系列執(zhí)行SQL語(yǔ)句的操作(增、刪、改),在一個(gè)事務(wù)中的所有操作要么完全執(zhí)行,要么完全不執(zhí)行(執(zhí)行到一半意外退出則會(huì)發(fā)生回滾),它是一個(gè)不可分割的工作執(zhí)行單元。
? 開(kāi)啟事務(wù)后執(zhí)行修改指令,變更的數(shù)據(jù)會(huì)保存到mysql服務(wù)端的緩存文件中,而不維護(hù)到物理表中,只有執(zhí)行commit之后才會(huì)將本地緩存文件中的數(shù)據(jù)提交到原物理表中,完成數(shù)據(jù)的更新。
? 注意:如果沒(méi)有顯示的開(kāi)啟一個(gè)事務(wù),每條sql語(yǔ)句都會(huì)被當(dāng)做一個(gè)事務(wù),并且由于mysql默認(rèn)采用自動(dòng)提交模式(autocommit),所以會(huì)自動(dòng)執(zhí)行提交事務(wù)的操作。
? set autocommit=0; 表示取消自動(dòng)提交事務(wù)模式,需要手動(dòng)commit完成事務(wù)的提交(當(dāng)開(kāi)啟一個(gè)事務(wù)后,會(huì)自動(dòng)設(shè)置autocommit值為0)。
? 顯示的執(zhí)行commit或rollback表示該事務(wù)的結(jié)束
? 四大特性:
? 原子性(Atomicity):強(qiáng)調(diào)一個(gè)事務(wù)中的多個(gè)操作是一個(gè)整體
? 一致性(Consistency):強(qiáng)調(diào)數(shù)據(jù)庫(kù)中保存一致的狀態(tài),例如ATM取錢(qián)時(shí)你取多少錢(qián),銀行卡中余額就要減少多少
? 隔離性(Isolation):強(qiáng)調(diào)數(shù)據(jù)庫(kù)中的多個(gè)事務(wù)之間的操作互不可見(jiàn)
? 持久性(Durability):強(qiáng)調(diào)數(shù)據(jù)庫(kù)能永久保存數(shù)據(jù),一旦提交就不可撤銷(xiāo)
? 只有InnoDB引擎可使用事務(wù),常用的表的存儲(chǔ)引擎是InnoDB和MyISAM,MyISAM不支持事務(wù),優(yōu)勢(shì)是訪問(wèn)速度快。
? 修改表的存儲(chǔ)引擎:alter table 表名 engine=‘MyISAM’;
事務(wù)的操作:
? sql語(yǔ)句:
? 開(kāi)啟事務(wù)begin;或start transaction;
? 提交事務(wù)commit;
? 回滾事務(wù)rollback;
? 提交或回滾事務(wù)后,當(dāng)前事務(wù)會(huì)結(jié)束
pymysql:
? 連接數(shù)據(jù)庫(kù)對(duì)象.cursor() 創(chuàng)建游標(biāo)對(duì)象,會(huì)自動(dòng)隱式的開(kāi)啟一個(gè)事務(wù)
? 提交事務(wù):連接數(shù)據(jù)庫(kù)對(duì)象.commit()
? 回滾事務(wù):連接數(shù)據(jù)庫(kù)對(duì)象.rollback()
索引:
? 在mysql中也叫做”鍵“,是一個(gè)特殊的文件,保存著數(shù)據(jù)表里所有記錄的位置信息,就像一本書(shū)的目錄,能加快數(shù)據(jù)庫(kù)的查詢(xún)速度。
? show index from 表名; 查看索引(主鍵列會(huì)自動(dòng)創(chuàng)建索引)
? alter table test_index add index idx_title(title); 創(chuàng)建索引(非常耗費(fèi)時(shí)間),非唯一性索引的名稱(chēng)一般設(shè)置為idx_+字段名,不寫(xiě)默認(rèn)是字段名,添加索引后,使用where查詢(xún)數(shù)據(jù)時(shí)只需要對(duì)比一次。
? 在查詢(xún)語(yǔ)句前面加desc可以通過(guò)查看rows屬性值來(lái)查看對(duì)比次數(shù),Extra屬性值查看查詢(xún)方法
? alter table test_index drop index idx_title; 刪除索引
? set profiling=1; 開(kāi)啟時(shí)間監(jiān)測(cè)
? show profiles; 查看時(shí)間監(jiān)測(cè)的記錄數(shù)據(jù)
聯(lián)合索引(復(fù)合索引):一個(gè)索引覆蓋表中兩個(gè)或者多個(gè)字段,一般用在多個(gè)字段一起查詢(xún)的時(shí)候
? alter table 表名 add index [索引名] (字段1,字段2,…);
? 最左原則:如果給(字段1,字段2,字段3)創(chuàng)建聯(lián)合索引,那么同時(shí)會(huì)給(字段1)、(字段1,字段2)創(chuàng)建索引
? 好處是減少了磁盤(pán)空間的開(kāi)銷(xiāo),缺點(diǎn)是耗費(fèi)時(shí)間長(zhǎng)
索引的使用原則:
? 1.對(duì)經(jīng)常更新的表避免創(chuàng)建較多的索引,因?yàn)楦滤饕浅:臅r(shí),對(duì)經(jīng)常查詢(xún)的字段應(yīng)該創(chuàng)建索引
? 2.數(shù)據(jù)量小的表最好不要?jiǎng)?chuàng)建索引
? 3.在同一字段相同值比較多時(shí)不要建立索引,例如“性別”字段
? 4.索引并不是越多越好,要在查詢(xún)速度和建立索引所占用的時(shí)間和空間之間綜合權(quán)衡
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的MySQL知识点复习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux知识点复习
- 下一篇: Python利用pymysql连接Mys