国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL學習(九)

2024-07-24 12:59:25
字體:
來源:轉載
供稿:網友

使用視圖

視圖是虛擬的表。與包含數據的表不一樣,視圖只包含使用時動態檢索數據的查詢。

視圖的作用:

重用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由于沒有更多的行供循環而不能繼續時,出現這個條件。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 壶关县| 道孚县| 庐江县| 鹿泉市| 万宁市| 防城港市| 沙坪坝区| 万源市| 油尖旺区| 天全县| 赤峰市| 东阿县| 西乌珠穆沁旗| 闸北区| 固始县| 舟曲县| 台东县| 开远市| 天峨县| 祁门县| 沧源| 揭阳市| 大冶市| 惠水县| 公主岭市| 无锡市| 八宿县| 海晏县| 兖州市| 泉州市| 灵寿县| 舞钢市| 泸溪县| 忻城县| 五大连池市| 宁化县| 芦溪县| 宁蒗| 汾阳市| 阿图什市| 清丰县|