事務、準備數(shù)據(jù)

事務

提交事務:commit; 同步數(shù)據(jù)表,表示操作成功

回滾事務:rollback; 直接清空日志表,表示操作失敗

事務transaction

-- 創(chuàng)建一個賬戶

create table my_account(

id int primary key auto_increment,

number char(16) not null unique comment '賬戶',

name varchar(20) not null,

money decimal(10,2)default 0.0 comment '賬戶余額'

)charset utf8;

-- 插入數(shù)據(jù)

insert into my_account values

(null,'0000000000000001','張三',1000),

(null,'0000000000000002','李四',2000);

-- 張三轉賬1000元給李四

update my_account set money=money-1000 where id=1;

一、事務操作分兩種:自動事務(默認)、手動事務

1)手動事務的操作流程

? ? ? ? ? ? 1> 開啟事務:start transaction;

-- 事務安全

-- 開啟事務

start transaction;

? ? ? ? ? ? 2> 進行事務操作

-- 事務操作:1、李四賬戶減少

update my_account set money=money-1000 where id=2;

-- 事務操作:2、張三賬戶增加

update my_account set money=money+1000 where id=1;

? ? ? ? ? ? 3> 關閉事務

???????提交事務:commit;同步數(shù)據(jù)表,表示操作成功

-- 提交事務

commit;

????????回滾事務:rollback;直接清空日志表,表示操作失敗

-- 回滾事務:會清空

rollback;

二、事務操作原理:

事務開啟之后,所有的操作都會臨時保存到事務日志,而事務日志只有在得到commit命令才會同步到數(shù)據(jù)表,其他任何情況都會清空,比如rollback、斷電、斷開連接

三、回滾點

????????????1)設置回滾點語法:savepoint 回滾點名字;

????????????2)回到回滾點語法:rollback to 回滾點名字;

-- 設置回滾點

savepoint sp1;

-- 銀行扣稅

update my_account set money=money-10000*0.05 where id=2;-- 錯誤

-- 回滾到回滾點

rollback to sp1;

-- 繼續(xù)操作

update my_account set money=money-10000*0.05 where id=1;

-- 查看結果

select * from my_account;

-- 提交結果

commit;

-- 如果要注冊一個用戶? 用戶名 》密碼 》驗證碼 —— 如果不注冊,將全回滾用rollback

-- 如果要注冊一個用戶 用戶名 》密碼 》驗證碼 —— 如果重新輸入驗證碼,將用回滾點rollback to

回滾喝回滾點的不同

rollback :所有操作都不要了

rollback to:回到某個回滾點之后的錯誤操作目標

四、自動事務處理

? ? ? ? ? ? 1)show variables like 'autocommit';

? ? ? ? ? ? 2)關閉事務自動提交:set autocommit=off/0;

關閉自動提交后,增、刪、改就只能手動提交(commit)

打開事務自動提交:set autocommit=off/;——默認

-- 顯示系統(tǒng)變量autocommit(模糊查詢)

show variables like 'autocommit';

-- 關閉事務自動提交

set autocommit=0;

update my_account set money=money+10000 where id=2;

commit;

-- 打開事務自動提交

set autocommit=1;

update my_account set money=money-10000*0.05 where id=2;

五、(數(shù)據(jù)庫)事務的四大特性:ACID

????????????A:Atomic,原子性(要么都成功,要么都失敗)

????????????C:Consistency,一致性(只要開啟事務,沒有開啟commit,另一個窗口沒有影響)

????????????I:Isolation,隔離性(兩個窗口都在進行事務,互不影響)

????????????D:Durability,持久性(只要提交了,rollback也沒用)

-- 隔離性

start transaction;

-- 給張三返稅,反500元

update my_account set money=money+500 where id=1;

-- (在另一個命令行輸入)隔離性

start transaction;

-- (在另一個命令行輸入)李四淘寶花了500元

update my_account set money=money-500 where id=2;

-- 兩個命令行都可以查?

select * from my_account;?

rollback;?

select * from my_account;

六、鎖機制(有時候行鎖,有時候表鎖,有時候頁鎖)

-- 鎖機制

start transaction;

update my_account set money=money+500 where name='張三';-- 整體找,直到找到所有的‘張三’

-- (在另一個命令行輸入)李四+1000元

update my_account set money=money+1000 where id=2;

rollback;



準備數(shù)據(jù)

1、創(chuàng)建數(shù)據(jù)表

-- 創(chuàng)建 "京東" 數(shù)據(jù)庫createdatabasejdcharset=utf8;-- 使用 "京東" 數(shù)據(jù)庫usejd;-- 創(chuàng)建一個商品goods數(shù)據(jù)表createtablegoods(idintunsignedprimarykeyauto_incrementnotnull,namevarchar(150)notnull,? ? cate_namevarchar(40)notnull,? ? brand_namevarchar(40)notnull,? ? pricedecimal(10,3)notnulldefault0,? ? is_showbitnotnulldefault1,? ? is_saleoffbitnotnulldefault0);

2、插入數(shù)據(jù)

-- 向goods表中插入數(shù)據(jù)insertintogoodsvalues(0,'r510vc 15.6英寸筆記本','筆記本','華碩','3399',default,default);insertintogoodsvalues(0,'y400n 14.0英寸筆記本電腦','筆記本','聯(lián)想','4999',default,default);insertintogoodsvalues(0,'g150th 15.6英寸游戲本','游戲本','雷神','8499',default,default);insertintogoodsvalues(0,'x550cc 15.6英寸筆記本','筆記本','華碩','2799',default,default);insertintogoodsvalues(0,'x240 超極本','超級本','聯(lián)想','4880',default,default);insertintogoodsvalues(0,'u330p 13.3英寸超極本','超級本','聯(lián)想','4299',default,default);insertintogoodsvalues(0,'svp13226scb 觸控超極本','超級本','索尼','7999',default,default);insertintogoodsvalues(0,'ipad mini 7.9英寸平板電腦','平板電腦','蘋果','1998',default,default);insertintogoodsvalues(0,'ipad air 9.7英寸平板電腦','平板電腦','蘋果','3388',default,default);insertintogoodsvalues(0,'ipad mini 配備 retina 顯示屏','平板電腦','蘋果','2788',default,default);insertintogoodsvalues(0,'ideacentre c340 20英寸一體電腦 ','臺式機','聯(lián)想','3499',default,default);insertintogoodsvalues(0,'vostro 3800-r1206 臺式電腦','臺式機','戴爾','2899',default,default);insertintogoodsvalues(0,'imac me086ch/a 21.5英寸一體電腦','臺式機','蘋果','9188',default,default);insertintogoodsvalues(0,'at7-7414lp 臺式電腦 linux )','臺式機','宏碁','3699',default,default);insertintogoodsvalues(0,'z220sff f4f06pa工作站','服務器/工作站','惠普','4288',default,default);insertintogoodsvalues(0,'poweredge ii服務器','服務器/工作站','戴爾','5388',default,default);insertintogoodsvalues(0,'mac pro專業(yè)級臺式電腦','服務器/工作站','蘋果','28888',default,default);insertintogoodsvalues(0,'hmz-t3w 頭戴顯示設備','筆記本配件','索尼','6999',default,default);insertintogoodsvalues(0,'商務雙肩背包','筆記本配件','索尼','99',default,default);insertintogoodsvalues(0,'x3250 m4機架式服務器','服務器/工作站','ibm','6888',default,default);insertintogoodsvalues(0,'商務雙肩背包','筆記本配件','索尼','99',default,default);

-- 查詢類型cate_name為'超級本的商品

select name as 商品名稱,priceas 商品價格from goodswhere cate_name ='超級本';

-- 顯示商品的種類

select distinct cate_namefrom goods; -- 去重

select cate_namefrom goodsgroup by cate_name;-- 分組

-- 查詢每個種類的商品名稱

select cate_name,group_concat(name)from goodsgroup by cate_name;

-- 求所有電腦產品的平均價格,并且保留兩位小數(shù)

select round(avg(price),2)from goods;

-- 顯示每種商品的平均價格

select cate_name,avg(price)from goodsgroup by cate_name;

-- 查詢每種類型的商品中最貴、最便宜、平均價、數(shù)量

select cate_name,max(price),min(price),avg(price),count(*)from goodsgroup by cate_name;

-- 查詢所有價格大于平均價格的商品,并且按價格降序排序

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 * from (select cate_name,max(price)as max_pricefrom goodsgroup by cate_name)as g_new

left join goodsas g

on g_new.cate_name=g.cate_nameand g_new.max_price=g.priceorder by g_new.cate_name;

insert into goodsvalues(0,'東哥牌電腦','筆記本','老王','4999',default,default);

-- 創(chuàng)建商品分類表

create table if not exists goods_cates(

id int unsigned primary key auto_increment,

? ? name varchar(40)not null

);

-- 查詢goods商品的種類

select cate_namefrom goodsgroup by cate_name;

insert into goods_cates (name)select cate_namefrom goodsgroup by cate_name;

update goodsas ginner join goods_catesas con g.cate_name=c.name set g.cate_name=c.id;

insert into goods_cates(name)values ('路由器'),('交換機'),('網卡');

insert into goods (name,cate_id,brand_id,price)

values('LaserJet Pro P1606dn 黑白激光打印機', 12, 4,'1849');

alter table goods

change cate_namecate_id int unsigned not null;

alter table goodsadd foreign key (cate_id)references goods_cates(id);-- 外鍵關聯(lián)

delete from goodswhere id=23;

-- 創(chuàng)建商品品牌表

create table if not exists goods_brands(

id int unsigned primary key auto_increment,

? ? name varchar(40)not null

);

select brand_namefrom goodsgroup by brand_name;

insert into goods_brands (name)select brand_namefrom goodsgroup by brand_name;

update goodsas ginner join goods_brandsas bon g.brand_name=b.name set g.brand_name=b.id;

alter table goods

change brand_namebrand_id int unsigned not null;

alter table goodsadd foreign key (brand_id)references goods_brands(id);-- 外鍵關聯(lián)

insert into goods (name,cate_id,brand_id,price)

values('LaserJet Pro P1606dn 黑白激光打印機', 2, 100,'1849');

-- 創(chuàng)建品牌表

create table goods_brands (

id int unsigned primary key auto_increment,

? ? name varchar(40)not null)select brand_nameas name from goodsgroup by brand_name;

update goodsas ginner join goods_brandsas bon g.brand_name=b.name set g.brand_name=b.id;

alter table goods

change brand_namebrand_id int unsigned not null;

alter table goodsadd foreign key (brand_id)references goods_brands(id);

-- 取消外鍵

alter table goodsdrop foreign key goods_ibfk_1;

alter table goodsdrop foreign key goods_ibfk_2;

# 在python3中使用 數(shù)據(jù)庫

# python2? import MySQLdb

import pymysql

'''

'''

from pymysqlimport connect

# 創(chuàng)建connection連接

conn = connect(host='localhost',port=3306,database='wn',user='root',password='root',charset='utf8')

# 獲取cursor對象

cursor = conn.cursor()

count = cursor.execute("select * from goods;")

print('查詢到%d條數(shù)據(jù)'%count)

# print(cursor.fetchone())

# print(cursor.fetchone())

# print(cursor.fetchone())

# print(cursor.fetchone())

#

# print(cursor.fetchmany())# 取一條

# print(cursor.fetchmany(3))# 傳幾條取幾條

#

# print(cursor.fetchall()) #取所有

line_content = cursor.fetchone()

print(line_content)

print(line_content[0])

print(line_content[1])

print(line_content[2])

for tempin line_content:

print(temp)

lines = cursor.fetchmany(5)

for tempin lines:

print(temp)

# 關閉cursor對象

cursor.close()

conn.close()

# print(cursor.fetchone(),'--------->')

# count = cursor.execute("select * from goods;")

# 數(shù)據(jù)庫關閉之后不能再調用代碼

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。