Guest User

Untitled

a guest
Oct 11th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. -- Drop the tables, in reverse order of dependency
  2. DROP TABLE parcel;
  3. DROP TABLE customer;
  4. DROP TABLE courier;
  5. -- Create the tables
  6. CREATE TABLE courier
  7. (
  8. cID NUMBER(6),
  9. courName VARCHAR2(50) NOT NULL,
  10. salary NUMBER(7,2) NOT NULL,
  11. courEmail VARCHAR2(30) DEFAULT '[email protected]',
  12. CONSTRAINT courier_pk PRIMARY KEY (cID),
  13. CONSTRAINT couremail_chk CHECK (courEmail LIKE '%@%') -- check constraint for email
  14. ) ;
  15. CREATE TABLE customer
  16. (
  17. cNo NUMBER (6),
  18. custName VARCHAR2(50) NOT NULL,
  19. cAddress VARCHAR2(50) NOT NULL,
  20. custEmail VARCHAR2(30) DEFAULT '[email protected]',
  21. CONSTRAINT cust_pk PRIMARY KEY (cNo),
  22. CONSTRAINT custemail_chk CHECK(custEmail LIKE '%@%') -- check constraint for email
  23. ) ;
  24. CREATE TABLE parcel
  25. (
  26. pNo NUMBER(6),
  27. custNo NUMBER(6),
  28. courierID NUMBER (6),
  29. pdate DATE NOT NULL,
  30. pvalue NUMBER(6,2) NOT NULL,
  31. CONSTRAINT parcel_pk PRIMARY KEY (pNo,custNo),
  32. CONSTRAINT parcel_courier_fk FOREIGN KEY (courierID) REFERENCES courier(cID), -- Foreign key to courier
  33. CONSTRAINT parcel_customer_fk FOREIGN KEY (custNo) REFERENCES customer(CNo), -- Foreign key to customer
  34. CONSTRAINT pvalue_chk CHECK (pvalue <2500.00) -- check constraint for parcel value
  35. ) ;
  36. -- insert the data
  37. -- the inserts are in the order of the lines included in the spec but could be grouped by table
  38. -- need to insert data into courier and customer before inserting into parcel due to the foreign key constraints
  39. INSERT
  40. INTO customer
  41. (
  42. cno,
  43. custname,
  44. caddress,
  45. custemail
  46. )
  47. VALUES
  48. (
  49. 1,
  50. 'Marshall Mathers',
  51. ' 21 Malibu Drive',
  52. );
  53. INSERT
  54. INTO courier
  55. (
  56. cid,
  57. courname,
  58. couremail,
  59. salary
  60. )
  61. VALUES
  62. (
  63. 1,
  64. 'Slim Shady',
  65. 20000
  66. );
  67. INSERT
  68. INTO parcel
  69. (
  70. pno,
  71. custno,
  72. courierid,
  73. pdate,
  74. pvalue
  75. )
  76. VALUES
  77. (
  78. 1,1,1,
  79. '01 Jan 2016',
  80. 20.00
  81. );
  82. INSERT
  83. INTO customer
  84. (
  85. cno,
  86. custname,
  87. caddress
  88. )
  89. VALUES
  90. (
  91. 2,
  92. 'Mick Jagger',
  93. '26 Sunset Boulevard'
  94. ); -- This is a partial insert because no email address was given, the default value will be used
  95. INSERT
  96. INTO courier
  97. (
  98. cid,
  99. courname,
  100. couremail,
  101. salary
  102. )
  103. VALUES
  104. (
  105. 2,
  106. 'Ruby Tuesday',
  107. 20000
  108. );
  109. INSERT
  110. INTO parcel
  111. (
  112. pno,
  113. custno,
  114. courierid,
  115. pdate,
  116. pvalue
  117. )
  118. VALUES
  119. (
  120. 1,2,2,
  121. '12 Apr 2016',
  122. 560.00
  123. );
  124. INSERT
  125. INTO customer
  126. (
  127. cno,
  128. custname,
  129. caddress,
  130. custemail
  131. )
  132. VALUES
  133. (
  134. 3,
  135. 'Ronnie Van Sant',
  136. '56 Cliché Avenue',
  137. );
  138. INSERT
  139. INTO courier
  140. (
  141. cid,
  142. courname,
  143. couremail,
  144. salary
  145. )
  146. VALUES
  147. (
  148. 3,
  149. 'Curtis Loew',
  150. 18000
  151. );
  152. INSERT
  153. INTO parcel
  154. (
  155. pno,
  156. custno,
  157. courierid,
  158. pdate,
  159. pvalue
  160. )
  161. VALUES
  162. (
  163. 1,3,3,
  164. '13 Jun 2016',
  165. 490.00
  166. );
  167. INSERT
  168. INTO customer
  169. (
  170. cno,
  171. custname,
  172. caddress,
  173. custemail
  174. )
  175. VALUES
  176. (
  177. 4,
  178. 'Gene Pitney',
  179. '77 3rd Avenue, Nashville',
  180. );
  181. INSERT
  182. INTO courier
  183. (
  184. cid,
  185. courname,
  186. salary
  187. )
  188. VALUES
  189. (
  190. 4,
  191. 'Liberty Valance' ,
  192. 17000
  193. );
  194. INSERT
  195. INTO parcel
  196. (
  197. pno,
  198. custno,
  199. courierid,
  200. pdate,
  201. pvalue
  202. )
  203. VALUES
  204. (
  205. 1,
  206. 4,
  207. 4,
  208. '14 May 2016',
  209. 67.00
  210. );
  211. -- last two inserts are for customers that already exist using couriers that already exist
  212. INSERT
  213. INTO parcel
  214. (
  215. pno,
  216. custno,
  217. courierid,
  218. pdate,
  219. pvalue
  220. )
  221. VALUES
  222. (
  223. 2,1,2,
  224. '01 Jul 2016 ',
  225. 56.00
  226. );--Custno=1 Marshall Mathers, CourierID=2 Ruby Tuesday
  227. INSERT
  228. INTO parcel
  229. (
  230. pno,
  231. custno,
  232. courierid,
  233. pdate,
  234. pvalue
  235. )
  236. VALUES
  237. (
  238. 2,3,4,
  239. '14 Jun 2016',
  240. 490.00
  241. );-- Custno=3 Ronnie Van Sant, CourierID=4
  242. COMMIT;
  243. /*3. Write an SQL statement to return details of all parcels with a value 50 and 500 without using < and > in the comparison.
  244. In your output you should:
  245. • include the name of the customer to who the parcel was delivered
  246. • include the name of the courier who delivered it
  247. • include the parcel value
  248. • sort the output in order of parcel value descending.
  249. Hint: This requires an inner join.
  250. */
  251. SELECT custname,
  252. courname,
  253. pvalue
  254. FROM parcel
  255. JOIN customer
  256. ON custno=cno
  257. JOIN courier
  258. ON courierid=cid
  259. WHERE pvalue BETWEEN 50 AND 500
  260. ORDER BY pvalue DESC;
  261. /*4. Modify the previous statement to include the date of the parcel (formatted as DD/MM/YYYY)
  262. and left pad parcel value with * to 5 digits.
  263. */
  264. SELECT custname,
  265. courname,
  266. TO_CHAR(pdate, 'dd/mm/yyyy') ,
  267. lpad(pvalue, 5,'*')
  268. FROM parcel
  269. JOIN customer
  270. ON custno=cno
  271. JOIN courier
  272. ON courierid=cid
  273. WHERE pvalue BETWEEN 50 AND 500
  274. ORDER BY pvalue DESC;
  275. /*5. Write an SQL statement to return the names of all customers and their email addresses. Format the output so that:
  276. • Customer name is uppercase
  277. • Customer email is lowercase
  278. • Include in your output the position of the @ symbol in the email address
  279. */
  280. SELECT UPPER(custname),
  281. LOWER(custemail),
  282. INSTR(custemail,'@')
  283. FROM customer;
  284. /*6. Modify the previous SQL so that you also include a substring of 5 letters to the right of the position of the @ symbol
  285. – this will require nesting. */
  286. SELECT UPPER(custname),
  287. LOWER(custemail),
  288. SUBSTR(CUSTEMAIL,instr(custemail,'@')+1,5)
  289. FROM customer;
  290. /*7. Write an SQL statement that outputs for each parcel the name of the customer to whom the parcel was sent and using a CASE statement a comment on the parcel value
  291. so that if the value is < 50 the output will read ‘Cheap’, if < 500 it will read ‘Mid Range’ and anything else will be ‘Expensive’, name the output of the case
  292. statement pvalue comment and sort the output in descending order of comment.
  293. */
  294. SELECT custname,
  295. CASE
  296. WHEN pvalue < 50
  297. THEN 'Cheap'
  298. WHEN pvalue <500
  299. THEN 'Mid Range'
  300. ELSE 'Expensive'
  301. END PvalueComment
  302. FROM parcel
  303. JOIN customer
  304. ON custno=cno
  305. ORDER BY pvaluecomment DESC ;
  306. /*8. Write a statement to output the details of all parcels with a value less than any parcel delivered to customer 1.
  307. Exclude customer 1 from the output.*/
  308. SELECT *
  309. FROM parcel
  310. WHERE pvalue < ANY
  311. (SELECT pvalue FROM parcel WHERE custno=4
  312. )
  313. AND custno<>4;
  314. /*9. Amend the statement to consider just customer 3 and output details of parcels with a value
  315. less than all parcels for customer 3. Exclude customer 3 from the output.*/
  316. SELECT *
  317. FROM parcel
  318. WHERE pvalue < ALL
  319. (SELECT pvalue FROM parcel WHERE custno=3
  320. )
  321. AND custno<>3;
Advertisement
Add Comment
Please, Sign In to add comment