Advertisement
heavenriver

ex01.sql

Oct 22nd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.48 KB | None | 0 0
  1. -- initialisation: creates tables
  2. use omnibox;
  3. drop table if exists dirige;
  4. drop table if exists hobby;
  5. create table if not exists dirige
  6.     (azienda varchar(30) primary key,
  7.      direttore varchar(20));
  8. create table if not exists hobby
  9.     (persona varchar(20) primary key,
  10.      hobby varchar(15));
  11.  
  12. -- populates tables
  13. insert into dirige values
  14.     ('Alwil Software', 'White'),
  15.     ('Piriform', 'Sanders'),
  16.     ('Mozilla', 'Sanders'),
  17.     ('COMODO', 'Greyhound'),
  18.     ('Ikebana', 'Greyhound'),
  19.     ('Zen', 'Powell'),
  20.     ('Nintendo', 'Masuda'),
  21.     ('Pokemon', 'Tajiri');
  22.  
  23. insert into hobby values
  24.     ('Roger', null),
  25.     ('Sanders', 'chess'),
  26.     ('Tritan', 'swimming'),
  27.     ('Greyhound', null),
  28.     ('Tajiri', 'chess'),
  29.     ('Powell', 'collections');
  30.  
  31. -- EXERCISE 1.1
  32. -- shows CEOs without a hobby
  33. select distinct d.direttore
  34.     as persona
  35.     from dirige d, hobby h
  36.     where (direttore = h.persona
  37.            and h.hobby is not null);
  38. -- alternate solution: views
  39. create view ceos
  40.     as select direttore as individuo
  41.     from dirige;
  42. create view hobbies
  43.     as select persona
  44.     from hobby
  45.     where hobby is not null;
  46.  
  47. select distinct persona
  48.     from ceos c, hobbies h
  49.     where c.individuo = h.persona;
  50. drop view if exists ceos;
  51. drop view if exists hobbies;
  52.  
  53. -- EXERCISE 1.2
  54. -- shows pairs of CEOs that have at least one hobby in common
  55. select distinct d1.direttore, d2.direttore
  56.     from dirige d1, dirige d2, hobby h1, hobby h2
  57.     where (d1.direttore = h1.persona and d2.direttore = h2.persona -- both have hobbies,
  58.            and h1.hobby = h2.hobby                                 -- and said hobbies coincide,
  59.            and d1.direttore <> d2.direttore);                      -- but they are not the same person
  60.  
  61. -- EXERCISE 1.3
  62. -- shows CEOs that have less hobbies compared to the number of companies they are in charge of
  63. create view ceos
  64.     as select direttore as individuo
  65.     from dirige;
  66. create view hobbies
  67.     as select persona
  68.     from hobby; -- there can be no hobbies this time!
  69. -- note: this part of the code does not give the expected results (Greyhound and Powell) and produces an empty set instead
  70. -- it seems that "group by" constructs a set (aka a duplicate-less collection), therefore defeating the purpose of compare-selection
  71. select individuo
  72.     from ceos, hobbies
  73.     where individuo = persona
  74.     group by individuo
  75.     having count(persona) < count(individuo);
  76. drop view if exists ceos;
  77. drop view if exists hobbies;
  78.  
  79. -- removes data
  80. drop view if exists ceos;
  81. drop view if exists hobbies;
  82. drop table if exists dirige;
  83. drop table if exists hobby;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement