一、事務(wù)操作
事務(wù)操作分兩種:自動(dòng)事務(wù)(默認(rèn))、手動(dòng)事務(wù)
1、手動(dòng)事務(wù)的操作流程
(1)開(kāi)啟事務(wù):start transaction;
(2)進(jìn)行事務(wù)操作
(3)關(guān)閉事務(wù)
???????? 提交事務(wù):commit; 同步數(shù)據(jù)表,表示操作成功
???????? 回滾事務(wù):rollback; 直接清空日志表,表示操作失敗
2、事務(wù)操作原理:
事務(wù)開(kāi)啟之后,所有的操作都會(huì)臨時(shí)保存到事務(wù)日志,而事務(wù)日志只有在得到commit命令才會(huì)同步到數(shù)據(jù)表,其他任何情況都會(huì)清空,比如rollback、斷電、斷開(kāi)連接
3、回滾點(diǎn)
設(shè)置回滾點(diǎn)語(yǔ)法:savepoint 回滾點(diǎn)名字;
回到回滾點(diǎn)語(yǔ)法:rollback to 回滾點(diǎn)名字;
4、自動(dòng)事務(wù)處理
show variables like 'autocommit';
關(guān)閉自動(dòng)提交:set autocommit=off/0;
5、事務(wù)的四大特性:ACID
A:Atomic,原子性(要么都成功,要么都失?。?/p>
C:Consistency,一致性(事務(wù)操作中數(shù)據(jù)是一致的,只有同步之后數(shù)據(jù)才會(huì)發(fā)生變化)
I:Isolation,隔離性(兩個(gè)書(shū)屋相互隔離不受影響)
D:Durability,持久性(數(shù)據(jù)一旦提交,不可改變,不能回滾)
6、鎖機(jī)制
二、MySQL與python交互
1、準(zhǔn)備數(shù)據(jù)
(1)創(chuàng)建數(shù)據(jù)表
(2)插入數(shù)據(jù)
2、SQL演練
(1)SQL語(yǔ)句的強(qiáng)化
???????? 查詢類型cate_name為 '超極本' 的商品名稱、價(jià)格
???????? 顯示商品的種類
???????? 求所有電腦產(chǎn)品的平均價(jià)格,并且保留兩位小數(shù)
???????? 顯示每種商品的平均價(jià)格
???????? 查詢每種類型的商品中 最貴、最便宜、平均價(jià)、數(shù)量
???????? 查詢所有價(jià)格大于平均價(jià)格的商品,并且按價(jià)格降序排序
???????? 查詢每種類型中最貴的電腦信息
3、創(chuàng)建‘商品分類’表
(1)查詢goods表中商品的種類
(2)將分組結(jié)果寫(xiě)入到goods_cates數(shù)據(jù)表
4、同步表數(shù)據(jù)
???? 通過(guò)goods_cates數(shù)據(jù)表來(lái)更新goods表
5、修改表結(jié)構(gòu)
(1)查看 goods 的數(shù)據(jù)表結(jié)構(gòu),會(huì)發(fā)現(xiàn) cate_name 和 brand_name對(duì)應(yīng)的類型為varchar但是存儲(chǔ)的都是數(shù)字
(2)通過(guò)alter table語(yǔ)句修改表結(jié)構(gòu)
6、外鍵
(1)分別在 goods_cates 和 goods_brands表中插入記錄
(2)在 goods 數(shù)據(jù)表中寫(xiě)入任意記錄
————————————代碼部分————————————
-- 創(chuàng)建一個(gè)賬戶表
create table my_account(
idint primary key auto_increment,
numberchar(16)not null unique
comment'賬戶',
namevarchar(20)not null,
moneydecimal(10,2)default 0.0
comment'賬戶余額'
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_accountvalues
(null,'0000000000000001','張三',
1000),
(null,'0000000000000002','李四',
2000);
-- 張三轉(zhuǎn)賬1000元給李四
update my_accountset money=money
-1000 where id=1;
-- 事務(wù)安全
-- 開(kāi)啟事務(wù)
starttransaction;
-- 事務(wù)操作:1、李四賬戶減少
update my_accountset money=money
-1000 where id=2;
-- 事務(wù)操作:2、張三賬戶增加
update my_accountset money=money
+1000 where id=1;
-- 提交事務(wù)
commit;
-- 回滾點(diǎn)操作
-- 開(kāi)啟事務(wù)
starttransaction;
-- 事務(wù)處理1:張三發(fā)工資了,加錢(qián)
update my_accountset money=money
+10000 where id=1;
-- 設(shè)置回滾點(diǎn)
savepoint sp1;
-- 銀行扣稅
update my_accountset money=money
-10000*0.05 where id=2;-- 錯(cuò)誤
-- 回滾到回滾點(diǎn)
rollback to sp1;
-- 繼續(xù)操作
update my_accountset money=
money-10000*0.05 where id=1;
-- 查看結(jié)果
select *from my_account;
-- 提交結(jié)果
commit;
-- 顯示系統(tǒng)變量autocommit(模糊查詢)
show variableslike 'autocommit';
-- 關(guān)閉事務(wù)自動(dòng)提交
set autocommit=0;
-- 給李四發(fā)工資
update my_accountset money=money
+10000 where id=2;
commit;
update my_accountset money=money-10000*0.05 where id=2;
-- 事務(wù)的隔離性
starttransaction;
-- 給張三返稅,返500塊錢(qián)
update my_accountset money=money+500 where id=1;
select *from my_account;
-- 另外窗口開(kāi)啟事務(wù)
starttransaction;
-- 李四淘寶花了500
update my_accountset money=money-500where id=2;
select *from my_account;
commit;
-- 回到張三窗口,事務(wù)回滾
rollback;
select *from my_account;-- 兩邊一致
-- 鎖機(jī)制
starttransaction;
-- 使用非索引字段(name),行鎖自動(dòng)上升為表鎖
update my_accountset money=money+500 where name='張三';
update my_accountset money=money+1000 where id=2;
-- SQL演練
-- 查詢類型cate_name為'超級(jí)本'的商品名稱、價(jià)格
select name,pricefrom goodswhere cate_name ='超級(jí)本';
select nameas 商品名稱,priceas 商品價(jià)格from goodswhere cate_name='超級(jí)本';
-- 顯示商品的種類
select cate_namefrom goods;
select distinct cate_namefrom goods;
select cate_namefrom goodsgroup by cate_name;
select cate_name,group_concat(name)from goodsgroup by cate_name;
-- 求所有電腦產(chǎn)品的平均價(jià)格,并且保留兩位小數(shù)
select round(avg(price),2)from goods;
-- 顯示每種商品的平均價(jià)格
select cate_name,avg(price)from goodsgroup by cate_name;
-- 查詢每種類型的商品中 最貴、最便宜、平均價(jià)、數(shù)量
select? cate_name,max(price),min(price),avg(price),count(*)from goodsgroup by cate_name;
-- 查詢所有價(jià)格大于平均價(jià)格的商品,并且按價(jià)格降序排序
select avg(price)from goods;
select *from goodswhere price > (select avg(price)from goods)order by pricedesc;
-- 查詢每種類型中最貴的電腦信息
select cate_name,max(price)from goodsgroup by cate_name;
select *from goods;
-- 查詢每種類型中最貴的電腦信息
select *from goods
inner join
? ? (
select
? ? ? ? cate_name,
max(price)as max_price,
min(price)as min_price,
avg(price)as avg_price,
count(*)from goodsgroup by cate_name
)as goods_new_info
on goods.cate_name=goods_new_info.cate_nameand goods.price=goods_new_info.max_price;
select g_new.cate_name,g.name,g.pricefrom (
select cate_name,max(price)as
max_pricefrom goodsgroup by
cate_name
)as g_newleft join goodsas g
on g_new.cate_name=g.cate_name
and g_new.max_price = g.priceorder by g_new.cate_name;
insert into goods
values(0,'東哥牌電腦','筆記本','老王','4999',default,default);
-- 創(chuàng)建商品分類表
create table ifnot exists goods_cates(
idint unsignedprimary key auto_increment,
namevarchar(40)not null);
show tables;
-- 查詢goods表中商品的種類
select cate_namefrom goodsgroup by cate_name;
-- 拆表
-- 將分組結(jié)果寫(xiě)入到goods_cates數(shù)據(jù)表
insert into goods_cates (name)
select cate_namefrom goodsgroup by cate_name;
update goodsset cate_name=401 where cate_name='電腦'
-- 同步表數(shù)據(jù)
-- 通過(guò)goods_cates數(shù)據(jù)表來(lái)更新goods表
update goodsas ginner join goods_catesas con g.cate_name=c.nameset g.cate_name=c.id;
-- 插入類別
insert into goods_cates(name)
values('路由器'),('交換機(jī)'),('網(wǎng)卡');
-- 插入商品
insert into goods(name,cate_id,brand_name,price)
values('LaserJet Pro P1606dn
黑白激光打印機(jī)',12,4,'1849');
-- 通過(guò)alter table語(yǔ)句修改表結(jié)構(gòu)
alter table goods
change cate_name cate_idint
unsignednot null;
delete from? goodswhere id=23;
-- 添加外鍵
alter table goodsadd foreign
key (cate_id)references
goods_cates(id);