使用視圖
視圖是虛擬的表。與包含數據的表不一樣,視圖只包含使用時動態檢索數據的查詢。
視圖的作用:
重用SQL語句。
簡化復雜的SQL操作。在編寫查詢后,可以方便地重用它而不必知道它的基本查詢細節。
使用表的組成部分而不是整個表。
保護數據。可以給用戶授予表的特定部分的訪問權限而不是整個表的訪問權限。
更改數據格式和表示。視圖可返回與底層表的表示和格式不同的數據。
視圖用Create View來創建,用Drop View刪除,更新視圖時,可以用Create View OR Replace View
1.利用視圖簡化復雜聯結
create view PRoductcustomers ASselect cust_name,cust_contact,prod_idfrom customers,orders,orderitemswhere customers.cust_id = orders.cust_idAND orderitems.order_num = orders.order_num;首先創建一個名為productcustomers的視圖select cust_name,cust_contactfrom productcustomerswhere prod_id = 'TNT2';之后就可以利用視圖檢索數據2.用視圖重新格式化檢索出的數據
create view vendorlocations ASselect concat(rtrim(vend_name),'(',rtrim(vend_country),')') AS vend_titlefrom vendorsorder by vend_name; 創建一個名為vendorlocations的視圖,里面格式化了數據select*from vendorlocations;之后就可以方便查看此格式的數據3.用視圖過濾不想要的數據
create view customeremaillist ASselect cust_id, cust_name,cust_emailfrom customerswhere cust_email is not null;創建一個名為vcustomeremaillist的視圖,里面過濾掉了一些數據select *from customeremaillist;之后可以直接查詢4.使用視圖計算字段
create view orderitemsexpanded ASselect order_num, prod_id, quantity, item_price, quantity*item_price AS expanded_pricefrom orderitems;創建一個名為orderitemsexpanded的視圖select *from orderitemsexpandedwhere order_num = 20005;使用存儲過程存儲過程簡單來說,就是為以后的使用而保存的一條或多條MySQL語句的集合。
創建存儲過程:Create Procedure
使用存儲過程:Call
刪除存儲過程:Drop Procedure
1.創建存儲過程
DELIMITER //create procedure productpricing()begin select AVg(prod_price) AS priceaverage from products;end//DELIMITER ;這里需要使用 DELIMITER // 臨時更改命令行實用程序的語句分隔符為//,這樣,存儲過程體內的;仍然保持不動最后使用DELIMITER ; 恢復分隔符
調用存儲過程
call productpricing();2.使用參數DELIMITER //create procedure productpricing( out pl decimal(8,2), out ph decimal(8,2), out pa decimal(8,2) )begin select Min(prod_price) into pl from products; select Max(prod_price) into ph from products; select AVg(prod_price) into pa from products;end//DELIMITER ;關鍵字Out支出相應的參數用來從存儲過程中傳出一個值,MySQL還支持In(傳遞給存儲過程),InOut(對存儲過程傳入和傳出)
關鍵字Into用來將值保存到相應的變量
調用此存儲過程,必須指定三個變量名
call productpricing(@pricelow, @pricehigh, @priceavgerage);所有變量名必須以@開始,這條語句不顯示任何數據,它返回以后可以顯示的變量select @priceaverage;用于顯示具體的數據
使用In,Out參數
DELIMITER //create procedure ordertotal( In onumber int, Out ototal decimal(8,2) )begin select Sum(item_price*quantity) from orderitems where order_num = onumber into ototal;end//DELIMITER ;call ordertotal(20005,@total);select @total;建立智能存儲過程
DELIMITER //create procedure ordertotal( In onumber int, In taxable boolean, Out ototal decimal(8,2) ) comment 'Obtain order total,adding tax'begin declare total decimal(8,2); declare taxrate int default 6;select Sum(item_price*quantity)from orderitemswhere order_num = onumberinto total;if taxable then select total+(total/100*taxrate) into total;end if;select total into ototal;end //DELIMITER ;用DECLARE語句定義了兩個局部變量。DECLARE要求指定變量名和數據類型,它也支持可選的默認值本例子中中包含了一個COMMENT值。它不是必需的,但如果給出,將在SHOW PROCEDURE STATUS的結果中顯示。call ordertotal(20005,0,@total);select @total;call ordertotal(20005,1,@totall);select @totall;使用游標
游標 (cursor) 是一個存儲在MySQL服務器上的數據庫查詢,它不是一條SELECT語句,而是被該語句檢索出來的結果集
(相當于一個指向檢索出來的行的指針把。。。)
使用游標的步驟:
在能夠使用游標前,必須聲明(定義)它。這個過程實際上沒有檢索數據,它只是定義要使用的SELECT語句
一旦聲明后,必須打開游標以供使用。這個過程用前面定義的SELECT語句把數據實際檢索出來
對于填有數據的游標,根據需要取出(檢索)各行
在結束游標使用時,必須關閉游標
1.創建游標:Declare ... Cursor
2.開打和關閉游標:Open Cursor Close Cursor
如果你不明確關閉游標,MySQL將會在到達END語句時自動關閉它。
3.使用游標數據: 在一個游標被打開后,可以使用FETCH語句分別訪問它的每一行。FETCH指定檢索什么數據(所需的列),檢索出來的數據存儲在什么地方。
它還向前移動游標中的內部行指針,使下一條FETCH語句檢索下一行。
DELIMITER //create procedure processorders()begin declare done boolean default 0; declare o int; declare t decimal(8,2); declare ordernumbers cursor for select order_num from orders; declare continue handler for sqlstate '02000' set done = 1; create table if not exists ordertotals (order_num int, total decimal(8,2)); open ordernumbers; repeat fetch ordernumbers into o; call ordertotal(o,1,t); insert into ordertotals(order_num,total) values(o,t); until done end repeat; close ordernumbers;end //DELIMITER ;FETCH是在REPEAT內,因此它反復執行直到done為真(由UNTILdone END REPEAT;規定)。為使它起作用,用一個DEFAULT 0(假,不結束)定義變量done。done怎樣才能在結束時被設置為真呢?答案是用以下語句:Declare Continue Handler for sqlstate '02000' set done = 1;
這條語句定義了一個CONTINUE HANDLER,它是在條件出現時被執行的代碼。這里, 它指出當SQLSTATE '02000'出現時,SET done=1。SQLSTATE'02000'是一個未找到條件, 當REPEAT由于沒有更多的行供循環而不能繼續時,出現這個條件。
新聞熱點
疑難解答