Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
121
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 'unknown@unknown.com',
  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 'unknown@unknown.com',
  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. ' mm@gmail.com'
  53. );
  54. INSERT
  55. INTO courier
  56. (
  57. cid,
  58. courname,
  59. couremail,
  60. salary
  61. )
  62. VALUES
  63. (
  64. 1,
  65. 'Slim Shady',
  66. ' ss@gmail.com',
  67. 20000
  68. );
  69. INSERT
  70. INTO parcel
  71. (
  72. pno,
  73. custno,
  74. courierid,
  75. pdate,
  76. pvalue
  77. )
  78. VALUES
  79. (
  80. 1,1,1,
  81. '01 Jan 2016',
  82. 20.00
  83. );
  84. INSERT
  85. INTO customer
  86. (
  87. cno,
  88. custname,
  89. caddress
  90. )
  91. VALUES
  92. (
  93. 2,
  94. 'Mick Jagger',
  95. '26 Sunset Boulevard'
  96. ); -- This is a partial insert because no email address was given, the default value will be used
  97. INSERT
  98. INTO courier
  99. (
  100. cid,
  101. courname,
  102. couremail,
  103. salary
  104. )
  105. VALUES
  106. (
  107. 2,
  108. 'Ruby Tuesday',
  109. 'rt@gmail.com',
  110. 20000
  111. );
  112. INSERT
  113. INTO parcel
  114. (
  115. pno,
  116. custno,
  117. courierid,
  118. pdate,
  119. pvalue
  120. )
  121. VALUES
  122. (
  123. 1,2,2,
  124. '12 Apr 2016',
  125. 560.00
  126. );
  127. INSERT
  128. INTO customer
  129. (
  130. cno,
  131. custname,
  132. caddress,
  133. custemail
  134. )
  135. VALUES
  136. (
  137. 3,
  138. 'Ronnie Van Sant',
  139. '56 Cliché Avenue',
  140. 'rvs@gmail.com'
  141. );
  142. INSERT
  143. INTO courier
  144. (
  145. cid,
  146. courname,
  147. couremail,
  148. salary
  149. )
  150. VALUES
  151. (
  152. 3,
  153. 'Curtis Loew',
  154. 'cl@gmail.com',
  155. 18000
  156. );
  157. INSERT
  158. INTO parcel
  159. (
  160. pno,
  161. custno,
  162. courierid,
  163. pdate,
  164. pvalue
  165. )
  166. VALUES
  167. (
  168. 1,3,3,
  169. '13 Jun 2016',
  170. 490.00
  171. );
  172. INSERT
  173. INTO customer
  174. (
  175. cno,
  176. custname,
  177. caddress,
  178. custemail
  179. )
  180. VALUES
  181. (
  182. 4,
  183. 'Gene Pitney',
  184. '77 3rd Avenue, Nashville',
  185. 'gp@gmail.com '
  186. );
  187. INSERT
  188. INTO courier
  189. (
  190. cid,
  191. courname,
  192. salary
  193. )
  194. VALUES
  195. (
  196. 4,
  197. 'Liberty Valance' ,
  198. 17000
  199. );
  200. INSERT
  201. INTO parcel
  202. (
  203. pno,
  204. custno,
  205. courierid,
  206. pdate,
  207. pvalue
  208. )
  209. VALUES
  210. (
  211. 1,
  212. 4,
  213. 4,
  214. '14 May 2016',
  215. 67.00
  216. );
  217. -- last two inserts are for customers that already exist using couriers that already exist
  218. INSERT
  219. INTO parcel
  220. (
  221. pno,
  222. custno,
  223. courierid,
  224. pdate,
  225. pvalue
  226. )
  227. VALUES
  228. (
  229. 2,1,2,
  230. '01 Jul 2016 ',
  231. 56.00
  232. );--Custno=1 Marshall Mathers, CourierID=2 Ruby Tuesday
  233. INSERT
  234. INTO parcel
  235. (
  236. pno,
  237. custno,
  238. courierid,
  239. pdate,
  240. pvalue
  241. )
  242. VALUES
  243. (
  244. 2,3,4,
  245. '14 Jun 2016',
  246. 490.00
  247. );-- Custno=3 Ronnie Van Sant, CourierID=4
  248. COMMIT;
  249. /*3. Write an SQL statement to return details of all parcels with a value 50 and 500 without using < and > in the comparison.
  250. In your output you should:
  251. • include the name of the customer to who the parcel was delivered
  252. • include the name of the courier who delivered it
  253. • include the parcel value
  254. • sort the output in order of parcel value descending.
  255. Hint: This requires an inner join.
  256. */
  257. SELECT custname,
  258. courname,
  259. pvalue
  260. FROM parcel
  261. JOIN customer
  262. ON custno=cno
  263. JOIN courier
  264. ON courierid=cid
  265. WHERE pvalue BETWEEN 50 AND 500
  266. ORDER BY pvalue DESC;
  267. /*4. Modify the previous statement to include the date of the parcel (formatted as DD/MM/YYYY)
  268. and left pad parcel value with * to 5 digits.
  269. */
  270. SELECT custname,
  271. courname,
  272. TO_CHAR(pdate, 'dd/mm/yyyy') ,
  273. lpad(pvalue, 5,'*')
  274. FROM parcel
  275. JOIN customer
  276. ON custno=cno
  277. JOIN courier
  278. ON courierid=cid
  279. WHERE pvalue BETWEEN 50 AND 500
  280. ORDER BY pvalue DESC;
  281. /*5. Write an SQL statement to return the names of all customers and their email addresses. Format the output so that:
  282. • Customer name is uppercase
  283. • Customer email is lowercase
  284. • Include in your output the position of the @ symbol in the email address
  285. */
  286. SELECT UPPER(custname),
  287. LOWER(custemail),
  288. INSTR(custemail,'@')
  289. FROM customer;
  290. /*6. Modify the previous SQL so that you also include a substring of 5 letters to the right of the position of the @ symbol
  291. – this will require nesting. */
  292. SELECT UPPER(custname),
  293. LOWER(custemail),
  294. SUBSTR(CUSTEMAIL,instr(custemail,'@')+1,5)
  295. FROM customer;
  296. /*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
  297. 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
  298. statement pvalue comment and sort the output in descending order of comment.
  299. */
  300. SELECT custname,
  301. CASE
  302. WHEN pvalue < 50
  303. THEN 'Cheap'
  304. WHEN pvalue <500
  305. THEN 'Mid Range'
  306. ELSE 'Expensive'
  307. END PvalueComment
  308. FROM parcel
  309. JOIN customer
  310. ON custno=cno
  311. ORDER BY pvaluecomment DESC ;
  312. /*8. Write a statement to output the details of all parcels with a value less than any parcel delivered to customer 1.
  313. Exclude customer 1 from the output.*/
  314. SELECT *
  315. FROM parcel
  316. WHERE pvalue < ANY
  317. (SELECT pvalue FROM parcel WHERE custno=4
  318. )
  319. AND custno<>4;
  320. /*9. Amend the statement to consider just customer 3 and output details of parcels with a value
  321. less than all parcels for customer 3. Exclude customer 3 from the output.*/
  322. SELECT *
  323. FROM parcel
  324. WHERE pvalue < ALL
  325. (SELECT pvalue FROM parcel WHERE custno=3
  326. )
  327. AND custno<>3;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement