http://www.cnblogs.com/linhaifeng/articles/7495918.htmlhtml
試圖就是一個虛擬表(非真實存在),本質就是【根據sql語句獲取動態的數據集,併爲其命名】,用戶使用時只須要使用名稱便可獲取數據集,可將該數據集看成表來使用。python
''' what: 視圖是由一張表或多張表的查詢結果構成的一張虛擬表 why: 將複雜經常使用的查詢結果保留下來重複使用 | 將一張大表拆分紅多張小表 語法: create [or replace] view 視圖名[(查詢字段別名們)] as 查詢語句 create view new_emp as (select * from emp); 注: 1.查詢字段別名們 要與 查詢語句的查詢字段對應 2.create or replace: 操做視圖沒有則建立、有則替換 create or replace view new_emp(id,姓名,工資) as (select id,name,salary from emp where dep_id = 2); 視圖的修改:alter 等價於 create or replace, 且語法一致 alter view new_emp(id,姓名,工資) as (select id,name,salary from emp where dep_id = 1); 視圖中字段的操做:不容許alter操做字段 alter table new_emp rename new_emp1; alter view new_emp modify id tinyint; 視圖中記錄的操做:等價於普通表,完成增刪改查 update new_emp set 姓名='san' where id = 3; delete from new_emp where id = 3; insert into new_emp(id, 姓名, 工資) values (10, "Bob", 10000); # 操做的是實體表, 虛擬表要從新建立才能拿到最新數據 視圖的刪除: drop view 視圖名; 總結: 虛擬表做用 -- 查詢 '''
使用觸發器能夠定製用戶對錶進行【增刪改】操做時先後的行爲(不報包括查詢)mysql
''' what:在表發生數據更新時,會自動觸發的功能稱之爲觸發器 why:當一個表在發生數據更新時,須要去完成一些操做,能夠爲具體數據更新的方式添加觸發器 語法: delimiter // create trigger 觸發器名 before|after insert|update|delete on 表名 for each row begin 須要觸發執行的sql代碼們 end // delimiter ; # 觸發器名: t1_before_insert_tri 注:delimiter是用來修改sql的語句結束標識符 刪除觸發器:drop trigger 觸發器名; '''
# 插入前 CREATE TRIGGER tri_before_insert_tb1 BEFORE INSERT ON tb1 FOR EACH ROW BEGIN ... END # 插入後 CREATE TRIGGER tri_after_insert_tb1 AFTER INSERT ON tb1 FOR EACH ROW BEGIN ... END # 刪除前 CREATE TRIGGER tri_before_delete_tb1 BEFORE DELETE ON tb1 FOR EACH ROW BEGIN ... END # 刪除後 CREATE TRIGGER tri_after_delete_tb1 AFTER DELETE ON tb1 FOR EACH ROW BEGIN ... END # 更新前 CREATE TRIGGER tri_before_update_tb1 BEFORE UPDATE ON tb1 FOR EACH ROW BEGIN ... END # 更新後 CREATE TRIGGER tri_after_update_tb1 AFTER UPDATE ON tb1 FOR EACH ROW BEGIN ... END # 刪除觸發器 drop trigger tri_after_insert_cmd;
# cmd表 create table cmd ( id int primary key auto_increment, user char(32), priv char(10), cmd char (64), sub_time datetime, # 提交時間 success enum ('yes', 'no') # 0表明執行失敗 ); # 錯誤日誌表 create table errlog ( id int primary key auto_increment, err_cmd char(64), err_time datetime ); # 建立觸發器 delimiter // create trigger trigger1 after insert on cmd for each row begin # new就是cmd當前插入的那條記錄(對象) if new.success = "no" then # 等待值判斷只有一個等號 insert into errlog values(null, new.cmd, new.sub_time); end if; end // delimiter ; # 往表cmd中插入記錄,觸發觸發器,根據IF的條件決定是否插入錯誤日誌 insert into cmd(user, priv, cmd, sub_time, success) values ('egon', '0765', 'ls -l /etc', now(), 'yes'), ('jerry', '0852', 'cat /etc/passwd', now(), 'no'), ('kevin', '0867', 'useradd xxx', now(), 'no'), ('owen', '0912', 'ps aux', now(), 'yes'); # 查看cmd數據信息 select * from cmd; # 查看錯誤日誌表中的記錄是否有自動插入 select * from errlog;
觸發器用戶沒法直接調用,而是由對錶的【增刪改】操做被動引起的程序員
事務用於將某些操做的多個SQL做爲原子性操做,一旦有某一個出現錯誤,便可回滾到原來的狀態,從而保證數據庫數據完整性sql
''' what:事務是邏輯上的一組操做,要麼都成功,要麼都失敗 why:不少時候一個數據操做,不是一個sql語句就完成的,可能有不少個sql語句,若是部分sql執行成功而部分sql執行失敗將致使數據錯亂 eg:轉帳 => 轉入轉出均成功,才能認爲操做成功 事務的使用: start transaction; --開啓事物,在這條語句以後的sql將處在同一事務,並不會當即修改數據庫 commit;--提交事務,讓這個事物中的sql當即執行數據的操做, rollback;--回滾事務,取消這個事物,這個事物不會對數據庫中的數據產生任何影響 事務的四大特性: 1.原子性:事務是一組不可分割的單位,要麼同時成功,要麼同時不成功 2.一致性:事物先後的數據完整性應該保持一致(數據庫的完整性:若是數據庫在某一時間點下,全部的數據都符合全部的約束,則稱數據庫爲完整性的狀態) 3.隔離性:事物的隔離性是指多個用戶併發訪問數據時,一個用戶的事物不能被其它用戶的事務所幹擾,多個併發事務之間數據要相互隔離 4.持久性:持久性是指一個事物一旦被提交,它對數據的改變就是永久性的,接下來即便數據庫發生故障也不該該對其有任何影響 事務的用戶隔離級別: 數據庫使用者能夠控制數據庫工做在哪一個級別下,就可與防止不一樣的隔離性問題 read uncommitted --不作任何隔離,可能髒讀,幻讀 read committed --能夠防止髒讀,不能防止不可重複讀,和幻讀, Repeatable read --能夠防止髒讀,不可重複讀,不能防止幻讀 Serializable --數據庫運行在串行化實現,全部問題都沒有,就是性能低 修改隔離級別: select @@tx_isolation;--查詢當前級別 set[session|global] transaction isolation level ....;修改級別 實例: set global transaction isolation level Repeatable read; 注:修改後從新鏈接服務器生效 '''
#準備數據 create table account( id int primary key auto_increment, name varchar(20), money double ); insert into account values (1,'owen',10000), (2,'egon',1000), (3,'jerry',1000), (4,'kevin',1000); # egon向owen借1000塊錢 # 未使用事務 update account set money = money - 1000 where id = 1; update account set moneys = money + 1000 where id = 2; # money打錯了致使執行失敗 # 在python中使用事務處理 from pymysql.err import InternalError sql = 'update account set money = money - 1000 where id = 1;' sql2 = 'update account set moneys = money + 1000 where id = 2;' # money打錯了致使執行失敗 try: cursor.execute(sql) cursor.execute(sql2) conn.commit() # 提交事務 except InternalError: print("轉帳失敗") conn.rollback()# 回滾操做
存儲過程包含了一系列可執行的sql語句,存儲過程存放於mysql中,經過調用它的名字可執行其內部的一堆sql語句數據庫
優勢:服務器
#1. 用於替代程序寫的SQL語句,實現程序與sql解耦 #2. 基於網絡傳輸,傳別名的數據量小,而直接傳sql數據量大
缺點:程序員擴展功能不方便網絡
程序與數據庫結合使用的三種方式session
#方式一: MySQL:存儲過程 程序:調用存儲過程 #方式二: MySQL: 程序:純SQL語句 #方式三: MySQL: 程序:類和對象,即ORM(本質仍是純SQL語句)
2、建立簡單的存儲過程(無參)併發
delimiter // create procedure p1() BEGIN select * from blog; INSERT into blog(name,sub_time) values("xxx",now()); END // delimiter ; #在mysql中調用 call p1() #在python中基於pymysql調用 cursor.callproc('p1') print(cursor.fetchall())
3、建立存儲過程(有參)
對於存儲過程,能夠接收參數,其參數有三類: #in 僅用於傳入參數用 #out 僅用於返回值用 #inout 既能夠傳入又能夠看成返回值
① in:傳入參數
delimiter // create procedure p2( in n1 int, in n2 int ) BEGIN select * from blog where id > n1; END // delimiter ; #在mysql中調用 call p2(3,2) #在python中基於pymysql調用 cursor.callproc('p2',(3,2)) print(cursor.fetchall())
② out:返回值
delimiter // create procedure p3( in n1 int, out res int ) BEGIN select * from blog where id > n1; set res = 1; END // delimiter ; #在mysql中調用 set @res=0; #0表明假(執行失敗),1表明真(執行成功) call p3(3,@res); select @res; #在python中基於pymysql調用 cursor.callproc('p3',(3,0)) #0至關於set @res=0 print(cursor.fetchall()) #查詢select的查詢結果 cursor.execute('select @_p3_0,@_p3_1;') #@p3_0表明第一個參數,@p3_1表明第二個參數,即返回值 print(cursor.fetchall())
③ inout 可傳值可返回
delimiter // create procedure p4( inout n1 int ) BEGIN select * from blog where id > n1; set n1 = 1; END // delimiter ; #在mysql中調用 set @x=3; call p4(@x); select @x; #在python中基於pymysql調用 cursor.callproc('p4',(3,)) print(cursor.fetchall()) #查詢select的查詢結果 cursor.execute('select @_p4_0;') print(cursor.fetchall())
4、執行存儲過程
① 在MySQL中執行存儲過程
-- 無參數 call proc_name() -- 有參數,全in call proc_name(1,2) -- 有參數,有in,out,inout set @t1=0; set @t2=3; call proc_name(1,2,@t1,@t2)
② 在python中基於MySQL執行存儲過程
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 執行存儲過程 cursor.callproc('p1', args=(1, 22, 3, 4)) # 獲取執行完存儲的參數 cursor.execute("select @_p1_0,@_p1_1,@_p1_2,@_p1_3") result = cursor.fetchall() conn.commit() cursor.close() conn.close() print(result)
5、刪除存儲過程
drop procedure proc_name;
6、小結
''' what:用於完成指定功能的sql語句塊,相似於Python中的函數 why:將能指定功能的sql語句塊創建成存儲過程,不只將sql語句邏輯化了,更是功能化了,那咱們要完成相同的事,只須要重複使用創建的存儲過程,不就須要再重複書寫sql語句了 # 總結: 存儲過程可讓sql語句具備 複用性, 從而提升開發效率 語法: delimiter // create procedure 存儲過程名( 輸入輸出類型1 參數名1 參數類型1(寬度), ... , 輸入輸出類型n 參數名n 參數類型n(寬度) ) begin sql語句塊 end // delimiter ; 注: 1.輸入輸出類型:in | out | inout 2.call 存儲過程名(實參們)來調用存儲過程 案例: set @res = null; # 定義空值變量, 用來接收存儲過程的執行結果 delimiter // create procedure user_info(in b int, in l int, out res char(20)) begin select * from emp limit b, l; set res = 'success'; end // delimiter ; call user_info(2, 3, @res); # 調用存儲過程, 傳入相應的實參 select @res; # 查看存儲過程的執行結果 變量的使用: 1.賦值變量:set @變量名 = 變量值 2.使用變量:@變量名 | select @變量名 3.刪除變量:set @變量名 = null 三種開發方式: 1. 業務邏輯 + 存儲過程:高執行與開發效率,低耦合 | 不易移植,人員成本高 2. 業務邏輯 + 原生sql:人員成本低 | 開發難度大 3. 業務邏輯 + ORM:高開發效率,對象化操做數據庫,可移植 | 性能消耗加大,多表聯查、複雜條件會複製化ORM 存儲過程的操做: 1.查看 select routine_name, routine_type from information_schema.routines where routine_schema='數據庫名'; eg: select routine_name, routine_type from information_schema.routines where routine_schema='db2'; 2.刪除 drop procedure [if exists] 數據庫名.存儲過程名 '''
delimiter // create procedure send_money( out p_return_code char(20) ) begin # 異常處理 declare exit handler for sqlexception begin # error set p_return_code = '錯誤異常'; rollback; end; # exit 也能夠換成continue 表示發送異常時繼續執行 declare exit handler for sqlwarning begin # warning set p_return_code = '警告異常'; rollback; end; start transaction; update account set money = money - 1000 where id = 1; update account set money = moneys + 1000 where id = 2; # moneys字段致使異常 commit; # success set p_return_code = '轉帳成功'; # 表明執行成功 end // delimiter ; # 在mysql中調用存儲過程 set @res=null; call send_money(@res); select @res;