A C++ DAL / ORM code generation framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

643 lines
22 KiB

13 years ago
  1. -- Sakila Sample Database Schema
  2. -- Version 0.8
  3. -- Copyright (c) 2006, MySQL AB
  4. -- All rights reserved.
  5. -- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. -- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. -- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. -- * Neither the name of MySQL AB nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10. SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
  11. SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
  12. SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
  13. DROP SCHEMA IF EXISTS sakila;
  14. CREATE SCHEMA sakila;
  15. USE sakila;
  16. --
  17. -- Table structure for table `actor`
  18. --
  19. CREATE TABLE actor (
  20. actor_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  21. first_name VARCHAR(45) NOT NULL,
  22. last_name VARCHAR(45) NOT NULL,
  23. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  24. PRIMARY KEY (actor_id),
  25. KEY idx_actor_last_name (last_name)
  26. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  27. --
  28. -- Table structure for table `address`
  29. --
  30. CREATE TABLE address (
  31. address_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  32. address VARCHAR(50) NOT NULL,
  33. address2 VARCHAR(50) DEFAULT NULL,
  34. district VARCHAR(20) NOT NULL,
  35. city_id SMALLINT UNSIGNED NOT NULL,
  36. postal_code VARCHAR(10) DEFAULT NULL,
  37. phone VARCHAR(20) NOT NULL,
  38. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  39. PRIMARY KEY (address_id),
  40. KEY idx_fk_city_id (city_id),
  41. CONSTRAINT `fk_address_city` FOREIGN KEY (city_id) REFERENCES city (city_id) ON DELETE RESTRICT ON UPDATE CASCADE
  42. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  43. --
  44. -- Table structure for table `category`
  45. --
  46. CREATE TABLE category (
  47. category_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  48. name VARCHAR(25) NOT NULL,
  49. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  50. PRIMARY KEY (category_id)
  51. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  52. --
  53. -- Table structure for table `city`
  54. --
  55. CREATE TABLE city (
  56. city_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  57. city VARCHAR(50) NOT NULL,
  58. country_id SMALLINT UNSIGNED NOT NULL,
  59. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  60. PRIMARY KEY (city_id),
  61. KEY idx_fk_country_id (country_id),
  62. CONSTRAINT `fk_city_country` FOREIGN KEY (country_id) REFERENCES country (country_id) ON DELETE RESTRICT ON UPDATE CASCADE
  63. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  64. --
  65. -- Table structure for table `country`
  66. --
  67. CREATE TABLE country (
  68. country_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  69. country VARCHAR(50) NOT NULL,
  70. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  71. PRIMARY KEY (country_id)
  72. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  73. --
  74. -- Table structure for table `customer`
  75. --
  76. CREATE TABLE customer (
  77. customer_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  78. store_id TINYINT UNSIGNED NOT NULL,
  79. first_name VARCHAR(45) NOT NULL,
  80. last_name VARCHAR(45) NOT NULL,
  81. email VARCHAR(50) DEFAULT NULL,
  82. address_id SMALLINT UNSIGNED NOT NULL,
  83. active BOOLEAN NOT NULL DEFAULT TRUE,
  84. create_date DATETIME NOT NULL,
  85. last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  86. PRIMARY KEY (customer_id),
  87. KEY idx_fk_store_id (store_id),
  88. KEY idx_fk_address_id (address_id),
  89. KEY idx_last_name (last_name),
  90. CONSTRAINT fk_customer_address FOREIGN KEY (address_id) REFERENCES address (address_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  91. CONSTRAINT fk_customer_store FOREIGN KEY (store_id) REFERENCES store (store_id) ON DELETE RESTRICT ON UPDATE CASCADE
  92. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  93. --
  94. -- Table structure for table `film`
  95. --
  96. CREATE TABLE film (
  97. film_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  98. title VARCHAR(255) NOT NULL,
  99. description TEXT DEFAULT NULL,
  100. release_year YEAR DEFAULT NULL,
  101. language_id TINYINT UNSIGNED NOT NULL,
  102. original_language_id TINYINT UNSIGNED DEFAULT NULL,
  103. rental_duration TINYINT UNSIGNED NOT NULL DEFAULT 3,
  104. rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
  105. length SMALLINT UNSIGNED DEFAULT NULL,
  106. replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
  107. rating ENUM('G','PG','PG-13','R','NC-17') DEFAULT 'G',
  108. special_features SET('Trailers','Commentaries','Deleted Scenes','Behind the Scenes') DEFAULT NULL,
  109. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  110. PRIMARY KEY (film_id),
  111. KEY idx_title (title),
  112. KEY idx_fk_language_id (language_id),
  113. KEY idx_fk_original_language_id (original_language_id),
  114. CONSTRAINT fk_film_language FOREIGN KEY (language_id) REFERENCES language (language_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  115. CONSTRAINT fk_film_language_original FOREIGN KEY (original_language_id) REFERENCES language (language_id) ON DELETE RESTRICT ON UPDATE CASCADE
  116. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  117. --
  118. -- Table structure for table `film_actor`
  119. --
  120. CREATE TABLE film_actor (
  121. actor_id SMALLINT UNSIGNED NOT NULL,
  122. film_id SMALLINT UNSIGNED NOT NULL,
  123. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  124. PRIMARY KEY (actor_id,film_id),
  125. KEY idx_fk_film_id (`film_id`),
  126. CONSTRAINT fk_film_actor_actor FOREIGN KEY (actor_id) REFERENCES actor (actor_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  127. CONSTRAINT fk_film_actor_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON DELETE RESTRICT ON UPDATE CASCADE
  128. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  129. --
  130. -- Table structure for table `film_category`
  131. --
  132. CREATE TABLE film_category (
  133. film_id SMALLINT UNSIGNED NOT NULL,
  134. category_id TINYINT UNSIGNED NOT NULL,
  135. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  136. PRIMARY KEY (film_id, category_id),
  137. CONSTRAINT fk_film_category_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  138. CONSTRAINT fk_film_category_category FOREIGN KEY (category_id) REFERENCES category (category_id) ON DELETE RESTRICT ON UPDATE CASCADE
  139. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  140. --
  141. -- Table structure for table `film_text`
  142. --
  143. CREATE TABLE film_text (
  144. film_id SMALLINT NOT NULL,
  145. title VARCHAR(255) NOT NULL,
  146. description TEXT,
  147. PRIMARY KEY (film_id),
  148. FULLTEXT KEY idx_title_description (title,description)
  149. )ENGINE=MyISAM DEFAULT CHARSET=utf8;
  150. --
  151. -- Triggers for loading film_text from film
  152. --
  153. DELIMITER ;;
  154. CREATE TRIGGER `ins_film` AFTER INSERT ON `film` FOR EACH ROW BEGIN
  155. INSERT INTO film_text (film_id, title, description)
  156. VALUES (new.film_id, new.title, new.description);
  157. END;;
  158. CREATE TRIGGER `upd_film` AFTER UPDATE ON `film` FOR EACH ROW BEGIN
  159. IF (old.title != new.title) or (old.description != new.description)
  160. THEN
  161. UPDATE film_text
  162. SET title=new.title,
  163. description=new.description,
  164. film_id=new.film_id
  165. WHERE film_id=old.film_id;
  166. END IF;
  167. END;;
  168. CREATE TRIGGER `del_film` AFTER DELETE ON `film` FOR EACH ROW BEGIN
  169. DELETE FROM film_text WHERE film_id = old.film_id;
  170. END;;
  171. DELIMITER ;
  172. --
  173. -- Table structure for table `inventory`
  174. --
  175. CREATE TABLE inventory (
  176. inventory_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
  177. film_id SMALLINT UNSIGNED NOT NULL,
  178. store_id TINYINT UNSIGNED NOT NULL,
  179. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  180. PRIMARY KEY (inventory_id),
  181. KEY idx_fk_film_id (film_id),
  182. KEY idx_store_id_film_id (store_id,film_id),
  183. CONSTRAINT fk_inventory_store FOREIGN KEY (store_id) REFERENCES store (store_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  184. CONSTRAINT fk_inventory_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON DELETE RESTRICT ON UPDATE CASCADE
  185. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  186. --
  187. -- Table structure for table `language`
  188. --
  189. CREATE TABLE language (
  190. language_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  191. name CHAR(20) NOT NULL,
  192. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  193. PRIMARY KEY (language_id)
  194. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  195. --
  196. -- Table structure for table `payment`
  197. --
  198. CREATE TABLE payment (
  199. payment_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  200. customer_id SMALLINT UNSIGNED NOT NULL,
  201. staff_id TINYINT UNSIGNED NOT NULL,
  202. rental_id INT DEFAULT NULL,
  203. amount DECIMAL(5,2) NOT NULL,
  204. payment_date DATETIME NOT NULL,
  205. last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  206. PRIMARY KEY (payment_id),
  207. KEY idx_fk_staff_id (staff_id),
  208. KEY idx_fk_customer_id (customer_id),
  209. CONSTRAINT fk_payment_rental FOREIGN KEY (rental_id) REFERENCES rental (rental_id) ON DELETE SET NULL ON UPDATE CASCADE,
  210. CONSTRAINT fk_payment_customer FOREIGN KEY (customer_id) REFERENCES customer (customer_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  211. CONSTRAINT fk_payment_staff FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ON DELETE RESTRICT ON UPDATE CASCADE
  212. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  213. --
  214. -- Table structure for table `rental`
  215. --
  216. CREATE TABLE rental (
  217. rental_id INT NOT NULL AUTO_INCREMENT,
  218. rental_date DATETIME NOT NULL,
  219. inventory_id MEDIUMINT UNSIGNED NOT NULL,
  220. customer_id SMALLINT UNSIGNED NOT NULL,
  221. return_date DATETIME DEFAULT NULL,
  222. staff_id TINYINT UNSIGNED NOT NULL,
  223. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  224. PRIMARY KEY (rental_id),
  225. UNIQUE KEY (rental_date,inventory_id,customer_id),
  226. KEY idx_fk_inventory_id (inventory_id),
  227. KEY idx_fk_customer_id (customer_id),
  228. KEY idx_fk_staff_id (staff_id),
  229. CONSTRAINT fk_rental_staff FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  230. CONSTRAINT fk_rental_inventory FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  231. CONSTRAINT fk_rental_customer FOREIGN KEY (customer_id) REFERENCES customer (customer_id) ON DELETE RESTRICT ON UPDATE CASCADE
  232. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  233. --
  234. -- Table structure for table `staff`
  235. --
  236. CREATE TABLE staff (
  237. staff_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  238. first_name VARCHAR(45) NOT NULL,
  239. last_name VARCHAR(45) NOT NULL,
  240. address_id SMALLINT UNSIGNED NOT NULL,
  241. picture BLOB DEFAULT NULL,
  242. email VARCHAR(50) DEFAULT NULL,
  243. store_id TINYINT UNSIGNED NOT NULL,
  244. active BOOLEAN NOT NULL DEFAULT TRUE,
  245. username VARCHAR(16) NOT NULL,
  246. password VARCHAR(40) BINARY DEFAULT NULL,
  247. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  248. PRIMARY KEY (staff_id),
  249. KEY idx_fk_store_id (store_id),
  250. KEY idx_fk_address_id (address_id),
  251. CONSTRAINT fk_staff_store FOREIGN KEY (store_id) REFERENCES store (store_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  252. CONSTRAINT fk_staff_address FOREIGN KEY (address_id) REFERENCES address (address_id) ON DELETE RESTRICT ON UPDATE CASCADE
  253. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  254. --
  255. -- Table structure for table `store`
  256. --
  257. CREATE TABLE store (
  258. store_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  259. manager_staff_id TINYINT UNSIGNED NOT NULL,
  260. address_id SMALLINT UNSIGNED NOT NULL,
  261. last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  262. PRIMARY KEY (store_id),
  263. UNIQUE KEY idx_unique_manager (manager_staff_id),
  264. KEY idx_fk_address_id (address_id),
  265. CONSTRAINT fk_store_staff FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  266. CONSTRAINT fk_store_address FOREIGN KEY (address_id) REFERENCES address (address_id) ON DELETE RESTRICT ON UPDATE CASCADE
  267. )ENGINE=InnoDB DEFAULT CHARSET=utf8;
  268. --
  269. -- View structure for view `customer_list`
  270. --
  271. CREATE VIEW customer_list
  272. AS
  273. 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`,
  274. 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
  275. FROM customer AS cu JOIN address AS a ON cu.address_id = a.address_id JOIN city ON a.city_id = city.city_id
  276. JOIN country ON city.country_id = country.country_id;
  277. --
  278. -- View structure for view `film_list`
  279. --
  280. CREATE VIEW film_list
  281. AS
  282. SELECT film.film_id AS FID, film.title AS title, film.description AS description, category.name AS category, film.rental_rate AS price,
  283. film.length AS length, film.rating AS rating, GROUP_CONCAT(CONCAT(actor.first_name, _utf8' ', actor.last_name) SEPARATOR ', ') AS actors
  284. 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
  285. JOIN film_actor ON film.film_id = film_actor.film_id
  286. JOIN actor ON film_actor.actor_id = actor.actor_id
  287. GROUP BY film.film_id;
  288. --
  289. -- View structure for view `nicer_but_slower_film_list`
  290. --
  291. CREATE VIEW nicer_but_slower_film_list
  292. AS
  293. SELECT film.film_id AS FID, film.title AS title, film.description AS description, category.name AS category, film.rental_rate AS price,
  294. film.length AS length, film.rating AS rating, GROUP_CONCAT(CONCAT(CONCAT(UCASE(SUBSTR(actor.first_name,1,1)),
  295. LCASE(SUBSTR(actor.first_name,2,LENGTH(actor.first_name))),_utf8' ',CONCAT(UCASE(SUBSTR(actor.last_name,1,1)),
  296. LCASE(SUBSTR(actor.last_name,2,LENGTH(actor.last_name)))))) SEPARATOR ', ') AS actors
  297. 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
  298. JOIN film_actor ON film.film_id = film_actor.film_id
  299. JOIN actor ON film_actor.actor_id = actor.actor_id
  300. GROUP BY film.film_id;
  301. --
  302. -- View structure for view `staff_list`
  303. --
  304. CREATE VIEW staff_list
  305. AS
  306. 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,
  307. city.city AS city, country.country AS country, s.store_id AS SID
  308. FROM staff AS s JOIN address AS a ON s.address_id = a.address_id JOIN city ON a.city_id = city.city_id
  309. JOIN country ON city.country_id = country.country_id;
  310. --
  311. -- View structure for view `sales_by_store`
  312. --
  313. CREATE VIEW sales_by_store
  314. AS
  315. SELECT
  316. CONCAT(c.city, _utf8',', cy.country) AS store
  317. , CONCAT(m.first_name, _utf8' ', m.last_name) AS manager
  318. , SUM(p.amount) AS total_sales
  319. FROM payment AS p
  320. INNER JOIN rental AS r ON p.rental_id = r.rental_id
  321. INNER JOIN inventory AS i ON r.inventory_id = i.inventory_id
  322. INNER JOIN store AS s ON i.store_id = s.store_id
  323. INNER JOIN address AS a ON s.address_id = a.address_id
  324. INNER JOIN city AS c ON a.city_id = c.city_id
  325. INNER JOIN country AS cy ON c.country_id = cy.country_id
  326. INNER JOIN staff AS m ON s.manager_staff_id = m.staff_id
  327. GROUP BY s.store_id
  328. ORDER BY cy.country, c.city;
  329. --
  330. -- View structure for view `sales_by_film_category`
  331. --
  332. -- Note that total sales will add up to >100% because
  333. -- some titles belong to more than 1 category
  334. --
  335. CREATE VIEW sales_by_film_category
  336. AS
  337. SELECT
  338. c.name AS category
  339. , SUM(p.amount) AS total_sales
  340. FROM payment AS p
  341. INNER JOIN rental AS r ON p.rental_id = r.rental_id
  342. INNER JOIN inventory AS i ON r.inventory_id = i.inventory_id
  343. INNER JOIN film AS f ON i.film_id = f.film_id
  344. INNER JOIN film_category AS fc ON f.film_id = fc.film_id
  345. INNER JOIN category AS c ON fc.category_id = c.category_id
  346. GROUP BY c.name
  347. ORDER BY total_sales DESC;
  348. --
  349. -- View structure for view `actor_info`
  350. --
  351. CREATE DEFINER=CURRENT_USER SQL SECURITY INVOKER VIEW actor_info
  352. AS
  353. SELECT
  354. a.actor_id,
  355. a.first_name,
  356. a.last_name,
  357. GROUP_CONCAT(DISTINCT CONCAT(c.name, ': ',
  358. (SELECT GROUP_CONCAT(f.title ORDER BY f.title SEPARATOR ', ')
  359. FROM sakila.film f
  360. INNER JOIN sakila.film_category fc
  361. ON f.film_id = fc.film_id
  362. INNER JOIN sakila.film_actor fa
  363. ON f.film_id = fa.film_id
  364. WHERE fc.category_id = c.category_id
  365. AND fa.actor_id = a.actor_id
  366. )
  367. )
  368. ORDER BY c.name SEPARATOR '; ')
  369. AS film_info
  370. FROM sakila.actor a
  371. LEFT JOIN sakila.film_actor fa
  372. ON a.actor_id = fa.actor_id
  373. LEFT JOIN sakila.film_category fc
  374. ON fa.film_id = fc.film_id
  375. LEFT JOIN sakila.category c
  376. ON fc.category_id = c.category_id
  377. GROUP BY a.actor_id, a.first_name, a.last_name;
  378. --
  379. -- Procedure structure for procedure `rewards_report`
  380. --
  381. DELIMITER //
  382. CREATE PROCEDURE rewards_report (
  383. IN min_monthly_purchases TINYINT UNSIGNED
  384. , IN min_dollar_amount_purchased DECIMAL(10,2) UNSIGNED
  385. , OUT count_rewardees INT
  386. )
  387. LANGUAGE SQL
  388. NOT DETERMINISTIC
  389. READS SQL DATA
  390. SQL SECURITY DEFINER
  391. COMMENT 'Provides a customizable report on best customers'
  392. proc: BEGIN
  393. DECLARE last_month_start DATE;
  394. DECLARE last_month_end DATE;
  395. /* Some sanity checks... */
  396. IF min_monthly_purchases = 0 THEN
  397. SELECT 'Minimum monthly purchases parameter must be > 0';
  398. LEAVE proc;
  399. END IF;
  400. IF min_dollar_amount_purchased = 0.00 THEN
  401. SELECT 'Minimum monthly dollar amount purchased parameter must be > $0.00';
  402. LEAVE proc;
  403. END IF;
  404. /* Determine start and end time periods */
  405. SET last_month_start = DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH);
  406. SET last_month_start = STR_TO_DATE(CONCAT(YEAR(last_month_start),'-',MONTH(last_month_start),'-01'),'%Y-%m-%d');
  407. SET last_month_end = LAST_DAY(last_month_start);
  408. /*
  409. Create a temporary storage area for
  410. Customer IDs.
  411. */
  412. CREATE TEMPORARY TABLE tmpCustomer (customer_id SMALLINT UNSIGNED NOT NULL PRIMARY KEY);
  413. /*
  414. Find all customers meeting the
  415. monthly purchase requirements
  416. */
  417. INSERT INTO tmpCustomer (customer_id)
  418. SELECT p.customer_id
  419. FROM payment AS p
  420. WHERE DATE(p.payment_date) BETWEEN last_month_start AND last_month_end
  421. GROUP BY customer_id
  422. HAVING SUM(p.amount) > min_dollar_amount_purchased
  423. AND COUNT(customer_id) > min_monthly_purchases;
  424. /* Populate OUT parameter with count of found customers */
  425. SELECT COUNT(*) FROM tmpCustomer INTO count_rewardees;
  426. /*
  427. Output ALL customer information of matching rewardees.
  428. Customize output as needed.
  429. */
  430. SELECT c.*
  431. FROM tmpCustomer AS t
  432. INNER JOIN customer AS c ON t.customer_id = c.customer_id;
  433. /* Clean up */
  434. DROP TABLE tmpCustomer;
  435. END //
  436. DELIMITER ;
  437. DELIMITER $$
  438. CREATE FUNCTION get_customer_balance(p_customer_id INT, p_effective_date DATETIME) RETURNS DECIMAL(5,2)
  439. DETERMINISTIC
  440. READS SQL DATA
  441. BEGIN
  442. #OK, WE NEED TO CALCULATE THE CURRENT BALANCE GIVEN A CUSTOMER_ID AND A DATE
  443. #THAT WE WANT THE BALANCE TO BE EFFECTIVE FOR. THE BALANCE IS:
  444. # 1) RENTAL FEES FOR ALL PREVIOUS RENTALS
  445. # 2) ONE DOLLAR FOR EVERY DAY THE PREVIOUS RENTALS ARE OVERDUE
  446. # 3) IF A FILM IS MORE THAN RENTAL_DURATION * 2 OVERDUE, CHARGE THE REPLACEMENT_COST
  447. # 4) SUBTRACT ALL PAYMENTS MADE BEFORE THE DATE SPECIFIED
  448. DECLARE v_rentfees DECIMAL(5,2); #FEES PAID TO RENT THE VIDEOS INITIALLY
  449. DECLARE v_overfees INTEGER; #LATE FEES FOR PRIOR RENTALS
  450. DECLARE v_payments DECIMAL(5,2); #SUM OF PAYMENTS MADE PREVIOUSLY
  451. SELECT IFNULL(SUM(film.rental_rate),0) INTO v_rentfees
  452. FROM film, inventory, rental
  453. WHERE film.film_id = inventory.film_id
  454. AND inventory.inventory_id = rental.inventory_id
  455. AND rental.rental_date <= p_effective_date
  456. AND rental.customer_id = p_customer_id;
  457. SELECT IFNULL(SUM(IF((TO_DAYS(rental.return_date) - TO_DAYS(rental.rental_date)) > film.rental_duration,
  458. ((TO_DAYS(rental.return_date) - TO_DAYS(rental.rental_date)) - film.rental_duration),0)),0) INTO v_overfees
  459. FROM rental, inventory, film
  460. WHERE film.film_id = inventory.film_id
  461. AND inventory.inventory_id = rental.inventory_id
  462. AND rental.rental_date <= p_effective_date
  463. AND rental.customer_id = p_customer_id;
  464. SELECT IFNULL(SUM(payment.amount),0) INTO v_payments
  465. FROM payment
  466. WHERE payment.payment_date <= p_effective_date
  467. AND payment.customer_id = p_customer_id;
  468. RETURN v_rentfees + v_overfees - v_payments;
  469. END $$
  470. DELIMITER ;
  471. DELIMITER $$
  472. CREATE PROCEDURE film_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT)
  473. READS SQL DATA
  474. BEGIN
  475. SELECT inventory_id
  476. FROM inventory
  477. WHERE film_id = p_film_id
  478. AND store_id = p_store_id
  479. AND inventory_in_stock(inventory_id);
  480. SELECT FOUND_ROWS() INTO p_film_count;
  481. END $$
  482. DELIMITER ;
  483. DELIMITER $$
  484. CREATE PROCEDURE film_not_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT)
  485. READS SQL DATA
  486. BEGIN
  487. SELECT inventory_id
  488. FROM inventory
  489. WHERE film_id = p_film_id
  490. AND store_id = p_store_id
  491. AND NOT inventory_in_stock(inventory_id);
  492. SELECT FOUND_ROWS() INTO p_film_count;
  493. END $$
  494. DELIMITER ;
  495. DELIMITER $$
  496. CREATE FUNCTION inventory_held_by_customer(p_inventory_id INT) RETURNS INT
  497. READS SQL DATA
  498. BEGIN
  499. DECLARE v_customer_id INT;
  500. DECLARE EXIT HANDLER FOR NOT FOUND RETURN NULL;
  501. SELECT customer_id INTO v_customer_id
  502. FROM rental
  503. WHERE return_date IS NULL
  504. AND inventory_id = p_inventory_id;
  505. RETURN v_customer_id;
  506. END $$
  507. DELIMITER ;
  508. DELIMITER $$
  509. CREATE FUNCTION inventory_in_stock(p_inventory_id INT) RETURNS BOOLEAN
  510. READS SQL DATA
  511. BEGIN
  512. DECLARE v_rentals INT;
  513. DECLARE v_out INT;
  514. #AN ITEM IS IN-STOCK IF THERE ARE EITHER NO ROWS IN THE rental TABLE
  515. #FOR THE ITEM OR ALL ROWS HAVE return_date POPULATED
  516. SELECT COUNT(*) INTO v_rentals
  517. FROM rental
  518. WHERE inventory_id = p_inventory_id;
  519. IF v_rentals = 0 THEN
  520. RETURN TRUE;
  521. END IF;
  522. SELECT COUNT(rental_id) INTO v_out
  523. FROM inventory LEFT JOIN rental USING(inventory_id)
  524. WHERE inventory.inventory_id = p_inventory_id
  525. AND rental.return_date IS NULL;
  526. IF v_out > 0 THEN
  527. RETURN FALSE;
  528. ELSE
  529. RETURN TRUE;
  530. END IF;
  531. END $$
  532. DELIMITER ;
  533. SET SQL_MODE=@OLD_SQL_MODE;
  534. SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
  535. SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;