Advertisement
Urthor

Object Oriented Database Assignment DDL

Oct 6th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.19 KB | None | 0 0
  1. -- create OMDB --
  2. -- drop tables --
  3. drop view all_albums
  4. /
  5. drop table albums
  6. /
  7. drop type disk_type
  8. /
  9. drop type mp3_type
  10. /
  11. drop type album_type
  12. /
  13. drop type artist_array_type
  14. /
  15. drop type artist_type
  16. /
  17. drop type review_table_type
  18. /
  19. drop type review_type
  20. /
  21. -- create types --
  22. create or replace type artist_type as object
  23. (artistName varchar(50),
  24. artistRole varchar(25))
  25. /
  26. create type artist_array_type
  27. as varray(5) of artist_type
  28. /
  29. create or replace type review_type as object
  30. (reviewerName varchar(25),
  31. reviewDate date,
  32. reviewText varchar(250),
  33. reviewScore number)
  34. /
  35. create or replace type review_table_type as table of review_type
  36. /
  37. create or replace type album_type as object
  38. (albumTitle varchar(50),
  39. albumPlaytime number(3), -- minutes
  40. albumReleaseDate date,
  41. albumGenre varchar(15),
  42. albumPrice number(9,2),
  43. albumTracks number(2),
  44. albumArtists artist_array_type,
  45. albumReviews review_table_type,
  46. member function discountPrice return number,
  47. member function containsText (pString1 varchar2, pString2 varchar2) return integer)
  48. not instantiable not final
  49. /
  50. create or replace type disk_type under album_type
  51. ( mediaType varchar(10),
  52. diskNum number(2), -- number of disks
  53. diskUsedPrice number(9,2),
  54. diskDeliveryCost number(9,2),
  55. overriding member function discountPrice return number)
  56. /
  57. create or replace type mp3_type under album_type
  58. (downloadSize number, -- size in MB
  59. overriding member function discountPrice return number)
  60. /
  61. /
  62. -- Implement function
  63. create or replace type body album_type
  64. as
  65. member function discountPrice return number is
  66. v_discount number(9,2);
  67. BEGIN
  68. null;
  69. END;
  70. member function containsText (pString1 varchar2, pString2 varchar2) return integer is
  71. BEGIN
  72. if INSTR(lower(pString1),lower(pString2)) > 0 then return 1;
  73. else return 0;
  74. end if;
  75. END;
  76. END;
  77. /
  78. -- Overriding Function
  79. create or replace type body disk_type as
  80. overriding member function discountPrice
  81. return number is v_discount number(9,2);
  82. begin
  83. if mediaType = 'Vinyl' and (albumReleaseDate < (Sysdate - interval '1' year)) then return (0.8*albumPrice);
  84. else if mediaType = 'Audio CD' and (albumReleaseDate < (Sysdate - interval '1' year)) then return (0.85*albumPrice);
  85. else return albumPrice;
  86. end if;
  87. end if;
  88. end;
  89. end;
  90. /
  91. create or replace type body mp3_type as
  92. overriding member function discountPrice
  93. return number is v_discount number(9,2);
  94. begin
  95. if albumReleaseDate < (Sysdate - interval '2' year) then return (0.9*albumPrice);
  96. else return albumPrice;
  97. end if;
  98. end;
  99. end;
  100. /
  101. --- MP3 TYPE --
  102. create or replace type body mp3_type as
  103. overriding member function discountPrice
  104. return number is v_discount number(9,2);
  105. begin
  106. if albumReleaseDate < (Sysdate - interval '2' year) then return (0.9*albumPrice);
  107. else return albumPrice;
  108. end if;
  109. end;
  110. end;
  111. /
  112. -- CONTAINS
  113. --create or replace type body disk_type as
  114. --overriding member function containsText
  115. -- return integer is StringDoesContain integer;
  116. -- begin
  117. -- if Contains(lower(pString1),lower(pString2)) then return 1;
  118. -- else return 0;
  119. -- end if;
  120. -- end;
  121. --end;
  122. /
  123. -- create tables --
  124. create table albums of album_type
  125. object id system generated
  126. nested table albumReviews store as store_reviews
  127. /
  128. -----------
  129. -- Vinyl --
  130. -----------
  131. INSERT INTO albums
  132. values(
  133. disk_type('The Essential Bob Dylan', 99, '8-Jul-2016', 'Pop', 37.00, 32,
  134. artist_array_type( artist_type('Bob Dylan', 'Composer'),
  135. artist_type('Bob Dylan', 'Vocals')
  136. ),
  137. review_table_type( review_type('Shawn', '2-Aug-2019', 'Great compilation of some of his most known songs!', 5),
  138. review_type('Reuben', '18-Feb-2013', 'Captures Bob Dylan transformation from a folk song composer', 5)
  139. ),
  140. 'Vinyl', 2, null, 11.0
  141. )
  142. )
  143. /
  144. INSERT INTO albums
  145. values(
  146. disk_type('Sketches of Spain', 45, '8-Mar-2011', 'Jazz', 14.99, 6,
  147. artist_array_type( artist_type('Miles Davis', 'Composer'),
  148. artist_type('Miles Davis', 'Musician')
  149. ),
  150. review_table_type( review_type('Frederick', '16-Sep-2016', 'Recommend listening while viewing sunset', 5),
  151. review_type('Juliet', '12-Mar-2018', 'Early days of the Great Miles--no lover of jazz should be without this album.', 5)
  152. ),
  153. 'Vinyl', 1, 16.29, 7.00
  154. )
  155. )
  156. /
  157. INSERT INTO albums
  158. values(
  159. disk_type('Bob Dylan''s Greatest Hits',45,'31-Jan-2017','Pop Rock',29.87,10,
  160. artist_array_type(artist_type('Bob Dylan','Composer'),
  161. artist_type('Bob Dylan','Vocals')
  162. ),
  163. review_table_type(
  164. review_type('Kandy','16-Mar-2015','Early Dylan in all his glory.',5),
  165. review_type('Steward','18-Feb-2013','Captures Bob Dylan transformation from a folk song Composer to a rock legend',4)
  166. ), 'Vinyl',1,null,11.00
  167. )
  168. )
  169.  
  170. /
  171. INSERT INTO albums
  172. values(
  173. disk_type('Harvest (2009 Remaster)', 44, '21-Jun-2009', 'Rock Country', 28.50, 10,
  174. artist_array_type( artist_type('Neil Young', 'Composer'),
  175. artist_type('Neil Young', 'Vocals')
  176. ),
  177. review_table_type( review_type('John', '18-Feb-2019', 'I absolutely LOVE this CD!', 5),
  178. review_type('Stewart', '18-Feb-2013', 'Sound good in vinyl!', 5)
  179. ),
  180. 'Vinyl', 1, 14.99, 11.00
  181. )
  182. )
  183. /
  184. INSERT INTO albums
  185. values(
  186. disk_type('Kind of Blue (Legacy Edition)', 155, '20-Jan-2009', 'Jazz', 19.99, 21,
  187. artist_array_type( artist_type('Miles Davis', 'Composer'),
  188. artist_type('Miles Davis', 'Musician')
  189. ),
  190. review_table_type( review_type('Laurence', '10-Sep-2014', 'Very very special recording.', 5)
  191. ),
  192. 'Vinyl', 3, 16.99, 10.00
  193. )
  194. )
  195. /
  196. --Audio CDs--
  197. INSERT INTO albums
  198. values(
  199. disk_type('Harvest (2009 Remaster)', 44, '21-Jun-2009', 'Rock Country', 10.50, 10,
  200. artist_array_type( artist_type('Neil Young', 'Composer'),
  201. artist_type('Neil Young', 'Vocals')
  202. ),
  203. review_table_type( review_type('John', '18-Feb-2019', 'I absolutely LOVE this CD!', 5),
  204. review_type('Anthony', '16-Aug-2019', 'Neil Young''s signature album.', 4)
  205. ),
  206.  
  207. 'Audio CD', 1, 4.99, 11.00
  208. )
  209. )
  210. /
  211. INSERT INTO albums
  212. values(
  213. disk_type('The Essential Bob Dylan', 99, '8-Jul-2016', 'Pop', 26.17, 32,
  214. artist_array_type( artist_type('Bob Dylan', 'Composer'),
  215. artist_type('Bob Dylan', 'Vocals')
  216. ),
  217. review_table_type( review_type('Christopher', '24-Jun-2016', 'This is a terrific album.', 5),
  218. review_type('Cauley', '2-Aug-2015', 'There can only be one Bob Dylan. God blessed him with the gift of verse.', 5)
  219. ),
  220. 'Audio CD', 2, null, 7.00
  221. )
  222. )
  223. /
  224. INSERT INTO albums
  225. values(
  226. disk_type('Bob Dylan''s Greatest Hits', 50, '1-Jun-1999', 'Pop Rock', 20.81, 10,
  227. artist_array_type( artist_type('Bob Dylan', 'Composer'),
  228. artist_type('Bob Dylan', 'Vocals')
  229. ),
  230. review_table_type( review_type('Kandy', '16-Mar-2015', 'Early Dylan in all his glory.', 5),
  231. review_type('Stewart', '18-Feb-2013', 'Captures Bob Dylan transformation from a folk song composer to a rock legend.', 4)
  232. ),
  233. 'Audio CD', 1, null, 7.00
  234. )
  235. )
  236. /
  237. INSERT INTO albums
  238. values(
  239. disk_type('Kind Of Blue (Legacy Edition)', 155, '20-Jan-2009', 'Jazz',19.99, 21,
  240. artist_array_type( artist_type('Miles Davis', 'Composer'),
  241. artist_type('Miles Davis', 'Musician')
  242. ),
  243. review_table_type( review_type('Amy', '17-Apr-2018', 'Poor quality sound compared to the vinyl record.', 2)
  244. ),
  245. 'Audio CD', 3, 16.99, 10.00
  246. )
  247. )
  248.  
  249. /
  250. INSERT INTO albums
  251. values(
  252. disk_type('Sketches of Spain', 45, '20-Jan-2009', 'Jazz', 3.11, 6,
  253. artist_array_type( artist_type('Miles Davis', 'Composer'),
  254. artist_type('Miles Davis', 'Musician')
  255. ),
  256. review_table_type( review_type('Sara', '3-Oct-2016', 'Another must have! One of Miles finest works', 5),
  257. review_type('Douglas', '14-Jun-2014', 'You might like it, but I admit it seems like a difficult listen.', 5)
  258. ),
  259. 'Audio CD', 1, 6.41, 7.00
  260. )
  261. )
  262. /
  263. INSERT INTO albums
  264. values(
  265. disk_type('Gustav Mahler Symphony No.9 ', 45, '12-Oct-2017', 'Classical', 23.10, 5,
  266. artist_array_type( artist_type('David Zinman', 'Conductor'),
  267. artist_type('Gustav Miller', 'Composer'),
  268. artist_type('Tonhalle Orchaestra', 'Orchestra')
  269. ),
  270. review_table_type( review_type('Lindon', '3-Dec-2010', 'This is an uneventful but fine recording.', 3),
  271. review_type('Prescott', '24-Aug-2013', 'This is truly a spellbinding record.', 5)
  272. ),
  273. 'Audio CD', 1, 15.20, 7.00
  274. )
  275. )
  276. /
  277. ---MP3 Queries---
  278. INSERT INTO albums
  279. values(
  280. mp3_type('Bob Dylan''s Greatest Hits', 55, '1-Jan-2019', 'Pop Rock', 5.98, 10,
  281. artist_array_type(
  282. artist_type('Bob Dylan', 'Composer'),
  283. artist_type('Bob Dylan', 'Vocals')
  284. ),
  285. review_table_type( review_type('Mandy', '16-Mar-2019', 'Fantastic music!.', 5)
  286. ),
  287. 60
  288. )
  289. )
  290. /
  291. INSERT INTO albums
  292. values(
  293. mp3_type('Best of Neil Young', 153, '21-Feb-2019', 'Pop Rock', 17.50, 35,
  294. artist_array_type( artist_type('Neil Young', 'Composer'),
  295. artist_type('Neil Young', 'Vocals')
  296. ),
  297. review_table_type( review_type('John', '16-Apr-2019', 'Great artist and great music.', 5)
  298. ),
  299. 165
  300. )
  301. )
  302. /
  303. INSERT INTO albums
  304. values(
  305. mp3_type('Harvest (2009 Remaster)', 44, '21-Jun-2009', 'Rock Country', 9.49, 10,
  306. artist_array_type( artist_type('Neil Young', 'Composer'),
  307. artist_type('Neil Young', 'Vocals')
  308. ),
  309. review_table_type( review_type('John', '16-Apr-2019', 'Great artist and great music.', 5)
  310. ),
  311. 52
  312. )
  313. )
  314. /
  315. INSERT INTO albums
  316. values(
  317. mp3_type('Sketches of Spain', 45, '16-Aug-2013', 'Jazz', 24.99, 6,
  318. artist_array_type( artist_type('Miles Davis', 'Composer'),
  319. artist_type('Miles Davis', 'Musician')
  320. ),
  321. review_table_type( review_type('Douglas', '14-Jun-2014', 'You might like it but I admit it seems like a difficult listen.', 5)
  322. ),
  323. 51
  324. )
  325. )
  326. /
  327. INSERT INTO albums
  328. values(
  329. mp3_type('B.B. King Greatest Hits', 114, '16-Jul-2013', 'Rock Blues', 11.49, 24,
  330. artist_array_type( artist_type('B.B. King', 'Vocals'),
  331. artist_type('B.B. King', 'Guitar')
  332. ),
  333. review_table_type( review_type('David', '18-May-2015', 'You might like it but I admit it seems like a difficult listen.', 5)
  334.  
  335. ),
  336. 125
  337. )
  338. )
  339. /
  340. INSERT INTO albums
  341. values(
  342. mp3_type('The Essential Bob Dylan', 99, '8-Jul-2016', 'Pop', 16.00, 32,
  343. artist_array_type( artist_type('Bob Dylan', 'Composer'),
  344. artist_type('Bob Dylan', 'Vocals')
  345. ),
  346. review_table_type( review_type('Christopher', '24-Jun-2016', 'This is a terrific album.', 5),
  347. review_type('Cauley', '2-Apr-2015', 'There can only be one Bob Dylan. God blessed him with the gift of verse', 5)
  348. ),
  349. 112
  350. )
  351. )
  352. /
  353. INSERT INTO albums
  354. values(
  355. mp3_type('Other Peoples Lives', 42, '15-Feb-2019', 'Rock Dance', 9.49, 10,
  356. artist_array_type( artist_type('Stats', 'Composer'),
  357. artist_type('Stats', 'Vocals')
  358. ),
  359. review_table_type( review_type('George', '17-Sep-2019', 'Good dancing music.', 3)
  360. ),
  361. 45
  362. )
  363. )
  364. /
  365. -- Query 1 --
  366. select DISTINCT ALBUMTITLE, ALBUMRELEASEDATE, ALBUMPRICE from albums a, table(a.ALBUMARTISTS) c
  367. where ALBUMRELEASEDATE > '1-Jan-2015' and c.artistName = 'Neil Young'
  368. /
  369. -- Query 2 =-
  370. select DISTINCT albumtitle, c.artistname from albums a, table(a.ALBUMARTISTS) c
  371. where value(a) IS OF (MP3_TYPE)
  372. order by ALBUMTITLE
  373. /
  374. -- Query 3 --
  375. with abc as (
  376. select albumTitle, avg(c.reviewScore) as averageScore from albums a, (table(a.ALBUMREVIEWS)) c
  377. where value(a) IS OF (MP3_TYPE)
  378. having count(c.reviewScore) > 1
  379. group by albumTitle
  380. )
  381. select * from abc
  382. where averageScore = (select min(averageScore) from abc)
  383. /
  384. --- Query 4 ---
  385. select albumtitle from albums a
  386. where value(a) IS OF (DISK_TYPE)
  387. INTERSECT
  388. select albumtitle from albums a
  389. where value(a) IS OF (MP3_TYPE)
  390. /
  391. -- Q5 answer is on line ---
  392. -- View for Q6 and Q7
  393. create or replace view all_albums (albumTitle, albumType, albumPrice, discount, albumUsedPrice) as
  394. select a.albumTitle, coalesce(treat(value(a) as Disk_Type).MediaType,'MP3'),
  395. albumPrice, (albumPrice-a.discountPrice()),
  396. coalesce(treat(value(a) as Disk_Type).diskUsedPrice , 0)
  397. from albums a;
  398. /
  399. -- Q6 Select the largest discount
  400. select * from all_albums
  401. where discount = (select max(discount) from all_albums)
  402. /
  403. --- Q7 Select the largest usedPrice
  404. select * from all_albums
  405. where albumUsedPrice = (select max(albumUsedPrice) from all_albums)
  406. /
  407. -- Q8 Select AlbumTitle, ReviewScore that has word great
  408. select DISTINCT albumTitle, c.reviewScore from albums a, table(a.ALBUMREVIEWS) c
  409. where a.containsText(c.ReviewText, 'great') > 0
  410. /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement