一、Introduction
Sakila可以作為數據庫設計的參考,也可作為實驗數據。我是用作數據倉庫和ODI學習的實驗數據。
The Sakila sample database was developed by Mike Hillyer, a former member of the MySQL AB documentation team, and is intended to provide a standard schema that can be used for examples in books, tutorials, articles, samples, and so forth. Sakila sample database also serves to highlight the latest features of MySQL such as Views, Stored Procedures, and Triggers.
二、Installation
方法一:安裝MySQL時選擇 FULL 完全安裝
安裝MySQL時選擇FULL 完全安裝, 默認安裝了Sakila sample database
方法二:下載后導入
The Sakila sample database is available from http://dev.mysql.com/doc/index-other.html. A downloadable archive is available in compressed file or Zip format. The archive contains three files: sakila-schema.sql
, sakila-data.sql
, and sakila.mwb
.
The sakila-schema.sql
file contains all the CREATE
statements required to create the structure of the Sakila database including tables, views, stored procedures, and triggers.
The sakila-data.sql
file contains the INSERT
statements required to populate the structure created by the sakila-schema.sql
file, along with definitions for triggers that must be created after the initial data load.
The sakila.mwb
file is a MySQL Workbench data model that you can open within MySQL Workbench to examine the database structure. For more information, see MySQL Workbench.
下載地址:
ZIP格式:http://downloads.mysql.com/docs/sakila-db.zip
tar格式 http://downloads.mysql.com/docs/sakila-db.tar.gz
官方文檔 http://dev.mysql.com/doc/sakila/en/index.html
解壓后得到三個文件:
1. sakila-schema.sql 文件包含創建Sakila數據庫的結構:表、視圖、存儲過程和觸發器
2. sakila-data.sql文件包含:使用 INSERT語句填充數據及在初始數據加載后,必須創建的觸發器的定義
3. sakila.mwb文件是一個MySQL Workbench數據模型,可以在MySQL的工作台打開查看數據庫結構。
導入參考: http://dev.mysql.com/doc/sakila/en/sakila-installation.html
shell> mysql -u root -p
mysql> SOURCE C:/temp/sakila-db/sakila-schema.sql;
mysql> SOURCE C:/temp/sakila-db/sakila-data.sql;
查看導入結果
三、Structure
Sakila樣例數據庫包括16張表格,7個視圖,3個Stored Procedures,3個Functions,6個Triggers。英文描述點開相應連接即可查看。
全圖:
Customer Data 和 Business Data
Inventory Data
3.1 Tables
3.1.3. The category Table 電影類別表
3.1.8. The film_actor Table 電影和演員中間對應表
3.1.9. The film_category Table 電影和類別的中間對應表
3.1.10. The film_text Table 只包含title和description兩列的電影表子表,用以快速檢索
3.1.11. The inventory Table 庫存表
3.1.12. The language Table 語言表
3.1.15. The staff Table 工作人員表
【01 演員表 actor】
演員表列出了所有演員的信息。演員表和電影表之間是多對多的關系,通過film_actor表建立關系
1 2 3 4 |
actor_id: 代理主鍵用於唯一標識表中的每個演員 first_name: 演員的名字 last_name: 演員的姓氏 last_update: 該行已創建或最近更新的時間 |
【02 地址表 address】
地址表包含客戶、員工和商店的地址信息。地址表的主鍵出現在顧客、 員工、和存儲表的外鍵 。
1 2 3 4 5 6 7 8 |
address_id: 代理主鍵用於唯一標識表中的每個地址 address: 地址的第一行 address2: 一個可選的第二行地址 district: 該地區的所屬地區,這可以是國家,省,縣等 city_id: 指向城市表的外鍵 postal_code: 郵政編碼 phone: 地址的電話號碼 last_update: 該行已創建或最近更新的時間 |
【03 分類表 category】
類別表列出了可以分配到一個電影類別。分類和電影是多對多的關系,通過表film_category建立關系
1 2 3 |
category_id: 代理主鍵用於唯一標識表中的每個類別 name: 類別名稱 last_update: 該行已創建或最近更新的時間 |
【04 城市表 city】
城市表包含的城市名單。城市表使用外鍵來標示國家;在地址表中被作為外鍵來使用。
1 2 3 4 |
city_id: 代理主鍵用於唯一標識表中的每個城市 city: 城市的名字 country_id: 外鍵,用於標示城市所屬的國家 last_update: 該行已創建或最近更新的時間 |
【05 國家表 country】
國家表中包含的國家名單。國家表是指在城市表的外鍵 。
1 2 3 |
country_id: 代理主鍵用於唯一標識表中的每個國家 country: 國家的名稱 last_update: 該行已創建或最近更新的時間 |
【06 客戶表 customer】
客戶表包含了所有客戶的列表 。 客戶表在支付表和租金表被作為外鍵使用;客戶表使用外鍵來表示地址和存儲。
1 2 3 4 5 6 7 8 9 |
customer_id: 代理主鍵用於唯一標識表中的每個客戶 store_id: 一個外鍵,確定客戶所屬的store。 first_name: 客戶的名字 last_name: 客戶的姓氏 email: 客戶的電子郵件地址 address_id: 使用在地址 表的外鍵來確定客戶的地址 active: 表示客戶是否是活躍的客戶 create_date: 顧客被添加到系統中的日期。使用 INSERT 觸發器自動設置。 last_update: 該行已創建或最近更新的時間 |
說明:
active: 此設置為“ FALSE“作為替代客戶徹底刪除。大多數查詢應該有一個 WHERE active = TRUE 字句。
store_id: 此處的客戶不僅限於只由這家商店出租,而是包括客戶常常去逛的商店
【07 電影表 film】
電影表是一個可能在商店庫存的所有影片名單。每部影片的拷貝的實際庫存信息保存在庫存表。電影表指使用外鍵來標示語言表;在film_category、film_actor和庫存表中作為外鍵使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
film_id: 代理主鍵用於唯一標識表中的每個電影 title: 影片的標題 description: 一個簡短的描述或電影的情節摘要 release_year: 電影發行的年份 language_id: 使用外鍵來標示語言 original_language_id: 電影的原始語音。使用外鍵來標示語言 rental_duration: 租賃期限的長短,以天作為單位 rental_rate: 指定的期限內電影的租金 length: 影片的長度,以分鍾為單位。 replacement_cost: 如果電影未被歸還或損壞狀態向客戶收取的款項 rating: 分配給電影評級。可以是 G, PG,PG - 13 , R 或NC - 17 special_features: 包括DVD上常見的特殊功能的列表 last_update: 該行已創建或最近更新的時間 |
特殊功能包括零個或多個拖車、評論、刪剪片段、幕后。
【08 film_actor表】
film_actor表是用來支持許多電影和演員之間的多對多關系 。對於每一個給定的電影演員,將有film_actor表中列出的演員和電影中的一個行 。
film_actor表指的是使用外鍵的電影和演員表。
1 2 3 |
actor_id: 用於識別演員的外鍵 film_id: 用於識別電影的外鍵 last_update: 該行已創建或最近更新的時間 |
【09 film_category表】
film_category表是用來支持許多電影和類別之間的多對多關系 。應用於電影的每個類別中,將有film_category表中列出的類別和電影中的一個行。
film_category表是指使用外鍵 的 電影 和類別表 。
1 2 3 |
film_id: 用於識別電影的外鍵 category_id: 用於識別類別的外鍵 last_update: 該行已創建或最近更新的時間 |
【10 film_text表】
film_text表是Sakila樣例數據庫唯一使用MyISAM存儲引擎的表。MyISAM類型不支持事務處理等高級處理,而InnoDB類型支持。MyISAM類型的表強調的是性能,其執行數度比InnoDB類型更快。此表提供允許全文搜索電影表中列出的影片的標題和描述。film_text表包含的film_id,標題和描述的列電影表,保存的內容與電影表上的內容同步(指電影表的插入、更新和刪除操作)
1 2 3 |
film_id: 代理主鍵用於唯一標識表中的每個電影 title: 影片的標題 description: 一個簡短的描述或電影的情節摘要 |
注意:film_text表的內容不應該直接修改。所有的變更來自於電影表 。
【11 庫存表 inventory】
庫存表的一行為存放在一個給定的商店里的一個給定的電影的copy副本。庫存表是使用外鍵來識別電影和存儲;在出租表中使用外鍵來識別庫存。
1 2 3 4 |
inventory_id: 理主鍵用於唯一標識每個項目在庫存 film_id: 使用外鍵來識別電影 store_id: 使用外鍵來識別物品所在的商店 last_update: 該行已創建或最近更新的時間 |
【12 語言表 language】
語言表是一個查找表,列出可能使用的語言,電影可以有自己的語言和原始語言值 。
語言表在電壓表中被作為外鍵來使用。
1 2 3 |
language_id: 代理主鍵用於唯一標識每一種語言 name: 語言的英文名稱 last_update: 該行已創建或最近更新的時間 |
【13 付款表 payment】
付款表記錄每個客戶的付款,如支付的金額和租金的資料。
付款表使用外鍵來表示客戶、出租、和工作人員。
1 2 3 4 5 6 7 |
payment_id: 代理主鍵用於唯一標識每個付款 customer_id: 使用外鍵來標識付款的客戶 staff_id: 工作人員,負責處理支付 。使用外鍵來標識 rental_id: 租借ID, 外鍵,參照rental表 amount: 付款金額 payment_date: 處理付款的日期 last_update: 該行已創建或最近更新的時間 |
【14 租金表 rental】
租借表的一行表示每個inventory的租借客戶、租借時間、歸還時間
租借表是使用外鍵來標識庫存 ,顧客 和工作人員;在支付表中使用了外鍵來標識租金 。
1 2 3 4 5 6 7 |
rental_id: 代理主鍵唯一標識的租金 rental_date: 該項目租用的日期和時間 inventory_id: 該項目被租用 customer_id: 租用該項目的客戶 return_date: 歸還日期 staff_id: 處理該項業務的工作人員 last_update: 該行已創建或最近更新的時間 |
【15 工作人員表 staff】
工作人員表列出了所有的工作人員,包括電子郵件地址,登錄信息和圖片信息 。
工作人員表是指使用外鍵來標識存儲和地址表;在出租、支付和商店表中作為外鍵。
1 2 3 4 5 6 7 8 9 10 11 12 |
staff_id: 代理主鍵唯一標識的工作人員 first_name: 工作人員的名字 last_name: 工作人員的姓氏 address_id: 工作人員的地址在地址表的外鍵 picture: 工作人員的照片,使用了 BLOB屬性 email: 工作人員的電子郵件地址 store_id: 工作人員所在的商店,用外鍵標識 active: 是否是活躍的工作人員。 username: 用戶名,由工作人員用來訪問租賃系統 password: 工作人員訪問租賃系統所使用的密碼。使用了 SHA1 函數 last_update: 該行已創建或最近更新的時間 active: 是否有效,刪除時設置為False |
【16 商店表 store】
store表列出了系統中的所有商店 。
store使用外鍵來標識工作人員和地址;在員工、客戶、庫存表被作為外鍵使用。
1 2 3 4 |
store_id: 代理主鍵唯一標識的商店 manager_staff_id: 使用外鍵來標識這家商店的經理 address_id: 使用外鍵來確定這家店的地址 last_update: 該行已創建或最近更新的時間 |
3.2 Views
actor_info
視圖提供了所有演員的列表及所演的電影, 電影按category分組.
actor →
film_actor →
film
→ film_category →
category
SELECT a.actor_id, a.first_name, a.last_name, GROUP_CONCAT(DISTINCT CONCAT(c.name, ': ', (SELECT GROUP_CONCAT(f.title ORDER BY f.title SEPARATOR ', ') FROM sakila.film f INNER JOIN sakila.film_category fc ON f.film_id = fc.film_id INNER JOIN sakila.film_actor fa ON f.film_id = fa.film_id WHERE fc.category_id = c.category_id AND fa.actor_id = a.actor_id ) ) ORDER BY c.name SEPARATOR '; ') AS film_info FROM sakila.actor a LEFT JOIN sakila.film_actor fa ON a.actor_id = fa.actor_id LEFT JOIN sakila.film_category fc ON fa.film_id = fc.film_id LEFT JOIN sakila.category c ON fc.category_id = c.category_id GROUP BY a.actor_id, a.first_name, a.last_name
客戶列表,firstname和lastname連接成fullname,將
address
, city
, country
集成在一個視圖里
customer
→ address
→ city →
country
SELECT cu.customer_id AS ID, CONCAT( cu.first_name, _utf8 ' ', cu.last_name ) AS NAME, a.address AS address, a.postal_code AS `zip code`, a.phone AS phone, city.city AS city, country.country AS country, IF ( cu.active, _utf8 'active', _utf8 '' ) AS notes, cu.store_id AS SID FROM customer AS cu JOIN address AS a ON cu.address_id = a.address_id JOIN city ON a.city_id = city.city_id JOIN country ON city.country_id = country.country_id
電影列表視圖,包含了每一部電影的信息及電影所對應的演員。電影對應的演員以逗號作為分隔符。連接了 film
, film_category
, category,
and film_actor
actor
表的數據
SELECT film.film_id AS FID, film.title AS title, film.description AS description, category. NAME AS category, film.rental_rate AS price, film.length AS length, film.rating AS rating, GROUP_CONCAT( CONCAT( actor.first_name, _utf8 ' ', actor.last_name ) SEPARATOR ', ' ) AS actors FROM category LEFT JOIN film_category ON category.category_id = film_category.category_id LEFT JOIN film ON film_category.film_id = film.film_id JOIN film_actor ON film.film_id = film_actor.film_id JOIN actor ON film_actor.actor_id = actor.actor_id GROUP BY film.film_id
3.2.4. The nicer_but_slower_film_list View
電影列表視圖,包含了每一部電影的信息及電影所對應的演員。電影對應的演員以逗號作為分隔符。連接了 film
, film_category
, category,
and film_actor
actor
表的數據。和The film_list View不同,演員名字只有單詞首字母大寫了。
SELECT film.film_id AS FID, film.title AS title, film.description AS description, category. NAME AS category, film.rental_rate AS price, film.length AS length, film.rating AS rating, GROUP_CONCAT( CONCAT( CONCAT( UCASE( SUBSTR(actor.first_name, 1, 1) ), LCASE( SUBSTR( actor.first_name, 2, LENGTH(actor.first_name) ) ), _utf8 ' ', CONCAT( UCASE( SUBSTR(actor.last_name, 1, 1) ), LCASE( SUBSTR( actor.last_name, 2, LENGTH(actor.last_name) ) ) ) ) ) SEPARATOR ', ' ) AS actors FROM category LEFT JOIN film_category ON category.category_id = film_category.category_id LEFT JOIN film ON film_category.film_id = film.film_id JOIN film_actor ON film.film_id = film_actor.film_id JOIN actor ON film_actor.actor_id = actor.actor_id GROUP BY film.film_id
3.2.5. The sales_by_film_category View
每個電影種類的銷售額 , payment →
rental
→
inventory
→
film
→
film_category
→
category
SELECT c.name AS category , SUM(p.amount) AS total_sales FROM payment AS p INNER JOIN rental AS r ON p.rental_id = r.rental_id INNER JOIN inventory AS i ON r.inventory_id = i.inventory_id INNER JOIN film AS f ON i.film_id = f.film_id INNER JOIN film_category AS fc ON f.film_id = fc.film_id INNER JOIN category AS c ON fc.category_id = c.category_id GROUP BY c.name ORDER BY total_sales DESC
3.2.6. The sales_by_store View
每個商店的manager及銷售額。
payment →
rental
→
inventory
→
store →
staff
SELECT CONCAT(c.city, _utf8',', cy.country) AS store , CONCAT(m.first_name, _utf8' ', m.last_name) AS manager , SUM(p.amount) AS total_sales FROM payment AS p INNER JOIN rental AS r ON p.rental_id = r.rental_id INNER JOIN inventory AS i ON r.inventory_id = i.inventory_id INNER JOIN store AS s ON i.store_id = s.store_id INNER JOIN address AS a ON s.address_id = a.address_id INNER JOIN city AS c ON a.city_id = c.city_id INNER JOIN country AS cy ON c.country_id = cy.country_id INNER JOIN staff AS m ON s.manager_staff_id = m.staff_id GROUP BY s.store_id ORDER BY cy.country, c.city
工作人員的列表
SELECT s.staff_id AS ID, CONCAT( s.first_name, _utf8 ' ', s.last_name ) AS NAME, a.address AS address, a.postal_code AS `zip code`, a.phone AS phone, city.city AS city, country.country AS country, s.store_id AS SID FROM staff AS s JOIN address AS a ON s.address_id = a.address_id JOIN city ON a.city_id = city.city_id JOIN country ON city.country_id = country.country_id
3.3 Stored Procedures
3.3.1. The film_in_stock Stored Procedure
Description
The
film_in_stock
stored procedure is used to determine whether any copies of a given film are in stock at a given store.判斷在一個給定的商店里是否有一個給定電影的copy.
Parameters
p_film_id
: The ID of the film to be checked, from thefilm_id
column of thefilm
table.
p_store_id
: The ID of the store to check for, from thestore_id
column of thestore
table.
p_film_count
: AnOUT
parameter that returns a count of the copies of the film in stock.Return Values
This procedure produces a table of inventory ID numbers for the copies of the film in stock, and returns (in the
p_film_count
parameter) a count that indicates the number of rows in that table.
BEGIN SELECT inventory_id FROM inventory WHERE film_id = p_film_id AND store_id = p_store_id AND inventory_in_stock(inventory_id); SELECT FOUND_ROWS() INTO p_film_count; END
3.3.2. The film_not_in_stock Stored Procedure
和The film_in_stock Stored Procedure相反,判斷給定的電影,是否有copy,不在一個給定的商店
Description
The
film_not_in_stock
stored procedure is used to determine whether there are any copies of a given film not in stock (rented out) at a given store.Parameters
p_film_id
: The ID of the film to be checked, from thefilm_id
column of thefilm
table.
p_store_id
: The ID of the store to check for, from thestore_id
column of thestore
table.
p_film_count
: AnOUT
parameter that returns a count of the copies of the film not in stock.Return Values
This procedure produces a table of inventory ID numbers for the copies of the film not in stock, and returns (in the
p_film_count
parameter) a count that indicates the number of rows in that table.
BEGIN SELECT inventory_id FROM inventory WHERE film_id = p_film_id AND store_id = p_store_id AND NOT inventory_in_stock(inventory_id); SELECT FOUND_ROWS() INTO p_film_count; END
3.3.3. The rewards_report Stored Procedure
過去一個月,數量大於輸入參數min_monthly_purchases ,消費金額大於輸入參數
min_dollar_amount_purchased的客戶
Description
The
rewards_report
stored procedure generates a customizable list of the top customers for the previous month.Parameters
min_monthly_purchases
: The minimum number of purchases or rentals a customer needed to make in the last month to qualify.
min_dollar_amount_purchased
: The minimum dollar amount a customer needed to spend in the last month to qualify.
count_rewardees
: AnOUT
parameter that returns a count of the customers who met the qualifications specified.Return Values
This function returns a table of customers who met the qualifications specified. The table has the same structure as the
customer
table. The procedure also returns (in thecount_rewardees
parameter) a count that indicates the number of rows in that table.Stop Service net stop uxsms
Start Service net start uxsms
Disable Service sc config uxsms start= disabled
Enable Service sc config uxsms start= auto
proc: BEGIN DECLARE last_month_start DATE; DECLARE last_month_end DATE; /* Some sanity checks... */ IF min_monthly_purchases = 0 THEN SELECT 'Minimum monthly purchases parameter must be > 0'; LEAVE proc; END IF; IF min_dollar_amount_purchased = 0.00 THEN SELECT 'Minimum monthly dollar amount purchased parameter must be > $0.00'; LEAVE proc; END IF; /* Determine start and end time periods */ SET last_month_start = DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH); SET last_month_start = STR_TO_DATE(CONCAT(YEAR(last_month_start),'-',MONTH(last_month_start),'-01'),'%Y-%m-%d'); SET last_month_end = LAST_DAY(last_month_start); /* Create a temporary storage area for Customer IDs. */ CREATE TEMPORARY TABLE tmpCustomer (customer_id SMALLINT UNSIGNED NOT NULL PRIMARY KEY); /* Find all customers meeting the monthly purchase requirements */ INSERT INTO tmpCustomer (customer_id) SELECT p.customer_id FROM payment AS p WHERE DATE(p.payment_date) BETWEEN last_month_start AND last_month_end GROUP BY customer_id HAVING SUM(p.amount) > min_dollar_amount_purchased AND COUNT(customer_id) > min_monthly_purchases; /* Populate OUT parameter with count of found customers */ SELECT COUNT(*) FROM tmpCustomer INTO count_rewardees; /* Output ALL customer information of matching rewardees. Customize output as needed. */ SELECT c.* FROM tmpCustomer AS t INNER JOIN customer AS c ON t.customer_id = c.customer_id; /* Clean up */ DROP TABLE tmpCustomer; END
因為不滿足系統時間,所以結果為0
3.4 Stored Functions
3.4.1. The get_customer_balance Function
返回指定客戶到某一個日期欠的錢。 所有的rental_rate +超期費用 - 所有的payment
Description
The
get_customer_balance
function returns the current amount owing on a specified customer's account.Parameters
p_customer_id
: The ID of the customer to check, from thecustomer_id
column of thecustomer
table.
p_effective_date
: The cutoff date for items that will be applied to the balance. Any rentals, payments, and so forth after this date are not counted.Return Values
This function returns the amount owing on the customer's account.
BEGIN #OK, WE NEED TO CALCULATE THE CURRENT BALANCE GIVEN A CUSTOMER_ID AND A DATE #THAT WE WANT THE BALANCE TO BE EFFECTIVE FOR. THE BALANCE IS: # 1) RENTAL FEES FOR ALL PREVIOUS RENTALS # 2) ONE DOLLAR FOR EVERY DAY THE PREVIOUS RENTALS ARE OVERDUE # 3) IF A FILM IS MORE THAN RENTAL_DURATION * 2 OVERDUE, CHARGE THE REPLACEMENT_COST # 4) SUBTRACT ALL PAYMENTS MADE BEFORE THE DATE SPECIFIED DECLARE v_rentfees DECIMAL(5,2); #FEES PAID TO RENT THE VIDEOS INITIALLY DECLARE v_overfees INTEGER; #LATE FEES FOR PRIOR RENTALS DECLARE v_payments DECIMAL(5,2); #SUM OF PAYMENTS MADE PREVIOUSLY SELECT IFNULL(SUM(film.rental_rate),0) INTO v_rentfees FROM film, inventory, rental WHERE film.film_id = inventory.film_id AND inventory.inventory_id = rental.inventory_id AND rental.rental_date <= p_effective_date AND rental.customer_id = p_customer_id; SELECT IFNULL(SUM(IF((TO_DAYS(rental.return_date) - TO_DAYS(rental.rental_date)) > film.rental_duration, ((TO_DAYS(rental.return_date) - TO_DAYS(rental.rental_date)) - film.rental_duration),0)),0) INTO v_overfees FROM rental, inventory, film WHERE film.film_id = inventory.film_id AND inventory.inventory_id = rental.inventory_id AND rental.rental_date <= p_effective_date AND rental.customer_id = p_customer_id; SELECT IFNULL(SUM(payment.amount),0) INTO v_payments FROM payment WHERE payment.payment_date <= p_effective_date AND payment.customer_id = p_customer_id; RETURN v_rentfees + v_overfees - v_payments; END
3.4.2. The inventory_held_by_customer Function
返回指定的inventory現在在那個客戶的customer_id
The
inventory_held_by_customer
function returns thecustomer_id
of the customer who has rented out the specified inventory item.Parameters
p_inventory_id
: The ID of the inventory item to be checked.Return Values
This function returns the
customer_id
of the customer who is currently renting the item, orNULL
if the item is in stock.
BEGIN DECLARE v_customer_id INT; DECLARE EXIT HANDLER FOR NOT FOUND RETURN NULL; SELECT customer_id INTO v_customer_id FROM rental WHERE return_date IS NULL AND inventory_id = p_inventory_id; RETURN v_customer_id; END
3.4.3. The inventory_in_stock Function
返回指定的inventory是否在商店里
The
inventory_in stock function
returns a boolean value indicating whether the inventory item specified is in stock.Parameters
p_inventory_id
: The ID of the inventory item to be checked.Return Values
This function returns
TRUE
orFALSE
to indicate whether the item specified is in stock.
BEGIN DECLARE v_rentals INT; DECLARE v_out INT; #AN ITEM IS IN-STOCK IF THERE ARE EITHER NO ROWS IN THE rental TABLE #FOR THE ITEM OR ALL ROWS HAVE return_date POPULATED SELECT COUNT(*) INTO v_rentals FROM rental WHERE inventory_id = p_inventory_id; IF v_rentals = 0 THEN RETURN TRUE; END IF; SELECT COUNT(rental_id) INTO v_out FROM inventory LEFT JOIN rental USING(inventory_id) WHERE inventory.inventory_id = p_inventory_id AND rental.return_date IS NULL; IF v_out > 0 THEN RETURN FALSE; ELSE RETURN TRUE; END IF; END
3.5 Triggers
3.5.1. The customer_create_date Trigger
插入新客戶時,設置create_date為當前日期
-- Trigger to enforce create dates on INSERT CREATE TRIGGER customer_create_date BEFORE INSERT ON customer FOR EACH ROW SET NEW.create_date = NOW();
3.5.2. The payment_date Trigger
payment新增記錄時,payment_date設置為當前日期
-- Trigger to enforce payment_date during INSERT CREATE TRIGGER payment_date BEFORE INSERT ON payment FOR EACH ROW SET NEW.payment_date = NOW();
3.5.3. The rental_date Trigger
rental表新增記錄時,rental_date設置為當前日期
-- Trigger to enforce rental_date on INSERT CREATE TRIGGER rental_date BEFORE INSERT ON rental FOR EACH ROW SET NEW.rental_date = NOW();
確保 film_text 和 film同步插入
CREATE TRIGGER `ins_film` AFTER INSERT ON `film` FOR EACH ROW BEGIN INSERT INTO film_text (film_id, title, description) VALUES (new.film_id, new.title, new.description); END;;
確保 film_text 和 film同步更新
CREATE TRIGGER `upd_film` AFTER UPDATE ON `film` FOR EACH ROW BEGIN IF (old.title != new.title) OR (old.description != new.description) OR (old.film_id != new.film_id) THEN UPDATE film_text SET title=new.title, description=new.description, film_id=new.film_id WHERE film_id=old.film_id; END IF; END;;
確保 film_text 和 film同步刪除
CREATE TRIGGER `del_film` AFTER DELETE ON `film` FOR EACH ROW BEGIN DELETE FROM film_text WHERE film_id = old.film_id; END;;