数据库(3)
子查詢
即嵌套查詢,將一個select查詢結果當做一個單值,一個集合或一個臨時表對待,參與另一個select查詢
?
內置函數
分為2類:單行函數和聚合函數
單行函數是以一條記錄為單位進行處理,而聚合函數是用來處理多條記錄的(即分組, 和group by)
常用聚合函數:count,sum,max,min,avg等
?
distinct 去除重復列
having關鍵字,對分組的結果進行過濾處理,和group by子句一起使用,不能單獨使用
exists關鍵字:判斷一個集合是空還是非空
?
連接(Join)
連接操作就是在笛卡爾集的操作之上過濾
連接操作分為三類:
1.交叉連接:即無條件連接,即笛卡爾積
2.內連接:等值連接(使用=),不等值連接(>,<等),自然連接(特殊的等值連接,它在等值連接的基礎上去除重復的字段)
3.外連接:左外連接(left join,left outer join),右外連接(right join,right outer join),全外連接(full join,full outer join,左外連接和右外連接的并集)
?
視圖(View)
相當于一張虛表,針對不同用戶的需求呈現不同的數據,其本質就是將一個select查詢封裝為一張虛表
視圖和表一樣,都可以進行CRUD操作,但不建議對視圖進行DML(增刪改查)操作,因為可能會破壞數據的完整性
?
索引(Index)
提升查詢效率的數據庫對象,建議基于主鍵或唯一鍵創建索引
索引也會占存儲空間
優化:
select語句基于索引位置查找速度會快很多
創建索引基于唯一鍵或者主鍵創建
主鍵也可以用幾個字段,而不單單只能有一個
?
?
擴展
當成集合查詢
select st.sno, st.name, st.sex, st.phone from student st where sno not in
(select sno from score sc , course c where c.name='JAVA程序設計' and c.cno=sc.cno);
當成表查詢:
select st.sno, st.name, st.sex, st.phone, t.name, t.score from student st, (select sno, name, score from course c, score sc where name='Python程序設計' and c.cno=sc.cno and score>75) t where st.sno=t.sno order by score desc;
查詢出Python程序設計低于班級平均分的學生信息
select * from student st, course c, score sc where c.name='Python程序設計' and st.sno=sc.sno and c.cno=sc.cno and score<
(select avg(score) from course c, score sc where name='Python程序設計' and c.cno=sc.cno) order by score;
?
分別查詢出男生和女生的平均分
select sex,floor(avg(score)) from student st, score sc, course c where c.name='Python程序設計' and st.sno=sc.sno and c.cno=sc.cno
group by sex
按什么字段進行分組 即group by后面接的什么 select后面就只能接什么 其余的接不了 ,即此處只能接sex ,不能接st.name 等其他信息
?
查看班上有多少人
select count(*) from student;
查看男女各多少人
select sex, count(*) from student group by sex;
?
having的使用:
select sno, avg(score) avg_score from score group by sno having avg_score >75;
?
exists的使用:
如果有大于90分的就打印成績表,沒有就不打印
select * from score where exists(select * from score where score > 90)
?
無條件交叉連接:
select * from student, course; 等效于
select * from student join course;
等值連接
select * from student st join score sc on st.sno=sc.sno
自然連接:
select * from student st natural join score sc;
左外連接:滿足條件的留下來,此外左表中不滿足條件的也會留下來
此方法可以統計此student表中一門都沒考過的
select * from student st left join score sc on st.sno=sc.sno;
右外連接則相反
全外連接:
select * from student st full join score sc on st.sno=sc.sno;
關聯,刪除兩個表共有的信息時,會同時刪掉
create table test1(a int primary key);
create table test2(b int, foreign key(b) references test1(a) on delete cascade
insert into test1 values(1)
insert into test2 values(1)
delete from test1 where a =1;
創建視圖
create view python_class_info (sno, sname, sex, phone, cno, cname, score) as select st.sno, st.name, st.sex, st.phone, c.cno,c.name from student st, course c, score sc,where c.name='Python程序設計' and st.sno=sc.sno and
c.cno=sc.cno
刪除視圖
drop view python_class_info
select * from python_class_info;
update python_class_info set score=96 where sno=1001
?
設置索引
create index score_i on score(sno,cno)
?
?
?
selcet的查詢結果:
1.單值,
2.單值的集合,
3.臨時表
?
?
?
數據庫對象是持久化存在的,只要不刪
多表查詢就是數據庫的連接操作
netstat -tlnp 查看端口號 t:tcp l:listen n:以數字的形式顯示 p顯示建立相關鏈接的程序名
?
重置密碼mysql
vim /etc/my.cnf
在[mysqld]后面任意一行添加“skip-grant-tables”用來跳過密碼驗證的過程
systemctl restart mysqld
mysql ? 進入
接下來就是用sql來修改root的密碼
use?mysql
update?user?set?password=password("你的新密碼")?where?user="root";
flush?privileges;
quit
到這里root賬戶就已經重置成新的密碼了。
再編輯my.cnf,去掉剛才添加的內容,然后重啟MySQL。大功告成!
?
?
轉載于:https://www.cnblogs.com/yanruizhe/p/11379660.html
總結