五、數據查詢語言(DQL) (重中之重) 六、事務控制語言(TCL) ...
五、數據查詢語言(DQL) (重中之重)
完整語法格式:
select 表達式1|欄位,....
[from 表名 where 條件]
[group by 列名]
[having 條件]
[order by 列名 [asc|desc]]
[limit 位置,數量]
<1>普通查詢
select 查詢表達式;//最簡單的sql語句,是一個函數
select database(); select version(); select now();
<2>條件查詢
where 條件表達式,支持運算符和函數
MySQL支持的運算符:
=、!=、>、>=、 < 、 <= 、 <>
and、or、not
is null、is not null
between...and..... 區間查詢 多少到多少之間
in(set);
like 通配符和占位符: % _ (模糊查詢)
%:表示0個或者多個字元
_:表示占位一個
-- 查詢所有的老師信息
select * from teacher;
-- 查詢id大於2的老師信息
select * from teacher where id>2;
-- 查詢姓名為空的老師信息 在資料庫中null永遠的不等於null,那麼怎麼去判斷null值呢? 通過is null、is not null
select * from teacher where name is not null
-- 查詢id為1 並且 姓名是“xiaosi”的老師信息
select * from teacher where id=1 and name ='xiaosi';
-- 查詢id為1 或者 姓名是“xiaosi”的老師信息
select * from teacher where id=1 or name ='xiaosi';
-- 查詢薪水在2000到10000之間的老師信息
select * from teacher where sal >=2000 and sal<=10000;
select * from teacher where sal between 2000 and 10000; # 這種方式等同於上面這種方式
-- 查詢姓名中有“拿”字的老師信息
select * from teacher where name like '%拿%';
-- 查詢姓名是三個字的
select * from teacher where name like '___';
-- 查詢姓“小”的老師信息
select * from teacher where name like '小%';
-- 查詢名字中含有下劃線的老師
select * from teacher where name like '%\_%';
<3>分組查詢
[group by 列名] [having 條件]
一般情況分組查詢結合聚合函數一起使用
(max()、min()、sum()、avg()、count())
-- 查詢每個部門的平均薪資
select dname,avg(sal) from teacher group by dname
-- 查詢部門平均薪資大於5000的部門
select dname,avg(sal) from teacher group by dname having avg(sal)>5000
select dname,avg(sal) asal from teacher group by dname having asal>5000
記住:分組的正確使用方式,group by 後面沒有出現的列名不能出現在select 和from的中間,雖然不報錯但是不是分組的正確使用方式。 聚合函數中出現的列名group by後面沒有無所謂。
<4>排序查詢
語法格式:order by 列名 asc|desc 預設升序(asc)
-- 查詢老師信息,要求根據薪資從大到小進行排序
select * from teacher order by sal desc 根據sal進行降序排序
select * from teacher order by sal asc 根據sal進行升序排序
select * from teacher order by sal 根據sal進行升序排序,利用預設排序
<5>限制結果集數量的查詢(分頁)
編號 商品名稱 商品價格 操作
1 大拿娃娃 100.0 刪除 修改
2 呂呂娃娃 200.0 刪除 修改
3 叢浩娃娃 350.0 刪除 修改
.....................
首頁 上一頁 1 2 3 4 5 下一頁 尾頁
語法格式:
limit n條數; ------從第一條開始取n條數據。(瞭解)
語法格式:
limit start開始下標索引,count條數; ---從起始位置start取count條數據(起始位置從0開始) 推薦使用
分頁(每頁顯示2條數據)
第一頁: select * from teacher limit 0,2;
第二頁: select * from teacher limit 2,2;
第三頁: select * from teacher limit 4,2;
第四頁: select * from teacher limit 6,2;
第五頁: select * from teacher limit 8,2;
分頁公式:
開始下標索引(起始位置) = (當前頁-1)*每頁顯示條數;
擴展:
別名
select * from teacher; -- 查詢表中所有欄位記錄
select name,sal,dname from teacher; -- 查詢表中指定欄位記錄
-- 給查詢的欄位設置別名 同時也可以給表設置別名 通過as 關鍵字實現別名
select name as '姓名',sal '薪資',dname '部門名稱' from teacher
六、事務控制語言(TCL)
MySQL事務預設自動開啟的。
在MySQK資料庫中只有使用Innodb資料庫引擎的數據表或庫才支持事務。
通過事務來管理 insert、update、delete語句
事務必須滿足4個條件(ACID):
原子性:要麼全部完成,要麼全部不完成,,不會結束在中間的某個環節。在執行的過程中一旦
出現錯誤/異常,會被回滾(Rollback)到事務開始前的狀態,就像這個事務從來沒有執行過一樣。
一致性:事務處理前後數據保持一致。
隔離性:事務處理必須是獨立的彼此隔離
持久性:事務對數據的修改被永久保存
<1>為什麼使用事務
銀行轉賬:老王給大拿轉賬30000元
事務廣泛使用:訂單系統,銀行系統等.....
<2>MySQL事務控制
commit(提交)、rollback(回滾)、savepoint(事務節點)
<3>實戰操作
create table student(
id int,
name varchar(32),
age int,
money double
);
insert into student values (1,'老王',18,60000);
語法:
set autocommit = false|true;//設置事務的提交方式
rollback;//事務回滾
commit;//事務提交
savepoint 節點名稱;//設置回滾的節點
rollback to 節點名稱;//回滾到具體的某個節點。
例如:
set autocommit = false;//設置事務手動提交
delete from student where id=1;//刪除id為1 的信息
rollback;//事務回滾
commit;//事務提交
update student set money = money-30000 where id=1;
savepoint t1;//設置事務節點
update student set money = money-20000 where id=1;
rollback to t1;//回滾到t1節點位置
commit;////事務提交
