MySQL必知必會—概念篇mysql
MySQL必知必會—檢索數據篇github
視圖僅僅是用來查看存儲在別處的數據的一種設施,自己不包含數據,返回的數據都是從其餘表中檢索出來的,視圖能更改數據格式和表示,最多見的應用就是重用 SQL 語句,簡化複雜的 SQL 操做。服務器
CREATE VIEW 建立視圖函數
SHOW CREATE VIEW viewname 查看建立視圖的語句post
DROP VIEW viewname 刪除視圖性能
更新視圖時,能夠先 DROP 而後再 CREATE 或者使用 CREATE OR REPLACE VIEW
### 以前有用聯表查詢 prod_id 是 TNT2的購買用戶信息,可是若是還想看 prod_id 是其餘值的時,還得從新查一遍,這樣的場景就能用到視圖了。
### 建立一個包含 cust_name, cust_contact, prod_id 的視圖
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;
### 如今就能看到 productcustomers 視圖能查詢的信息了
select * from productcustomers;
+----------------+--------------+---------+
| cust_name | cust_contact | prod_id |
+----------------+--------------+---------+
| Coyote Inc. | Y Lee | ANV01 |
| Coyote Inc. | Y Lee | ANV02 |
| Coyote Inc. | Y Lee | TNT2 |
| Coyote Inc. | Y Lee | FB |
| Coyote Inc. | Y Lee | FB |
| Coyote Inc. | Y Lee | OL1 |
| Coyote Inc. | Y Lee | SLING |
| Coyote Inc. | Y Lee | ANV03 |
| Wascals | Jim Jones | JP2000 |
| Yosemite Place | Y Sam | TNT2 |
| E Fudd | E Fudd | FC |
+----------------+--------------+---------+
### 若是想再查詢出 prod_id 爲 TNT2 的客戶信息就很簡單了
SELECT cust_name, cust_contact FROM productcustomers WHERE prod_id = 'TNT2';
+----------------+--------------+
| cust_name | cust_contact |
+----------------+--------------+
| Coyote Inc. | Y Lee |
| Yosemite Place | Y Sam |
+----------------+--------------+
複製代碼
使用存儲過程主要有三個好處,簡單,安全,高性能。
MySQL 稱存儲過程的執行爲調用,所以 MySQL 執行存儲過程的語句爲 CALL。CALL 接受存儲過程的名字以及須要傳遞給它的任意參數。
### 建立名爲 productpricing 的存儲過程 若是存儲過程接受參數,它們將在 productpricing () 這個括號中列舉出來。
DELIMITER //
CREATE PROCEDURE productpricing()
BEGIN
SELECT Avg(prod_price) AS priceaverage FROM products;
END//
### 調用存儲過程,彷彿在調用函數
CALL productpricing;
+--------------+
| priceaverage |
+--------------+
| 16.133571 |
+--------------+
### 刪除存儲過程
DROP PROCEDURE productpricing;
### 檢查存儲過程
SHOW CREATE PROCEDURE productpricing;
複製代碼
### 建立存儲過程
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 //
### 調用存儲過程
CALL productpricing(@pricelow,@pricehigh,@priceaverage);
### 查看3個變量
SELECT @pricelow,@pricehigh,@priceaverage;
+-----------+------------+---------------+
| @pricelow | @pricehigh | @priceaverage |
+-----------+------------+---------------+
| 2.50 | 55.00 | 16.13 |
+-----------+------------+---------------+
複製代碼
須要獲取訂單合計,而且對某些顧客的合計增長營業稅。
### 存儲過程全過程
DELIMITER //
-- Name: ordertotal
-- Parameters: onumber = order number
-- taxable = 0 if not taxable, 1 if taxable
-- ototal = order total variable
CREATE PROCEDURE ordertotal (
IN onumber INT,
IN taxable BOOLEAN,
OUT ototal DECIMAL(8,2)
) COMMENT 'Obtain order total, optionally adding tax'
BEGIN
-- Declare variable for total
DECLARE total DECIMAL(8,2);
-- Declare tax percentage
DECLARE taxrate INT DEFAULT 6;
-- Get the order total
SELECT Sum(item_price*quantity) FROM orderitems WHERE order_num = onumber INTO total;
-- Is this taxable
IF taxable THEN
-- Yes, so add taxrate to the total
SELECT total+(total/100*taxrate) INTO total;
END IF;
-- And finally, save to out variable
SELECT total INTO ototal;
END //
### 調用,看一下 order number 是 20005 訂單關於加不加營業稅的區別
### 不加營業稅
CALL ordertotal(20005, 0, @total);
SELECT @total;
+--------+
| @total |
+--------+
| 149.87 |
+--------+
### 加營業稅
CALL ordertotal(20005, 1, @total);
SELECT @total;
+--------+
| @total |
+--------+
| 158.86 |
+--------+
複製代碼
上面代碼中作些必要的解釋
--
添加註釋,在存儲過程複雜是,註釋頗有必要。DECLARE
定義局部變量,須要指定變量名和數據類型,支持可選的默認值COMMENT
關鍵字,不是必需的,若是添加了,在 SHOW PROCEDURE STATUS 的結果中顯示。SHOW PROCEDURE STATUS
能夠列出全部存儲過程,也能夠使用 LIKE 指定一個過濾模式: SHOW PROCEDURE STATUS LIKE 'ordertotal';
一個持續更新的github筆記,連接地址:Front-End-Basics,能夠watch,也能夠star。
此篇文章的地址:MySql必知必會