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

http://github.com/jOOQ/jOOQ · SQL · 505 lines · 344 code · 49 blank · 112 comment · 1 complexity · bf8372f8f568635030cf8e4ad6d201e1 MD5 · raw file

  1. /*
  2. Sakila for Microsoft SQL Server 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. CREATE DATABASE sakila;
  10. GO
  11. USE sakila;
  12. --
  13. -- Table structure for table actor
  14. --
  15. CREATE TABLE actor (
  16. actor_id int NOT NULL IDENTITY ,
  17. first_name VARCHAR(45) NOT NULL,
  18. last_name VARCHAR(45) NOT NULL,
  19. last_update DATETIME NOT NULL,
  20. PRIMARY KEY NONCLUSTERED (actor_id)
  21. )
  22. GO
  23. ALTER TABLE actor ADD CONSTRAINT [DF_actor_last_update] DEFAULT (getdate()) FOR last_update
  24. GO
  25. CREATE INDEX idx_actor_last_name ON actor(last_name)
  26. GO
  27. --
  28. -- Table structure for table country
  29. --
  30. CREATE TABLE country (
  31. country_id SMALLINT NOT NULL IDENTITY ,
  32. country VARCHAR(50) NOT NULL,
  33. last_update DATETIME,
  34. PRIMARY KEY NONCLUSTERED (country_id)
  35. )
  36. GO
  37. ALTER TABLE country ADD CONSTRAINT [DF_country_last_update] DEFAULT (getdate()) FOR last_update
  38. GO
  39. --
  40. -- Table structure for table city
  41. --
  42. CREATE TABLE city (
  43. city_id int NOT NULL IDENTITY ,
  44. city VARCHAR(50) NOT NULL,
  45. country_id SMALLINT NOT NULL,
  46. last_update DATETIME NOT NULL,
  47. PRIMARY KEY NONCLUSTERED (city_id),
  48. CONSTRAINT fk_city_country FOREIGN KEY (country_id) REFERENCES country (country_id) ON DELETE NO ACTION ON UPDATE CASCADE
  49. )
  50. GO
  51. ALTER TABLE city ADD CONSTRAINT [DF_city_last_update] DEFAULT (getdate()) FOR last_update
  52. GO
  53. CREATE INDEX idx_fk_country_id ON city(country_id)
  54. GO
  55. --
  56. -- Table structure for table address
  57. --
  58. CREATE TABLE address (
  59. address_id int NOT NULL IDENTITY ,
  60. address VARCHAR(50) NOT NULL,
  61. address2 VARCHAR(50) DEFAULT NULL,
  62. district VARCHAR(20) NOT NULL,
  63. city_id INT NOT NULL,
  64. postal_code VARCHAR(10) DEFAULT NULL,
  65. phone VARCHAR(20) NOT NULL,
  66. last_update DATETIME NOT NULL,
  67. PRIMARY KEY NONCLUSTERED (address_id)
  68. )
  69. GO
  70. ALTER TABLE address ADD CONSTRAINT [DF_address_last_update] DEFAULT (getdate()) FOR last_update
  71. GO
  72. CREATE INDEX idx_fk_city_id ON address(city_id)
  73. GO
  74. ALTER TABLE address ADD CONSTRAINT fk_address_city FOREIGN KEY (city_id) REFERENCES city (city_id) ON DELETE NO ACTION ON UPDATE CASCADE
  75. GO
  76. --
  77. -- Table structure for table language
  78. --
  79. CREATE TABLE language (
  80. language_id TINYINT NOT NULL IDENTITY,
  81. name CHAR(20) NOT NULL,
  82. last_update DATETIME NOT NULL,
  83. PRIMARY KEY NONCLUSTERED (language_id)
  84. )
  85. GO
  86. ALTER TABLE language ADD CONSTRAINT [DF_language_last_update] DEFAULT (getdate()) FOR last_update
  87. GO
  88. --
  89. -- Table structure for table category
  90. --
  91. CREATE TABLE category (
  92. category_id TINYINT NOT NULL IDENTITY,
  93. name VARCHAR(25) NOT NULL,
  94. last_update DATETIME NOT NULL,
  95. PRIMARY KEY NONCLUSTERED (category_id)
  96. )
  97. GO
  98. ALTER TABLE category ADD CONSTRAINT [DF_category_last_update] DEFAULT (getdate()) FOR last_update
  99. GO
  100. --
  101. -- Table structure for table customer
  102. --
  103. CREATE TABLE customer (
  104. customer_id INT NOT NULL IDENTITY ,
  105. store_id INT NOT NULL,
  106. first_name VARCHAR(45) NOT NULL,
  107. last_name VARCHAR(45) NOT NULL,
  108. email VARCHAR(50) DEFAULT NULL,
  109. address_id INT NOT NULL,
  110. active CHAR(1) NOT NULL DEFAULT 'Y',
  111. create_date DATETIME NOT NULL,
  112. last_update DATETIME NOT NULL,
  113. PRIMARY KEY NONCLUSTERED (customer_id),
  114. CONSTRAINT fk_customer_address FOREIGN KEY (address_id) REFERENCES address (address_id) ON DELETE NO ACTION ON UPDATE CASCADE
  115. )
  116. GO
  117. ALTER TABLE customer ADD CONSTRAINT [DF_customer_last_update] DEFAULT (getdate()) FOR last_update
  118. GO
  119. ALTER TABLE customer ADD CONSTRAINT [DF_customer_create_date] DEFAULT (getdate()) FOR create_date
  120. GO
  121. CREATE INDEX idx_fk_store_id ON customer(store_id)
  122. GO
  123. CREATE INDEX idx_fk_address_id ON customer(address_id)
  124. GO
  125. CREATE INDEX idx_last_name ON customer(last_name)
  126. GO
  127. --
  128. -- Table structure for table film
  129. --
  130. CREATE TABLE film (
  131. film_id int NOT NULL IDENTITY ,
  132. title VARCHAR(255) NOT NULL,
  133. description TEXT DEFAULT NULL,
  134. release_year VARCHAR(4) NULL,
  135. language_id TINYINT NOT NULL,
  136. original_language_id TINYINT DEFAULT NULL,
  137. rental_duration TINYINT NOT NULL DEFAULT 3,
  138. rental_rate DECIMAL(4,2) NOT NULL DEFAULT 4.99,
  139. length SMALLINT DEFAULT NULL,
  140. replacement_cost DECIMAL(5,2) NOT NULL DEFAULT 19.99,
  141. rating VARCHAR(10) DEFAULT 'G',
  142. special_features VARCHAR(255) DEFAULT NULL,
  143. last_update DATETIME NOT NULL,
  144. PRIMARY KEY NONCLUSTERED (film_id),
  145. CONSTRAINT fk_film_language FOREIGN KEY (language_id) REFERENCES language (language_id) ,
  146. CONSTRAINT fk_film_language_original FOREIGN KEY (original_language_id) REFERENCES language (language_id)
  147. )
  148. GO
  149. ALTER TABLE film ADD CONSTRAINT CHECK_special_features CHECK(special_features is null or
  150. special_features like '%Trailers%' or
  151. special_features like '%Commentaries%' or
  152. special_features like '%Deleted Scenes%' or
  153. special_features like '%Behind the Scenes%')
  154. GO
  155. ALTER TABLE film ADD CONSTRAINT CHECK_special_rating CHECK(rating in ('G','PG','PG-13','R','NC-17'))
  156. GO
  157. ALTER TABLE film ADD CONSTRAINT [DF_film_last_update] DEFAULT (getdate()) FOR last_update
  158. GO
  159. CREATE INDEX idx_fk_language_id ON film(language_id)
  160. GO
  161. CREATE INDEX idx_fk_original_language_id ON film(original_language_id)
  162. GO
  163. --
  164. -- Table structure for table film_actor
  165. --
  166. CREATE TABLE film_actor (
  167. actor_id INT NOT NULL,
  168. film_id INT NOT NULL,
  169. last_update DATETIME NOT NULL,
  170. PRIMARY KEY NONCLUSTERED (actor_id,film_id),
  171. CONSTRAINT fk_film_actor_actor FOREIGN KEY (actor_id) REFERENCES actor (actor_id) ON DELETE NO ACTION ON UPDATE CASCADE,
  172. CONSTRAINT fk_film_actor_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON DELETE NO ACTION ON UPDATE CASCADE
  173. )
  174. GO
  175. ALTER TABLE film_actor ADD CONSTRAINT [DF_film_actor_last_update] DEFAULT (getdate()) FOR last_update
  176. GO
  177. CREATE INDEX idx_fk_film_actor_film ON film_actor(film_id)
  178. GO
  179. CREATE INDEX idx_fk_film_actor_actor ON film_actor(actor_id)
  180. GO
  181. --
  182. -- Table structure for table film_category
  183. --
  184. CREATE TABLE film_category (
  185. film_id INT NOT NULL,
  186. category_id TINYINT NOT NULL,
  187. last_update DATETIME NOT NULL,
  188. PRIMARY KEY NONCLUSTERED (film_id, category_id),
  189. CONSTRAINT fk_film_category_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON DELETE NO ACTION ON UPDATE CASCADE,
  190. CONSTRAINT fk_film_category_category FOREIGN KEY (category_id) REFERENCES category (category_id) ON DELETE NO ACTION ON UPDATE CASCADE
  191. )
  192. GO
  193. ALTER TABLE film_category ADD CONSTRAINT [DF_film_category_last_update] DEFAULT (getdate()) FOR last_update
  194. GO
  195. CREATE INDEX idx_fk_film_category_film ON film_category(film_id)
  196. GO
  197. CREATE INDEX idx_fk_film_category_category ON film_category(category_id)
  198. GO
  199. --
  200. -- Table structure for table film_text
  201. --
  202. CREATE TABLE film_text (
  203. film_id SMALLINT NOT NULL,
  204. title VARCHAR(255) NOT NULL,
  205. description TEXT,
  206. PRIMARY KEY NONCLUSTERED (film_id),
  207. )
  208. --
  209. -- Table structure for table inventory
  210. --
  211. CREATE TABLE inventory (
  212. inventory_id INT NOT NULL IDENTITY,
  213. film_id INT NOT NULL,
  214. store_id INT NOT NULL,
  215. last_update DATETIME NOT NULL,
  216. PRIMARY KEY NONCLUSTERED (inventory_id),
  217. CONSTRAINT fk_inventory_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON DELETE NO ACTION ON UPDATE CASCADE
  218. )
  219. GO
  220. ALTER TABLE inventory ADD CONSTRAINT [DF_inventory_last_update] DEFAULT (getdate()) FOR last_update
  221. GO
  222. CREATE INDEX idx_fk_film_id ON inventory(film_id)
  223. GO
  224. CREATE INDEX idx_fk_film_id_store_id ON inventory(store_id,film_id)
  225. GO
  226. --
  227. -- Table structure for table staff
  228. --
  229. CREATE TABLE staff (
  230. staff_id TINYINT NOT NULL IDENTITY,
  231. first_name VARCHAR(45) NOT NULL,
  232. last_name VARCHAR(45) NOT NULL,
  233. address_id INT NOT NULL,
  234. picture IMAGE DEFAULT NULL,
  235. email VARCHAR(50) DEFAULT NULL,
  236. store_id INT NOT NULL,
  237. active BIT NOT NULL DEFAULT 1,
  238. username VARCHAR(16) NOT NULL,
  239. password VARCHAR(40) DEFAULT NULL,
  240. last_update DATETIME NOT NULL,
  241. PRIMARY KEY NONCLUSTERED (staff_id),
  242. CONSTRAINT fk_staff_address FOREIGN KEY (address_id) REFERENCES address (address_id) ON DELETE NO ACTION ON UPDATE CASCADE
  243. )
  244. GO
  245. ALTER TABLE staff ADD CONSTRAINT [DF_staff_last_update] DEFAULT (getdate()) FOR last_update
  246. GO
  247. CREATE INDEX idx_fk_store_id ON staff(store_id)
  248. GO
  249. CREATE INDEX idx_fk_address_id ON staff(address_id)
  250. GO
  251. --
  252. -- Table structure for table store
  253. --
  254. CREATE TABLE store (
  255. store_id INT NOT NULL IDENTITY,
  256. manager_staff_id TINYINT NOT NULL,
  257. address_id INT NOT NULL,
  258. last_update DATETIME NOT NULL,
  259. PRIMARY KEY NONCLUSTERED (store_id),
  260. CONSTRAINT fk_store_staff FOREIGN KEY (manager_staff_id) REFERENCES staff (staff_id) ,
  261. CONSTRAINT fk_store_address FOREIGN KEY (address_id) REFERENCES address (address_id)
  262. )
  263. GO
  264. ALTER TABLE store ADD CONSTRAINT [DF_store_last_update] DEFAULT (getdate()) FOR last_update
  265. GO
  266. CREATE UNIQUE NONCLUSTERED INDEX idx_fk_address_id ON store(manager_staff_id)
  267. GO
  268. CREATE INDEX idx_fk_store_address ON store(address_id)
  269. GO
  270. --
  271. -- Table structure for table payment
  272. --
  273. CREATE TABLE payment (
  274. payment_id int NOT NULL IDENTITY ,
  275. customer_id INT NOT NULL,
  276. staff_id TINYINT NOT NULL,
  277. rental_id INT DEFAULT NULL,
  278. amount DECIMAL(5,2) NOT NULL,
  279. payment_date DATETIME NOT NULL,
  280. last_update DATETIME NOT NULL,
  281. PRIMARY KEY NONCLUSTERED (payment_id),
  282. CONSTRAINT fk_payment_customer FOREIGN KEY (customer_id) REFERENCES customer (customer_id) ,
  283. CONSTRAINT fk_payment_staff FOREIGN KEY (staff_id) REFERENCES staff (staff_id)
  284. )
  285. GO
  286. ALTER TABLE payment ADD CONSTRAINT [DF_payment_last_update] DEFAULT (getdate()) FOR last_update
  287. GO
  288. CREATE INDEX idx_fk_staff_id ON payment(staff_id)
  289. GO
  290. CREATE INDEX idx_fk_customer_id ON payment(customer_id)
  291. GO
  292. --
  293. -- Table structure for table rental
  294. --
  295. CREATE TABLE rental (
  296. rental_id INT NOT NULL IDENTITY,
  297. rental_date DATETIME NOT NULL,
  298. inventory_id INT NOT NULL,
  299. customer_id INT NOT NULL,
  300. return_date DATETIME DEFAULT NULL,
  301. staff_id TINYINT NOT NULL,
  302. last_update DATETIME NOT NULL,
  303. PRIMARY KEY NONCLUSTERED (rental_id),
  304. CONSTRAINT fk_rental_staff FOREIGN KEY (staff_id) REFERENCES staff (staff_id) ,
  305. CONSTRAINT fk_rental_inventory FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id) ,
  306. CONSTRAINT fk_rental_customer FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
  307. )
  308. GO
  309. ALTER TABLE rental ADD CONSTRAINT [DF_rental_last_update] DEFAULT (getdate()) FOR last_update
  310. GO
  311. CREATE INDEX idx_fk_inventory_id ON rental(inventory_id)
  312. GO
  313. CREATE INDEX idx_fk_customer_id ON rental(customer_id)
  314. GO
  315. CREATE INDEX idx_fk_staff_id ON rental(staff_id)
  316. GO
  317. CREATE UNIQUE INDEX idx_uq ON rental (rental_date,inventory_id,customer_id)
  318. GO
  319. -- FK CONSTRAINTS
  320. ALTER TABLE customer ADD CONSTRAINT fk_customer_store FOREIGN KEY (store_id) REFERENCES store (store_id) ON DELETE NO ACTION ON UPDATE CASCADE
  321. GO
  322. ALTER TABLE inventory ADD CONSTRAINT fk_inventory_store FOREIGN KEY (store_id) REFERENCES store (store_id) ON DELETE NO ACTION ON UPDATE CASCADE;
  323. GO
  324. ALTER TABLE staff ADD CONSTRAINT fk_staff_store FOREIGN KEY (store_id) REFERENCES store (store_id) ON DELETE NO ACTION ON UPDATE CASCADE;
  325. GO
  326. ALTER TABLE payment ADD CONSTRAINT fk_payment_rental FOREIGN KEY (rental_id) REFERENCES rental (rental_id) ON DELETE SET NULL ON UPDATE CASCADE;
  327. GO
  328. --
  329. -- View structure for view customer_list
  330. --
  331. CREATE VIEW customer_list
  332. AS
  333. SELECT cu.customer_id AS ID,
  334. cu.first_name + ' ' + cu.last_name AS name,
  335. a.address AS address,
  336. a.postal_code AS zip_code,
  337. a.phone AS phone,
  338. city.city AS city,
  339. country.country AS country,
  340. case when cu.active=1 then 'active' else '' end AS notes,
  341. cu.store_id AS SID
  342. FROM customer AS cu JOIN address AS a ON cu.address_id = a.address_id JOIN city ON a.city_id = city.city_id
  343. JOIN country ON city.country_id = country.country_id
  344. GO
  345. --
  346. -- View structure for view film_list
  347. --
  348. CREATE VIEW film_list
  349. AS
  350. SELECT film.film_id AS FID,
  351. film.title AS title,
  352. film.description AS description,
  353. category.name AS category,
  354. film.rental_rate AS price,
  355. film.length AS length,
  356. film.rating AS rating,
  357. actor.first_name+' '+actor.last_name AS actors
  358. 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
  359. JOIN film_actor ON film.film_id = film_actor.film_id
  360. JOIN actor ON film_actor.actor_id = actor.actor_id
  361. GO
  362. --
  363. -- View structure for view staff_list
  364. --
  365. CREATE VIEW staff_list
  366. AS
  367. SELECT s.staff_id AS ID,
  368. s.first_name+' '+s.last_name AS name,
  369. a.address AS address,
  370. a.postal_code AS zip_code,
  371. a.phone AS phone,
  372. city.city AS city,
  373. country.country AS country,
  374. s.store_id AS SID
  375. FROM staff AS s JOIN address AS a ON s.address_id = a.address_id JOIN city ON a.city_id = city.city_id
  376. JOIN country ON city.country_id = country.country_id
  377. GO
  378. --
  379. -- View structure for view sales_by_store
  380. --
  381. CREATE VIEW sales_by_store
  382. AS
  383. SELECT
  384. s.store_id
  385. ,c.city+','+cy.country AS store
  386. ,m.first_name+' '+ m.last_name AS manager
  387. ,SUM(p.amount) AS total_sales
  388. FROM payment AS p
  389. INNER JOIN rental AS r ON p.rental_id = r.rental_id
  390. INNER JOIN inventory AS i ON r.inventory_id = i.inventory_id
  391. INNER JOIN store AS s ON i.store_id = s.store_id
  392. INNER JOIN address AS a ON s.address_id = a.address_id
  393. INNER JOIN city AS c ON a.city_id = c.city_id
  394. INNER JOIN country AS cy ON c.country_id = cy.country_id
  395. INNER JOIN staff AS m ON s.manager_staff_id = m.staff_id
  396. GROUP BY
  397. s.store_id
  398. , c.city+ ','+cy.country
  399. , m.first_name+' '+ m.last_name
  400. GO
  401. --
  402. -- View structure for view sales_by_film_category
  403. --
  404. -- Note that total sales will add up to >100% because
  405. -- some titles belong to more than 1 category
  406. --
  407. CREATE VIEW sales_by_film_category
  408. AS
  409. SELECT
  410. c.name AS category
  411. , SUM(p.amount) AS total_sales
  412. FROM payment AS p
  413. INNER JOIN rental AS r ON p.rental_id = r.rental_id
  414. INNER JOIN inventory AS i ON r.inventory_id = i.inventory_id
  415. INNER JOIN film AS f ON i.film_id = f.film_id
  416. INNER JOIN film_category AS fc ON f.film_id = fc.film_id
  417. INNER JOIN category AS c ON fc.category_id = c.category_id
  418. GROUP BY c.name
  419. GO
  420. --
  421. -- View structure for view actor_info
  422. --
  423. /*
  424. CREATE VIEW actor_info
  425. AS
  426. SELECT
  427. a.actor_id,
  428. a.first_name,
  429. a.last_name,
  430. GROUP_CONCAT(DISTINCT CONCAT(c.name, ': ',
  431. (SELECT GROUP_CONCAT(f.title ORDER BY f.title SEPARATOR ', ')
  432. FROM sakila.film f
  433. INNER JOIN sakila.film_category fc
  434. ON f.film_id = fc.film_id
  435. INNER JOIN sakila.film_actor fa
  436. ON f.film_id = fa.film_id
  437. WHERE fc.category_id = c.category_id
  438. AND fa.actor_id = a.actor_id
  439. )
  440. )
  441. ORDER BY c.name SEPARATOR '; ')
  442. AS film_info
  443. FROM sakila.actor a
  444. LEFT JOIN sakila.film_actor fa
  445. ON a.actor_id = fa.actor_id
  446. LEFT JOIN sakila.film_category fc
  447. ON fa.film_id = fc.film_id
  448. LEFT JOIN sakila.category c
  449. ON fc.category_id = c.category_id
  450. GROUP BY a.actor_id, a.first_name, a.last_name;
  451. */
  452. -- TO DO PROCEDURES
  453. -- TO DO TRIGGERS