/jOOQ-examples/Sakila/oracle-sakila-db/oracle-sakila-schema.sql

http://github.com/jOOQ/jOOQ · SQL · 762 lines · 504 code · 136 blank · 122 comment · 0 complexity · bb167bed77b50fcffe6ca8c3421a8858 MD5 · raw file

  1. /*
  2. Sakila for Oracle is a port of the Sakila example database available for MySQL, which was originally developed by Mike Hillyer of the MySQL AB documentation team.
  3. This project is designed to help database administrators to decide which database to use for development of new products
  4. The user can run the same SQL against different kind of databases and compare the performance
  5. License: BSD
  6. Copyright DB Software Laboratory
  7. http://www.etl-tools.com
  8. */
  9. --
  10. -- Table structure for table actor
  11. --
  12. --DROP TABLE actor;
  13. CREATE TABLE actor (
  14. actor_id numeric NOT NULL ,
  15. first_name VARCHAR(45) NOT NULL,
  16. last_name VARCHAR(45) NOT NULL,
  17. last_update DATE NOT NULL,
  18. CONSTRAINT pk_actor PRIMARY KEY (actor_id)
  19. );
  20. CREATE INDEX idx_actor_last_name ON actor(last_name);
  21. --DROP SEQUENCE actor_sequence;
  22. CREATE SEQUENCE actor_sequence;
  23. CREATE OR REPLACE TRIGGER actor_before_trigger
  24. BEFORE INSERT ON actor FOR EACH ROW
  25. BEGIN
  26. IF (:NEW.actor_id IS NULL) THEN
  27. SELECT actor_sequence.nextval INTO :NEW.actor_id
  28. FROM DUAL;
  29. END IF;
  30. :NEW.last_update:=current_date;
  31. END;
  32. /
  33. CREATE OR REPLACE TRIGGER actor_before_update
  34. BEFORE UPDATE ON actor FOR EACH ROW
  35. BEGIN
  36. :NEW.last_update:=current_date;
  37. END;
  38. /
  39. --
  40. -- Table structure for table country
  41. --
  42. CREATE TABLE country (
  43. country_id SMALLINT NOT NULL,
  44. country VARCHAR(50) NOT NULL,
  45. last_update DATE,
  46. CONSTRAINT pk_country PRIMARY KEY (country_id)
  47. );
  48. ---DROP SEQUENCE country_sequence;
  49. CREATE SEQUENCE country_sequence;
  50. CREATE OR REPLACE TRIGGER country_before_trigger
  51. BEFORE INSERT ON country FOR EACH ROW
  52. BEGIN
  53. IF (:NEW.country_id IS NULL) THEN
  54. SELECT country_sequence.nextval INTO :NEW.country_id
  55. FROM DUAL;
  56. END IF;
  57. :NEW.last_update:=current_date;
  58. END;
  59. /
  60. CREATE OR REPLACE TRIGGER country_before_update
  61. BEFORE UPDATE ON country FOR EACH ROW
  62. BEGIN
  63. :NEW.last_update:=current_date;
  64. END;
  65. /
  66. --
  67. -- Table structure for table city
  68. --
  69. CREATE TABLE city (
  70. city_id int NOT NULL,
  71. city VARCHAR(50) NOT NULL,
  72. country_id SMALLINT NOT NULL,
  73. last_update DATE NOT NULL,
  74. CONSTRAINT pk_city PRIMARY KEY (city_id),
  75. CONSTRAINT fk_city_country FOREIGN KEY (country_id) REFERENCES country (country_id)
  76. );
  77. CREATE INDEX idx_fk_country_id ON city(country_id);
  78. --- DROP SEQUENCE city_sequence;
  79. CREATE SEQUENCE city_sequence;
  80. CREATE OR REPLACE TRIGGER city_before_trigger
  81. BEFORE INSERT ON city FOR EACH ROW
  82. BEGIN
  83. IF (:NEW.city_id IS NULL) THEN
  84. SELECT city_sequence.nextval INTO :NEW.city_id
  85. FROM DUAL;
  86. END IF;
  87. :NEW.last_update:=current_date;
  88. END;
  89. /
  90. CREATE OR REPLACE TRIGGER city_before_update
  91. BEFORE UPDATE ON city FOR EACH ROW
  92. BEGIN
  93. :NEW.last_update:=current_date;
  94. END;
  95. /
  96. --
  97. -- Table structure for table address
  98. --
  99. CREATE TABLE address (
  100. address_id int NOT NULL,
  101. address VARCHAR(50) NOT NULL,
  102. address2 VARCHAR(50) DEFAULT NULL,
  103. district VARCHAR(20) NOT NULL,
  104. city_id INT NOT NULL,
  105. postal_code VARCHAR(10) DEFAULT NULL,
  106. phone VARCHAR(20) NOT NULL,
  107. last_update DATE NOT NULL,
  108. CONSTRAINT pk_address PRIMARY KEY (address_id)
  109. );
  110. CREATE INDEX idx_fk_city_id ON address(city_id);
  111. ALTER TABLE address ADD CONSTRAINT fk_address_city FOREIGN KEY (city_id) REFERENCES city (city_id);
  112. --DROP SEQUENCE city_sequence;
  113. CREATE SEQUENCE address_sequence;
  114. CREATE OR REPLACE TRIGGER address_before_trigger
  115. BEFORE INSERT ON address FOR EACH ROW
  116. BEGIN
  117. IF (:NEW.address_id IS NULL) THEN
  118. SELECT address_sequence.nextval INTO :NEW.address_id
  119. FROM DUAL;
  120. END IF;
  121. :NEW.last_update:=current_date;
  122. END;
  123. /
  124. CREATE OR REPLACE TRIGGER address_before_update
  125. BEFORE UPDATE ON address FOR EACH ROW
  126. BEGIN
  127. :NEW.last_update:=current_date;
  128. END;
  129. /
  130. --
  131. -- Table structure for table language
  132. --
  133. CREATE TABLE language (
  134. language_id SMALLINT NOT NULL ,
  135. name CHAR(20) NOT NULL,
  136. last_update DATE NOT NULL,
  137. CONSTRAINT pk_language PRIMARY KEY (language_id)
  138. );
  139. ---DROP SEQUENCE language_sequence;
  140. CREATE SEQUENCE language_sequence;
  141. CREATE OR REPLACE TRIGGER language_before_trigger
  142. BEFORE INSERT ON language FOR EACH ROW
  143. BEGIN
  144. IF (:NEW.language_id IS NULL) THEN
  145. SELECT language_sequence.nextval INTO :NEW.language_id
  146. FROM DUAL;
  147. END IF;
  148. :NEW.last_update:=current_date;
  149. END;
  150. /
  151. CREATE OR REPLACE TRIGGER language_before_update
  152. BEFORE UPDATE ON language FOR EACH ROW
  153. BEGIN
  154. :NEW.last_update:=current_date;
  155. END;
  156. /
  157. --
  158. -- Table structure for table category
  159. --
  160. CREATE TABLE category (
  161. category_id SMALLINT NOT NULL,
  162. name VARCHAR(25) NOT NULL,
  163. last_update DATE NOT NULL,
  164. CONSTRAINT pk_category PRIMARY KEY (category_id)
  165. );
  166. ---DROP SEQUENCE category_sequence;
  167. CREATE SEQUENCE category_sequence;
  168. CREATE OR REPLACE TRIGGER category_before_trigger
  169. BEFORE INSERT ON category FOR EACH ROW
  170. BEGIN
  171. IF (:NEW.category_id IS NULL) THEN
  172. SELECT category_sequence.nextval INTO :NEW.category_id
  173. FROM DUAL;
  174. END IF;
  175. :NEW.last_update:=current_date;
  176. END;
  177. /
  178. CREATE OR REPLACE TRIGGER category_before_update
  179. BEFORE UPDATE ON category FOR EACH ROW
  180. BEGIN
  181. :NEW.last_update:=current_date;
  182. END;
  183. /
  184. --
  185. -- Table structure for table customer
  186. --
  187. CREATE TABLE customer (
  188. customer_id INT NOT NULL,
  189. store_id INT NOT NULL,
  190. first_name VARCHAR(45) NOT NULL,
  191. last_name VARCHAR(45) NOT NULL,
  192. email VARCHAR(50) DEFAULT NULL,
  193. address_id INT NOT NULL,
  194. active CHAR(1) DEFAULT 'Y' NOT NULL,
  195. create_date DATE NOT NULL,
  196. last_update DATE NOT NULL,
  197. CONSTRAINT pk_customer PRIMARY KEY (customer_id),
  198. CONSTRAINT fk_customer_address FOREIGN KEY (address_id) REFERENCES address(address_id)
  199. );
  200. CREATE INDEX idx_customer_fk_store_id ON customer(store_id);
  201. CREATE INDEX idx_customer_fk_address_id ON customer(address_id);
  202. CREATE INDEX idx_customer_last_name ON customer(last_name);
  203. ---DROP SEQUENCE customer_sequence;
  204. CREATE SEQUENCE customer_sequence;
  205. CREATE OR REPLACE TRIGGER customer_before_trigger
  206. BEFORE INSERT ON customer FOR EACH ROW
  207. BEGIN
  208. IF (:NEW.customer_id IS NULL) THEN
  209. SELECT customer_sequence.nextval INTO :NEW.customer_id
  210. FROM DUAL;
  211. END IF;
  212. :NEW.last_update:=current_date;
  213. :NEW.create_date:=current_date;
  214. END;
  215. /
  216. CREATE OR REPLACE TRIGGER customer_before_update
  217. BEFORE UPDATE ON customer FOR EACH ROW
  218. BEGIN
  219. :NEW.last_update:=current_date;
  220. END;
  221. /
  222. --
  223. -- Table structure for table film
  224. --
  225. CREATE TABLE film (
  226. film_id int NOT NULL,
  227. title VARCHAR(255) NOT NULL,
  228. description CLOB DEFAULT NULL,
  229. release_year VARCHAR(4) DEFAULT NULL,
  230. language_id SMALLINT NOT NULL,
  231. original_language_id SMALLINT DEFAULT NULL,
  232. rental_duration SMALLINT DEFAULT 3 NOT NULL,
  233. rental_rate DECIMAL(4,2) DEFAULT 4.99 NOT NULL,
  234. length SMALLINT DEFAULT NULL,
  235. replacement_cost DECIMAL(5,2) DEFAULT 19.99 NOT NULL,
  236. rating VARCHAR(10) DEFAULT 'G',
  237. special_features VARCHAR(100) DEFAULT NULL,
  238. last_update DATE NOT NULL,
  239. CONSTRAINT pk_film PRIMARY KEY (film_id),
  240. CONSTRAINT fk_film_language FOREIGN KEY (language_id) REFERENCES language (language_id) ,
  241. CONSTRAINT fk_film_language_original FOREIGN KEY (original_language_id) REFERENCES language (language_id)
  242. );
  243. ALTER TABLE film ADD CONSTRAINT CHECK_special_features CHECK(special_features is null or
  244. special_features like '%Trailers%' or
  245. special_features like '%Commentaries%' or
  246. special_features like '%Deleted Scenes%' or
  247. special_features like '%Behind the Scenes%');
  248. ALTER TABLE film ADD CONSTRAINT CHECK_special_rating CHECK(rating in ('G','PG','PG-13','R','NC-17'));
  249. CREATE INDEX idx_fk_language_id ON film(language_id);
  250. CREATE INDEX idx_fk_original_language_id ON film(original_language_id);
  251. ---DROP SEQUENCE film_sequence;
  252. CREATE SEQUENCE film_sequence;
  253. CREATE OR REPLACE TRIGGER film_before_trigger
  254. BEFORE INSERT ON film FOR EACH ROW
  255. BEGIN
  256. IF (:NEW.film_id IS NULL) THEN
  257. SELECT film_sequence.nextval INTO :NEW.film_id
  258. FROM DUAL;
  259. END IF;
  260. :NEW.last_update:=current_date;
  261. END;
  262. /
  263. CREATE OR REPLACE TRIGGER film_before_update
  264. BEFORE UPDATE ON film FOR EACH ROW
  265. BEGIN
  266. :NEW.last_update:=current_date;
  267. END;
  268. /
  269. --
  270. -- Table structure for table film_actor
  271. --
  272. CREATE TABLE film_actor (
  273. actor_id INT NOT NULL,
  274. film_id INT NOT NULL,
  275. last_update DATE NOT NULL,
  276. CONSTRAINT pk_film_actor PRIMARY KEY (actor_id,film_id),
  277. CONSTRAINT fk_film_actor_actor FOREIGN KEY (actor_id) REFERENCES actor (actor_id),
  278. CONSTRAINT fk_film_actor_film FOREIGN KEY (film_id) REFERENCES film (film_id)
  279. );
  280. CREATE INDEX idx_fk_film_actor_film ON film_actor(film_id);
  281. CREATE INDEX idx_fk_film_actor_actor ON film_actor(actor_id) ;
  282. CREATE OR REPLACE TRIGGER film_actor_before_trigger
  283. BEFORE INSERT ON film_actor FOR EACH ROW
  284. BEGIN
  285. :NEW.last_update:=current_date;
  286. END;
  287. /
  288. CREATE OR REPLACE TRIGGER film_actor_before_update
  289. BEFORE UPDATE ON film_actor FOR EACH ROW
  290. BEGIN
  291. :NEW.last_update:=current_date;
  292. END;
  293. /
  294. --
  295. -- Table structure for table film_category
  296. --
  297. CREATE TABLE film_category (
  298. film_id INT NOT NULL,
  299. category_id SMALLINT NOT NULL,
  300. last_update DATE NOT NULL,
  301. CONSTRAINT pk_film_category PRIMARY KEY (film_id, category_id),
  302. CONSTRAINT fk_film_category_film FOREIGN KEY (film_id) REFERENCES film (film_id),
  303. CONSTRAINT fk_film_category_category FOREIGN KEY (category_id) REFERENCES category (category_id)
  304. );
  305. CREATE INDEX idx_fk_film_category_film ON film_category(film_id);
  306. CREATE INDEX idx_fk_film_category_category ON film_category(category_id);
  307. CREATE OR REPLACE TRIGGER film_category_before_trigger
  308. BEFORE INSERT ON film_category FOR EACH ROW
  309. BEGIN
  310. :NEW.last_update:=current_date;
  311. END;
  312. /
  313. CREATE OR REPLACE TRIGGER film_category_before_update
  314. BEFORE UPDATE ON film_category FOR EACH ROW
  315. BEGIN
  316. :NEW.last_update:=current_date;
  317. END;
  318. /
  319. --
  320. -- Table structure for table film_text
  321. --
  322. CREATE TABLE film_text (
  323. film_id SMALLINT NOT NULL,
  324. title VARCHAR(255) NOT NULL,
  325. description CLOB,
  326. CONSTRAINT pk_film_text PRIMARY KEY (film_id)
  327. );
  328. --
  329. -- Table structure for table inventory
  330. --
  331. CREATE TABLE inventory (
  332. inventory_id INT NOT NULL,
  333. film_id INT NOT NULL,
  334. store_id INT NOT NULL,
  335. last_update DATE NOT NULL,
  336. CONSTRAINT pk_inventory PRIMARY KEY (inventory_id),
  337. CONSTRAINT fk_inventory_film FOREIGN KEY (film_id) REFERENCES film (film_id)
  338. );
  339. CREATE INDEX idx_fk_film_id ON inventory(film_id);
  340. CREATE INDEX idx_fk_film_id_store_id ON inventory(store_id,film_id);
  341. ---DROP SEQUENCE inventory_sequence;
  342. CREATE SEQUENCE inventory_sequence;
  343. CREATE OR REPLACE TRIGGER inventory_before_trigger
  344. BEFORE INSERT ON inventory FOR EACH ROW
  345. BEGIN
  346. IF (:NEW.inventory_id IS NULL) THEN
  347. SELECT inventory_sequence.nextval INTO :NEW.inventory_id
  348. FROM DUAL;
  349. END IF;
  350. :NEW.last_update:=current_date;
  351. END;
  352. /
  353. CREATE OR REPLACE TRIGGER inventory_before_update
  354. BEFORE UPDATE ON inventory FOR EACH ROW
  355. BEGIN
  356. :NEW.last_update:=current_date;
  357. END;
  358. /
  359. --
  360. -- Table structure for table staff
  361. --
  362. CREATE TABLE staff (
  363. staff_id SMALLINT NOT NULL,
  364. first_name VARCHAR(45) NOT NULL,
  365. last_name VARCHAR(45) NOT NULL,
  366. address_id INT NOT NULL,
  367. picture BLOB DEFAULT NULL,
  368. email VARCHAR(50) DEFAULT NULL,
  369. store_id INT NOT NULL,
  370. active SMALLINT DEFAULT 1 NOT NULL,
  371. username VARCHAR(16) NOT NULL,
  372. password VARCHAR(40) DEFAULT NULL,
  373. last_update DATE NOT NULL,
  374. CONSTRAINT pk_staff PRIMARY KEY (staff_id),
  375. CONSTRAINT fk_staff_address FOREIGN KEY (address_id) REFERENCES address (address_id)
  376. );
  377. CREATE INDEX idx_fk_staff_store_id ON staff(store_id);
  378. CREATE INDEX idx_fk_staff_address_id ON staff(address_id);
  379. ---DROP SEQUENCE inventory_sequence;
  380. CREATE SEQUENCE staff_sequence;
  381. CREATE OR REPLACE TRIGGER staff_before_trigger
  382. BEFORE INSERT ON staff FOR EACH ROW
  383. BEGIN
  384. IF (:NEW.staff_id IS NULL) THEN
  385. SELECT staff_sequence.nextval INTO :NEW.staff_id
  386. FROM DUAL;
  387. END IF;
  388. :NEW.last_update:=current_date;
  389. END;
  390. /
  391. CREATE OR REPLACE TRIGGER staff_before_update
  392. BEFORE UPDATE ON staff FOR EACH ROW
  393. BEGIN
  394. :NEW.last_update:=current_date;
  395. END;
  396. /
  397. --
  398. -- Table structure for table store
  399. --
  400. CREATE TABLE store (
  401. store_id INT NOT NULL,
  402. manager_staff_id SMALLINT NOT NULL,
  403. address_id INT NOT NULL,
  404. last_update DATE NOT NULL,
  405. CONSTRAINT pk_store PRIMARY KEY (store_id),
  406. CONSTRAINT fk_store_staff FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id) ,
  407. CONSTRAINT fk_store_address FOREIGN KEY (address_id) REFERENCES address (address_id)
  408. );
  409. CREATE INDEX idx_store_fk_manager_staff_id ON store(manager_staff_id);
  410. CREATE INDEX idx_fk_store_address ON store(address_id);
  411. ---DROP SEQUENCE store_sequence;
  412. CREATE SEQUENCE store_sequence;
  413. CREATE OR REPLACE TRIGGER store_before_trigger
  414. BEFORE INSERT ON store FOR EACH ROW
  415. BEGIN
  416. IF (:NEW.store_id IS NULL) THEN
  417. SELECT store_sequence.nextval INTO :NEW.store_id
  418. FROM DUAL;
  419. END IF;
  420. :NEW.last_update:=current_date;
  421. END;
  422. /
  423. CREATE OR REPLACE TRIGGER store_before_update
  424. BEFORE UPDATE ON store FOR EACH ROW
  425. BEGIN
  426. :NEW.last_update:=current_date;
  427. END;
  428. /
  429. --
  430. -- Table structure for table payment
  431. --
  432. CREATE TABLE payment (
  433. payment_id int NOT NULL,
  434. customer_id INT NOT NULL,
  435. staff_id SMALLINT NOT NULL,
  436. rental_id INT DEFAULT NULL,
  437. amount DECIMAL(5,2) NOT NULL,
  438. payment_date DATE NOT NULL,
  439. last_update DATE NOT NULL,
  440. CONSTRAINT pk_payment PRIMARY KEY (payment_id),
  441. CONSTRAINT fk_payment_customer FOREIGN KEY (customer_id) REFERENCES customer (customer_id) ,
  442. CONSTRAINT fk_payment_staff FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
  443. );
  444. CREATE INDEX idx_fk_staff_id ON payment(staff_id);
  445. CREATE INDEX idx_fk_customer_id ON payment(customer_id);
  446. ---DROP SEQUENCE payment_sequence;
  447. CREATE SEQUENCE payment_sequence;
  448. CREATE OR REPLACE TRIGGER payment_before_trigger
  449. BEFORE INSERT ON payment FOR EACH ROW
  450. BEGIN
  451. IF (:NEW.payment_id IS NULL) THEN
  452. SELECT payment_sequence.nextval INTO :NEW.payment_id
  453. FROM DUAL;
  454. END IF;
  455. :NEW.last_update:=current_date;
  456. END;
  457. /
  458. CREATE OR REPLACE TRIGGER payment_before_update
  459. BEFORE UPDATE ON payment FOR EACH ROW
  460. BEGIN
  461. :NEW.last_update:=current_date;
  462. END;
  463. /
  464. CREATE TABLE rental (
  465. rental_id INT NOT NULL,
  466. rental_date DATE NOT NULL,
  467. inventory_id INT NOT NULL,
  468. customer_id INT NOT NULL,
  469. return_date DATE DEFAULT NULL,
  470. staff_id SMALLINT NOT NULL,
  471. last_update DATE NOT NULL,
  472. CONSTRAINT pk_rental PRIMARY KEY (rental_id),
  473. CONSTRAINT fk_rental_staff FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ,
  474. CONSTRAINT fk_rental_inventory FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id) ,
  475. CONSTRAINT fk_rental_customer FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
  476. );
  477. CREATE INDEX idx_rental_fk_inventory_id ON rental(inventory_id);
  478. CREATE INDEX idx_rental_fk_customer_id ON rental(customer_id);
  479. CREATE INDEX idx_rental_fk_staff_id ON rental(staff_id);
  480. CREATE UNIQUE INDEX idx_rental_uq ON rental (rental_date,inventory_id,customer_id);
  481. ---DROP SEQUENCE payment_sequence;
  482. CREATE SEQUENCE rental_sequence;
  483. CREATE OR REPLACE TRIGGER rental_before_trigger
  484. BEFORE INSERT ON rental FOR EACH ROW
  485. BEGIN
  486. IF (:NEW.rental_id IS NULL) THEN
  487. SELECT rental_sequence.nextval INTO :NEW.rental_id
  488. FROM DUAL;
  489. END IF;
  490. :NEW.last_update:=current_date;
  491. END;
  492. /
  493. CREATE OR REPLACE TRIGGER rental_before_update
  494. BEFORE UPDATE ON rental FOR EACH ROW
  495. BEGIN
  496. :NEW.last_update:=current_date;
  497. END;
  498. /
  499. -- FK CONSTRAINTS
  500. ALTER TABLE customer ADD CONSTRAINT fk_customer_store FOREIGN KEY (store_id) REFERENCES store (store_id);
  501. ALTER TABLE inventory ADD CONSTRAINT fk_inventory_store FOREIGN KEY (store_id) REFERENCES store (store_id);
  502. ALTER TABLE staff ADD CONSTRAINT fk_staff_store FOREIGN KEY (store_id) REFERENCES store (store_id);
  503. ALTER TABLE payment ADD CONSTRAINT fk_payment_rental FOREIGN KEY (rental_id) REFERENCES rental (rental_id) ON DELETE SET NULL;
  504. --
  505. -- View structure for view customer_list
  506. --
  507. CREATE OR REPLACE VIEW customer_list
  508. AS
  509. SELECT cu.customer_id AS ID,
  510. cu.first_name||' '||cu.last_name AS name,
  511. a.address AS address,
  512. a.postal_code AS zip_code,
  513. a.phone AS phone,
  514. city.city AS city,
  515. country.country AS country,
  516. decode(cu.active, 1,'active','') AS notes,
  517. cu.store_id AS SID
  518. FROM customer cu JOIN address a ON cu.address_id = a.address_id JOIN city ON a.city_id = city.city_id
  519. JOIN country ON city.country_id = country.country_id;
  520. --
  521. -- View structure for view film_list
  522. --
  523. CREATE OR REPLACE VIEW film_list
  524. AS
  525. SELECT film.film_id AS FID,
  526. film.title AS title,
  527. film.description AS description,
  528. category.name AS category,
  529. film.rental_rate AS price,
  530. film.length AS length,
  531. film.rating AS rating,
  532. actor.first_name||' '||actor.last_name AS actors
  533. 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
  534. JOIN film_actor ON film.film_id = film_actor.film_id
  535. JOIN actor ON film_actor.actor_id = actor.actor_id;
  536. --
  537. -- View structure for view staff_list
  538. --
  539. CREATE OR REPLACE VIEW staff_list
  540. AS
  541. SELECT s.staff_id AS ID,
  542. s.first_name||' '||s.last_name AS name,
  543. a.address AS address,
  544. a.postal_code AS zip_code,
  545. a.phone AS phone,
  546. city.city AS city,
  547. country.country AS country,
  548. s.store_id AS SID
  549. FROM staff s JOIN address a ON s.address_id = a.address_id JOIN city ON a.city_id = city.city_id
  550. JOIN country ON city.country_id = country.country_id;
  551. --
  552. -- View structure for view sales_by_store
  553. --
  554. CREATE OR REPLACE VIEW sales_by_store
  555. AS
  556. SELECT
  557. s.store_id
  558. ,c.city||','||cy.country AS store
  559. ,m.first_name||' '||m.last_name AS manager
  560. ,SUM(p.amount) AS total_sales
  561. FROM payment p
  562. INNER JOIN rental r ON p.rental_id = r.rental_id
  563. INNER JOIN inventory i ON r.inventory_id = i.inventory_id
  564. INNER JOIN store s ON i.store_id = s.store_id
  565. INNER JOIN address a ON s.address_id = a.address_id
  566. INNER JOIN city c ON a.city_id = c.city_id
  567. INNER JOIN country cy ON c.country_id = cy.country_id
  568. INNER JOIN staff m ON s.manager_staff_id = m.staff_id
  569. GROUP BY
  570. s.store_id
  571. , c.city||','||cy.country
  572. , m.first_name||' '||m.last_name;
  573. --
  574. -- View structure for view sales_by_film_category
  575. --
  576. -- Note that total sales will add up to >100% because
  577. -- some titles belong to more than 1 category
  578. --
  579. CREATE OR REPLACE VIEW sales_by_film_category
  580. AS
  581. SELECT
  582. c.name AS category
  583. , SUM(p.amount) AS total_sales
  584. FROM payment p
  585. INNER JOIN rental r ON p.rental_id = r.rental_id
  586. INNER JOIN inventory i ON r.inventory_id = i.inventory_id
  587. INNER JOIN film f ON i.film_id = f.film_id
  588. INNER JOIN film_category fc ON f.film_id = fc.film_id
  589. INNER JOIN category c ON fc.category_id = c.category_id
  590. GROUP BY c.name;
  591. --
  592. -- View structure for view actor_info
  593. --
  594. /*
  595. CREATE VIEW actor_info
  596. AS
  597. SELECT
  598. a.actor_id,
  599. a.first_name,
  600. a.last_name,
  601. GROUP_CONCAT(DISTINCT CONCAT(c.name, ': ',
  602. (SELECT GROUP_CONCAT(f.title ORDER BY f.title SEPARATOR ', ')
  603. FROM sakila.film f
  604. INNER JOIN sakila.film_category fc
  605. ON f.film_id = fc.film_id
  606. INNER JOIN sakila.film_actor fa
  607. ON f.film_id = fa.film_id
  608. WHERE fc.category_id = c.category_id
  609. AND fa.actor_id = a.actor_id
  610. )
  611. )
  612. ORDER BY c.name SEPARATOR '; ')
  613. AS film_info
  614. FROM sakila.actor a
  615. LEFT JOIN sakila.film_actor fa
  616. ON a.actor_id = fa.actor_id
  617. LEFT JOIN sakila.film_category fc
  618. ON fa.film_id = fc.film_id
  619. LEFT JOIN sakila.category c
  620. ON fc.category_id = c.category_id
  621. GROUP BY a.actor_id, a.first_name, a.last_name;
  622. */
  623. -- TO DO PROCEDURES