mysql 事务 数量_MySQL 数据查询语言(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=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 '%_%'; # 錯誤
select * from teacher where name like '%\_%';
<3> 分組查詢
語法格式:
[group by 列名] [haveing 條件]
一般情況分組查詢結合聚合函數一起使用
max()
min()
sum()
avg()
count()
-- 查詢每個部門的平居薪資
# select * from teacher GROUP BY dname
# 記住:分組的正確使用方式,group by 后面沒有出現的列名不能出現在select 和from 的中間,
# 雖然不報錯,但是不是分組的正確使用方式
# 聚合函數中出現的列名group by后面沒有無所謂
select dname from teacher GROUP BY dname;
select dname, avg(sal) from teacher GROUP BY dname;
<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?????????玩具飛機????300.0?????????刪除 修改 ................................ 首頁????上一頁????1 2 3 4 5????下一頁???尾頁
語法格式
limit n條數;-------從第一條開始取n條數據(了解)
limit start開始下標索引,count條數; ---- 從起始位置start取count條數據(起始位置是從0開始的) (推薦使用)
分頁(每頁顯示兩條數據)
第一頁: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)*每頁顯示條數;
-- 每頁顯示3條
-- 顯示第二頁
select * from teacher limit 3,3;
<6> 擴展
別名
select * from teacher; # 查詢表中所有字段記錄
select name, sal, dname from teacher; # 查詢表中指定字段記錄
-- 給查詢的字段設置別名 同時也可以給表設置別名 通過as 關鍵字實現別名
select name as '姓名', sal '薪資', dname '部門名稱' from teacher
二、 事務控制語言(TCL)
MySQL事務默認自動開啟的
在MySQL數據庫中只有使用了Innodb數據庫引擎的數據表或庫才會支持事務
通過事務來管理 insert、update、delete語句
事務必須滿足4個條件(ACID):
原子性(不可分割性): 要么全部完成,要么全部不完成,不會結束在中間的某個環節。在執行的過程中一旦出現錯誤/異常,會被回滾(Rollback)到事務開始前的狀態,就像這個事務從來沒有執行過一樣。
一致性: 事務處理前后數據保持一致
隔離性: 事務處理必須是獨立的彼此隔離
持久性: 事務對數據的修改永久保存
<1> 為什么使用事務
銀行轉賬
事務廣泛使用:訂單系統,銀行系統等....
<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);
select * from student;
rollback;
手動關閉事務提交語法:
set autocommit = false|true; // 設置事務的提交方式
rollback; // 事務回滾
commit; // 事務提交
savepoint 節點名稱; // 設置回滾的節點
rollback to 節點名稱; // 回滾到具體的某個節點
set autocommit = false; # 設置事務手動提交
select * from student;
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; # 事務提交
總結
以上是生活随笔為你收集整理的mysql 事务 数量_MySQL 数据查询语言(DQL) 事务控制语言(TCL)详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vs远程编译linux程序,使用Visu
- 下一篇: 用宝塔本地搭建php,Windows系统