Advertisement
heavenriver

ex03.sql

Oct 23rd, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.84 KB | None | 0 0
  1. -- initialisation: creates tables
  2. use omnibox;
  3. drop table if exists gallery;
  4. drop table if exists museum;
  5. create table if not exists gallery
  6.     (artwork integer(3),
  7.      dated integer(4),
  8.      atmuseum varchar(25),
  9.     primary key (artwork, dated));
  10. create table if not exists museum
  11.     (code varchar(25) primary key,
  12.      nation varchar(20));
  13.  
  14. -- populates tables
  15. insert into gallery values
  16.     (1, 1990, 'Louvre'),(1, 1991, 'Louvre'),(1, 1992, 'Louvre'),(1, 1993, 'Uffizi'),(1, 1994, 'Uffizi'),(1, 1995, 'Louvre'),
  17.     (2, 1990, 'Uffizi'),(2, 1991, 'Vatican'),(2, 1992, 'Guggenheim'),(2, 1993, 'Guggenheim'),(2, 1994, 'Guggenheim'),(2, 1995, 'Guggenheim'),
  18.     (3, 1990, 'Vatican'),(3, 1991, 'Guggenheim'),(3, 1992, 'Vatican'),(3, 1993, 'Orsay'),(3, 1994, 'Orsay'),(3, 1995, 'Orsay'),
  19.     (4, 1990, 'Guggenheim'),(4, 1991, 'Guggenheim'),(4, 1992, 'Orsay'),(4, 1993, 'Orsay'),(4, 1994, 'Louvre'),(4, 1995, 'Louvre'),
  20. -- Louvre exclusives
  21.     (5, 1990, 'Louvre'),(5, 1991, 'Louvre'),(5, 1992, 'Louvre'),(5, 1993, 'Louvre'),(5, 1994, 'Louvre'),(5, 1995, 'Louvre'),
  22.     (6, 1990, 'Louvre'),(6, 1991, 'Louvre'),(6, 1992, 'Louvre'),(6, 1993, 'Louvre'),(6, 1994, 'Louvre'),(6, 1995, 'Louvre'),
  23.     (7, 1990, 'Louvre'),(7, 1991, 'Louvre'),(7, 1992, 'Louvre'),(7, 1993, 'Louvre'),(7, 1994, 'Louvre'),(7, 1995, 'Louvre'),
  24.     (8, 1990, 'Louvre'),(8, 1991, 'Louvre'),(8, 1992, 'Louvre'),(8, 1993, 'Louvre'),(8, 1994, 'Louvre'),(8, 1995, 'Louvre'),
  25.     (9, 1990, 'Louvre'),(9, 1991, 'Louvre'),(9, 1992, 'Louvre'),(9, 1993, 'Louvre'),(9, 1994, 'Louvre'),(9, 1995, 'Louvre'),
  26.     (10, 1990, 'Louvre'),(10, 1991, 'Louvre'),(10, 1992, 'Louvre'),(10, 1993, 'Louvre'),(10, 1994, 'Louvre'),(10, 1995, 'Louvre'),
  27.     (11, 1990, 'Louvre'),(11, 1991, 'Louvre'),(11, 1992, 'Louvre'),(11, 1993, 'Louvre'),(11, 1994, 'Louvre'),(11, 1995, 'Louvre'),
  28.     (12, 1990, 'Louvre'),(12, 1991, 'Louvre'),(12, 1992, 'Louvre'),(12, 1993, 'Louvre'),(12, 1994, 'Louvre'),(12, 1995, 'Louvre'),
  29.     (13, 1990, 'Louvre'),(13, 1991, 'Louvre'),(13, 1992, 'Louvre'),(13, 1993, 'Louvre'),(13, 1994, 'Louvre'),(13, 1995, 'Louvre'),
  30. -- Orsay exclusives
  31.     (14, 1990, 'Orsay'),(14, 1991, 'Orsay'),(14, 1992, 'Orsay'),(14, 1993, 'Orsay'),(14, 1994, 'Orsay'),(14, 1995, 'Orsay'),
  32.     (15, 1990, 'Orsay'),(15, 1991, 'Orsay'),(15, 1992, 'Orsay'),(15, 1993, 'Orsay'),(15, 1994, 'Orsay'),(15, 1995, 'Orsay'),
  33.     (16, 1990, 'Orsay'),(16, 1991, 'Orsay'),(16, 1992, 'Orsay'),(16, 1993, 'Orsay'),(16, 1994, 'Orsay'),(16, 1995, 'Orsay'),
  34.     (17, 1990, 'Orsay'),(17, 1991, 'Orsay'),(17, 1992, 'Orsay'),(17, 1993, 'Orsay'),(17, 1994, 'Orsay'),(17, 1995, 'Orsay'),
  35.     (18, 1990, 'Orsay'),(18, 1991, 'Orsay'),(18, 1992, 'Orsay'),(18, 1993, 'Orsay'),(18, 1994, 'Orsay'),(18, 1995, 'Orsay'),
  36.     (19, 1990, 'Orsay'),(19, 1991, 'Orsay'),(19, 1992, 'Orsay'),(19, 1993, 'Orsay'),(19, 1994, 'Orsay'),(19, 1995, 'Orsay'),
  37.     (20, 1990, 'Orsay'),(20, 1991, 'Orsay'),(20, 1992, 'Orsay'),(20, 1993, 'Orsay'),(20, 1994, 'Orsay'),(20, 1995, 'Orsay'),
  38.     (21, 1990, 'Orsay'),(21, 1991, 'Orsay'),(21, 1992, 'Orsay'),(21, 1993, 'Orsay'),(21, 1994, 'Orsay'),(21, 1995, 'Orsay'),
  39.     (22, 1990, 'Orsay'),(22, 1991, 'Orsay'),(22, 1992, 'Orsay'),(22, 1993, 'Orsay'),(22, 1994, 'Orsay'),(22, 1995, 'Orsay'),
  40.     (23, 1990, 'Orsay'),(23, 1991, 'Orsay'),(23, 1992, 'Orsay'),(23, 1993, 'Orsay'),(23, 1994, 'Orsay'),(23, 1995, 'Orsay'),
  41. -- Uffizi exclusives
  42.     (24, 1990, 'Uffizi'),(24, 1991, 'Uffizi'),(24, 1992, 'Uffizi'),(24, 1993, 'Uffizi'),(24, 1994, 'Uffizi'),(24, 1995, 'Uffizi'),
  43.     (25, 1990, 'Uffizi'),(25, 1991, 'Uffizi'),(25, 1992, 'Uffizi'),(25, 1993, 'Uffizi'),(25, 1994, 'Uffizi'),(25, 1995, 'Uffizi');
  44. -- museums
  45. insert into museum values
  46.     ('Prado', 'Spain'),
  47.     ('Louvre', 'France'),
  48.     ('Orsay', 'France'),
  49.     ('Uffizi', 'Italy'),
  50.     ('Vatican', 'Italy'),
  51.     ('Guggenheim', 'United States');
  52.  
  53. -- EXERCISE 3.1
  54. -- shows the artworks stored at least two years in a row in Italian museums
  55. create view template
  56.     as select g.artwork, g.dated, g.atmuseum
  57.             from gallery g, museum m
  58.             where g.atmuseum = m.code
  59.                 and m.nation = 'Italy'
  60.             order by artwork;
  61. select distinct t1.artwork
  62.     from template t1, template t2
  63.     where t1.artwork = t2.artwork
  64.           and t1.dated + 1 = t2.dated;
  65. drop view if exists template;
  66.  
  67. -- EXERCISE 3.2
  68. -- shows the number of artworks stored in any French museum in any given year, but only if it's greater than 10
  69. select g.atmuseum, g.dated, count(distinct g.artwork) as artworks
  70.         from gallery g, museum m
  71.         where g.atmuseum = m.code
  72.               and m.nation = 'France'
  73.         group by g.atmuseum, g.dated
  74.         having count(distinct g.artwork) > 10;
  75.  
  76. -- EXERCISE 3.3
  77. -- shows the artworks that have never been stored in Italian museums
  78. select distinct artwork
  79.     from gallery
  80.     where artwork <> all
  81.         (select distinct g.artwork
  82.          from gallery g, museum m
  83.          where g.atmuseum = m.code
  84.                and m.nation = 'Italy');
  85.  
  86. -- removes data
  87. drop view if exists template;
  88. drop table if exists gallery;
  89. drop table if exists museum;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement