Guest User

Untitled

a guest
Jan 21st, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.12 KB | None | 0 0
  1. /* Creation of tables for part2*/
  2.  
  3. mysql> create database exercise1_2;
  4. Query OK, 1 row affected (0.00 sec)
  5.  
  6. mysql> use exercise1_2;
  7. Database changed
  8. mysql> create table tastes
  9. -> (
  10. -> name char(15) not null,
  11. -> filling char(15) not null
  12. -> );
  13. Query OK, 0 rows affected (0.10 sec)
  14.  
  15. mysql> select * from tastes;
  16. +-------+---------+
  17. | name | filling |
  18. +-------+---------+
  19. | Brown | Turkey |
  20. | Brown | Beef |
  21. | Brown | Ham |
  22. | Jones | Cheese |
  23. | Green | Beef |
  24. | Green | Turkey |
  25. | Green | Cheese |
  26. +-------+---------+
  27. 7 rows in set (0.00 sec)
  28.  
  29. mysql> create table locations
  30. -> (
  31. -> id int not null auto_increment,
  32. -> LName varchar(20) not null,
  33. -> Phone int(11) not null,
  34. -> Address varchar(30) not null,
  35. -> PRIMARY KEY(id)
  36. -> );
  37. Query OK, 0 rows affected (0.11 sec)
  38.  
  39. mysql> select * from locations;
  40. +----+-----------+---------+---------------+
  41. | id | LName | Phone | Address |
  42. +----+-----------+---------+---------------+
  43. | 1 | Lincoln | 6834523 | Lincoln Place |
  44. | 2 | O'Neill's | 6742134 | Pearse St |
  45. | 3 | Old Nag | 7678132 | Dame St |
  46. | 4 | Buttery | 7023421 | College St |
  47. +----+-----------+---------+---------------+
  48. 4 rows in set (0.00 sec)
  49.  
  50. mysql> create table sandwiches
  51. -> (
  52. -> Location varchar(20) not null,
  53. -> Bread char(10) not null,
  54. -> Filling char(15) not null,
  55. -> Price double not null
  56. -> );
  57. Query OK, 0 rows affected (0.08 sec)
  58.  
  59. mysql> select * from sandwiches;
  60. +-----------+-------+---------+-------+
  61. | Location | Bread | Filling | Price |
  62. +-----------+-------+---------+-------+
  63. | Lincoln | rye | ham | 1.25 |
  64. | O'Neill's | white | cheese | 1.2 |
  65. | O'Neill's | whole | ham | 1.25 |
  66. | Old Nag | rye | beef | 1.35 |
  67. | Buttery | white | cheese | 1 |
  68. | O'Neill's | white | turkey | 1.35 |
  69. | Buttery | white | ham | 1.1 |
  70. | Lincoln | rye | beef | 1.35 |
  71. | Lincoln | white | ham | 1.3 |
  72. | Old Nag | rye | ham | 1.4 |
  73. +-----------+-------+---------+-------+
  74. 10 rows in set (0.00 sec)
  75.  
  76. /*Query 2.2 solution*/
  77. mysql> select sandwiches.location
  78. -> from tastes left join sandwiches
  79. -> on tastes.filling = sandwiches.filling
  80. -> where tastes.name = 'jones';
  81. +-----------+
  82. | location |
  83. +-----------+
  84. | O'Neill's |
  85. | Buttery |
  86. +-----------+
  87. 2 rows in set (0.00 sec)
  88.  
  89. mysql> alter table tastes add constraint pk_tasteID PRIMARY KEY(name,filling);
  90. Query OK, 7 rows affected (0.23 sec)
  91. Records: 7 Duplicates: 0 Warnings: 0
  92.  
  93. mysql> alter table sandwiches add constraint pk_sandID PRIMARY KEY(location,bread,filling);
  94. Query OK, 10 rows affected (0.05 sec)
  95. Records: 10 Duplicates: 0 Warnings: 0
  96.  
  97. mysql> alter table locations drop PRIMARY KEY;
  98. ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
  99. mysql> alter table locations drop column id;
  100. Query OK, 4 rows affected (0.06 sec)
  101. Records: 4 Duplicates: 0 Warnings: 0
  102.  
  103. mysql> select * from locations;
  104. +-----------+---------+---------------+
  105. | LName | Phone | Address |
  106. +-----------+---------+---------------+
  107. | Lincoln | 6834523 | Lincoln Place |
  108. | O'Neill's | 6742134 | Pearse St |
  109. | Old Nag | 7678132 | Dame St |
  110. | Buttery | 7023421 | College St |
  111. +-----------+---------+---------------+
  112. 4 rows in set (0.00 sec)
  113.  
  114. mysql> alter table locations add PRIMARY KEY(lname);
  115. Query OK, 4 rows affected (0.07 sec)
  116. Records: 4 Duplicates: 0 Warnings: 0
  117. /* Query 2.1 solution*/
  118. mysql> select location from sandwiches where filling in(select filling from tastes where name='jones';
  119. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
  120. mysql> select location from sandwiches where filling in(select filling from tastes where name='jones');
  121. +-----------+
  122. | location |
  123. +-----------+
  124. | Buttery |
  125. | O'Neill's |
  126. +-----------+
  127. 2 rows in set (0.00 sec)
  128.  
  129. /*Query 2.3 solution */
  130. mysql> select sandwiches.location, count(distinct name) from tastes inner join sandwiches on tastes.filling = sandwiches.filling group by sandwiches.location;
  131. +-----------+----------------------+
  132. | location | count(distinct name) |
  133. +-----------+----------------------+
  134. | Buttery | 3 |
  135. | Lincoln | 2 |
  136. | O'Neill's | 3 |
  137. | Old Nag | 2 |
  138. +-----------+----------------------+
  139. 4 rows in set (0.05 sec)
  140.  
  141. /* Creation of database and tables for part 3 */
  142.  
  143. mysql> create database exercise1_3;
  144. Query OK, 1 row affected (0.00 sec)
  145.  
  146. mysql> use exercise1_3;
  147. Database changed
  148. mysql> create table Branches
  149. -> (
  150. -> bcode varchar(5) not null,
  151. -> librarian varchar(30) not null,
  152. -> address varchar(30) not null,
  153. -> PRIMARY KEY(bcode)
  154. -> );
  155. Query OK, 0 rows affected (0.13 sec)
  156.  
  157. mysql> create table titles
  158. -> (
  159. -> title char(20) not null Unique,
  160. -> author char(20) not null,
  161. -> publisher char(20) not null,
  162. -> PRIMARY KEY(title)
  163. -> );
  164. Query OK, 0 rows affected (0.07 sec)
  165.  
  166. mysql> create table holdings
  167. -> (
  168. -> branch varchar(5) not null,
  169. -> title char(20) not null,
  170. -> #copies int(10) not null,
  171. -> constraint pk_holdID PRIMARY KEY(branch,title),
  172. -> FOREIGN KEY(branch) REFERENCES branches(bcode),
  173. -> FOREIGN KEY(title) REFERENCES titles(title)
  174. -> );
  175. Query OK, 0 rows affected (0.11 sec)
  176.  
  177. mysql> select * from branches;
  178. +-------+---------------+---------------+
  179. | bcode | librarian | address |
  180. +-------+---------------+---------------+
  181. | B1 | John Smith | 2 anglesea rd |
  182. | B2 | Mary Jones | 34 pearse st |
  183. | B3 | Francis Owens | grange X |
  184. +-------+---------------+---------------+
  185. 3 rows in set (0.00 sec)
  186.  
  187. mysql> select * from titles;
  188. +---------------------+---------------+------------+
  189. | title | author | publisher |
  190. +---------------------+---------------+------------+
  191. | Susannah | Ann Brown | macmillian |
  192. | How to fish | Amy Fly | stop press |
  193. | A history of dublin | David Little | wiley |
  194. | Computers | Blaise Pascal | applewoods |
  195. | The Wife | Ann Brown | macmillian |
  196. +---------------------+---------------+------------+
  197. 5 rows in set (0.01 sec)
  198.  
  199. mysql> select * from holdings;
  200. +--------+---------------------+---------+
  201. | branch | title | #copies |
  202. +--------+---------------------+---------+
  203. | B1 | Susannah | 3 |
  204. | B1 | How to fish | 2 |
  205. | B1 | A history of Dublin | 1 |
  206. | B2 | how to fish | 4 |
  207. | B2 | Computers | 2 |
  208. | B2 | The wife | 3 |
  209. | B3 | A history of dublin | 1 |
  210. | B3 | Computers | 4 |
  211. | B3 | Susannah | 3 |
  212. | B3 | The Wife | 1 |
  213. +--------+---------------------+---------+
  214. 10 rows in set (0.00 sec)
  215.  
  216. /* Query 3.1 solution*/
  217. mysql> select title from titles where publisher = 'macmillian';
  218. +----------+
  219. | title |
  220. +----------+
  221. | Susannah |
  222. | The Wife |
  223. +----------+
  224. 2 rows in set (0.00 sec)
  225.  
  226. /*Query 3.3 solution*/
  227. mysql> select distinct holdings.branch
  228. -> from titles left join holdings
  229. -> on titles.title = holdings.title
  230. -> where titles.author = 'ann brown';
  231. +--------+
  232. | branch |
  233. +--------+
  234. | B1 |
  235. | B3 |
  236. | B2 |
  237. +--------+
  238. 3 rows in set (0.00 sec)
  239.  
  240. /*Query 3.4 solution*/
  241. mysql> select branch, count(branch)
  242. -> from holdings
  243. -> group by branch;
  244. +--------+---------------+
  245. | branch | count(branch) |
  246. +--------+---------------+
  247. | B1 | 3 |
  248. | B2 | 3 |
  249. | B3 | 4 |
  250. +--------+---------------+
  251. 3 rows in set (0.00 sec)
  252.  
  253. /*Query 3.2 solution*/
  254.  
  255. mysql> select distinct(branch) from holdings where title in(select title from titles where author = 'ann brown');
  256. +--------+
  257. | branch |
  258. +--------+
  259. | B1 |
  260. | B2 |
  261. | B3 |
  262. +--------+
  263. 3 rows in set (0.00 sec)
  264.  
  265. /*Query 3.4 redone*/
  266. mysql> select branch, sum(copies) from holdings group by branch;
  267. +--------+-------------+
  268. | branch | sum(copies) |
  269. +--------+-------------+
  270. | B1 | 6 |
  271. | B2 | 9 |
  272. | B3 | 9 |
  273. +--------+-------------+
  274. 3 rows in set (0.00 sec)
Add Comment
Please, Sign In to add comment