《MySQL必知必會》學習筆記(2)—增刪改、視圖、存儲過程、遊標

目錄

數據的增刪改

創建和操縱表

視圖

存儲過程

遊標


數據的增刪改

  • 插入數據

插入數據時VALUES必須以其指定的次序匹配指定的列名,而不一定要按各個列出現在實際表中的次序。

insert into customers(cust_name, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) 
values('Pep E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA', null, null),
      ('Pep E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA', null, null);

insert還可以將一條select語句的結果插入表中 :

--使用INSERT SELECT從custnew中將所有數據導入customers
insert into customers(cust_contact, cust_email, cust_name, cust_adress, cust_city, cust_state, cust_zip, cust_country) 
select cust_id, cust_email, cust_name, cust_adress, cust_city, cust_state, cust_zip, cust_country from custnew;
  • 更新數據

在更新多個列時,只需要使用單個SET命令,每個“列=值”對之間用逗號分隔。另外,如果要刪除某一列的值,可設置它成爲null。

update customers set cust_emailn = '[email protected]', cust_name = 'The Fudds' where cust_id = 10005;
  • 刪除數據
delete from customers where cust_id = 10006;

創建和操縱表

  • 創建表
create table customers
(
    cust_id  int  not null auto_increment,
    cust_name char(50) not null, 
    cust_adress char(50) not null, 
    cust_city char(50) not null,
    cust_state char(5) not null,
    cust_zip char(10) not null, 
    cust_country char(50) not null,
    cust_contact char(50) not null,
    cust_email char(255) not null,
    primary key(cust_id)
)Enging = InnoDB;
  1. 表的主鍵由PRIMARY KEY關鍵字指定;
  2. AUTO_INCREMENT會告訴MySQL,本列每當增加一行時自動增量;每個表只能有一個AUTO_INCREMENT列,並且必須被索引;
  3. DEFAULT關鍵字可以指定某列的默認值;
  4. ENGINE來指定使用的存儲引擎,常用的有:InnoDB是一個可靠的事務處理引擎,它不支持全文本搜索;MEMORY在功能等同於MyISAM,但由於數據存儲在內存(不是磁盤)中,速度很快(特別適合於臨時表);MyISAM是一個性能極高的引擎,它支持全文本搜索,但不支持事務處理
  • 更新表
--增加一個列
alter table vendors add vend_phone char(20);
--刪除一個列
alter table vendors drop column vend_phone;

ALTER TABLE的一種常見用途是用來定義外鍵:

alter table orders add constraint fk_orderitems_products foreign key (cust_id) references customers (cust_id);
  • 刪除表
drop table customers;
  • 重命名錶
rename table customers2 to customers;

視圖

  • 爲什麼要使用視圖
  1. 重用SQL語句。
  2. 簡化複雜的SQL操作。在編寫查詢後,可以方便地重用它而不必知道它的基本查詢細節。
  3. 使用表的組成部分而不是整個表。
  4. 保護數據。可以給用戶授予表的特定部分的訪問權限而不是整個表的訪問權限。
  5. 更改數據格式和表示。視圖可返回與底層表的表示和格式不同的數據。
  • 利用視圖簡化複雜的聯結
create view productcustomers as
select cust_name, cust_contact, prod_id
from customers, orders, orderitems
where customers.cust_id = orders.cust_id and orderitems.order_num = orders.order_num;
--利用視圖,可一次性編寫基礎的SQL,然後根據需要多次使用。
select cust_name, cust_contact from productcustomers where prod_id = 'TNT2';
  • 用視圖重新格式化檢索出的數據
--創建一個視圖來存儲某種格式化的結果,而不必在每次需要時執行聯結
create view vendorlocations as 
select contact(RTrim(vend_name), '(', RTrim(vend_country), ')') as vend_title from vendors order by vend_name;
  • 用視圖過濾不想要的數據
--過濾沒有電子郵件地址的客戶
create view customeremaillist as
select cust_id, cust_name, cust_email from customers where cust_email is not null;
  • 使用視圖與計算字段

create view orderitemsexpanded as 
select order_num prod_id, quantity, item_price, quantity*item_price as expanded_price from orderitems;
一般來說,應該將視圖用於檢索(SELECT語句) 而不用於更新(INSERTUPDATEDELETE)。

存儲過程

  • 爲什麼要使用存儲過程
存儲過程是爲了以後的使用而保存的一條或多條MySQL語句的集合,使用存儲過程主要有3個好處:簡單、安全、高性能
  1. 通過把處理封裝在容易使用的單元中,簡化複雜的操作;
  2. 防止錯誤,由於不要求反覆建立一系列處理步驟,這保證了數據的完整性;
  3. 簡化對變動的管理。如果表名、列名或業務邏輯等有變化,只需要更改存儲過程的代碼,使用它的人員甚至不需知道這些變化;
  4. 提高性能,使用存儲過程比使用單獨的SQL語句要快。
使用存儲過程也存在一些缺陷,首先,一般來說,存儲過程的編寫比基本SQL語句複雜,編寫存儲過程需要更高的技能,更豐富的經驗。另外,需要限制存儲過程的創建權限,允許用戶使用存儲過程,但不允許他們創建存儲過程。
 
  • 使用存儲過程

(1)執行存儲過程

--執行名爲productpricing的存儲過程,它計算並返回產品的最低、最高和平均價格
CALL productpricing(@pricelow, @pricehigh, @priceaverage);

(2)創建存儲過程

CREATE PROCEDURE productpricing()
begin
         select avg(prod_price) as priceaverage
         from products;
end;

(3)刪除存儲過程

--注意沒有使用後面的()
drop procedure productpricing;

(4)使用參數

create procedure productpricing(
        out pl declimal(8,2),
        out ph declimal(8,2),
        out pa declimal(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;

--調用此存儲過程,必須指定3個變量名,用於保存該存儲過程的結果
CALL productpricing(@pricelow, @pricehigh, @priceaverage);

(5)檢查存儲過程

show create procedure ordertotal;

遊標

遊標是一個存儲在MySQL服務器上的數據庫查詢,它不是一條SELECT語句,而是被該語句檢索出來的結果集。在存儲了遊標之後,應用程序可以根據需要滾動或瀏覽其中的數據。它主要用於交互式應用,其中用戶需要滾動屏幕上的數據,並對數據進行瀏覽或做出更改。

  • 使用遊標

(1)在能夠使用遊標前,必須聲明(定義)它,這個過程實際上沒有檢索數據,只是定義要使用的SELECT語句。遊標用DECLARE語句創建,如:

create procedure processorders()
begin
         DECLARE ordernumbers cursor
         for
         select order_num from orders;
end;

(2)一旦聲明後,必須打開遊標以供使用,這個過程用前面定義的 SELECT語句把數據實際檢索出來;

--在處理OPEN語句時執行查詢,存儲檢索出的數據以供瀏覽和滾動。
open ordernumbers;

(3)對於填有數據的遊標,根據需要取出(檢索)各行;

--用fetch檢索當前行的order_num列到一個名爲o的局部聲明的變量中
create procedure processorders()
begin 
         declare o int;
         declare ordernumbers cursor
         for
         select order_num from orders;
         open ordernumbers;
         FETCH ordernumbers into o;
         close ordernumbers;
end;

(4)在結束遊標使用時,必須關閉遊標。

close ordernumbers;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章