Guest User

Untitled

a guest
Feb 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. 1 CREATE TABLES Question
  2. DIVISION
  3. Column Name DIVISION_ID DIVISION_NAME
  4. Key Type PK
  5. Null/Unique NN, U
  6. FK Table
  7. FK Column
  8. Validation
  9. Datatype NUMBER VARCHAR
  10. Length 3 25
  11. Sample data
  12.  
  13. 10 East Coast
  14. 20 Quebec
  15. 30 Ontario
  16.  
  17. WAREHOUSE
  18. Column Name WAREHOUSE_ID CITY RATING FOUND_DATE DIVISION_ID
  19. Key Type PK CK FK
  20. Null/Unique NN, U NN NN
  21. FK Table DIVISION
  22. FK Column DIVISION_ID
  23. Validation A, B, C, D
  24. Datatype NUMBER VARCHAR CHAR DATE NUMBER
  25. Length 3 15 1 3
  26. Sample Data 1 Montreal A Current date 10
  27. 7 Fredericton B Current date 10
  28. 10 Toronto A Current date 30
  29.  
  30. SECTION
  31. Column Name WAREHOUSE_ID SECTION_ID DESCRIPTION CAPACITY
  32. Key Type PK, FK PK
  33. Null/Unique NN
  34. FK Table WAREHOUSE
  35. FK Column WAREHOUSE_ID
  36. Datatype NUMBER NUMBER VARCHAR NUMBER
  37.  
  38. Length 3 2 50 8
  39.  
  40. Sample data 1 1 Whse 1 Floor 1 2000
  41. 1 2 Whse 1 Floor 2 500
  42. 7 1 Whse 7 Floor 1 15000
  43.  
  44. 1 (10 marks) Write the required SQL statements to create tables WAREHOUSE, DIVISION and SECTION.
  45.  
  46.  
  47. Follow these general rules in the process:
  48. A. Create all CHECK (incl. NOT NULL) and UNIQUE as column level constraints
  49. Constraint names needed for CHECK constraints.
  50. The other constraints (NN and UK) do not need a name.
  51. B. Create all PK and FK constraints at the table level and give them proper names.
  52.  
  53. PUT ANSWERS starting here
  54.  
  55. create table division (
  56. division_id number(3),
  57. division_name varchar2(25)
  58. constraint div_division_name_uq unique
  59. constraint div_division_name_nn not null,
  60. constraint div_division_id_pk primary key (division_id)
  61. );
  62.  
  63. create table warehouse (
  64. warehouse_id number(3),
  65. city varchar2(15)
  66. constraint wh_city_uq unique
  67. constraint wh_city_nn not null,
  68. rating char(1)
  69. constraint wh_rating_ck check (rating in('A','B','C','D')),
  70. found_date date default sysdate
  71. constraint wh_found_date_nn not null,
  72. division_id number(3)
  73. constraint wh_division_id_nn not null,
  74. constraint wh_warehouse_id_pk primary key (warehouse_id),
  75. constraint wh_division_id_fk foreign key (division_id) references division(division_id)
  76. );
  77.  
  78. create table section (
  79. warehouse_id number(3),
  80. section_id number(2),
  81. description varchar2(50)
  82. constraint sec_section_nn not null,
  83. capacity number (8),
  84. constraint sec_warehouse_id_section_id_pk primary key (warehouse_id, section_id),
  85. constraint sec_warehouse_id_fk foreign key (warehouse_id) references warehouse(warehouse_id)
  86. );
  87.  
  88.  
  89. insert all
  90. into division values (10, 'East Coast')
  91. into division values (20, 'Quebec')
  92. into division values (30, 'Ontario')
  93. select 1 from dual;
  94.  
  95. insert all
  96. into warehouse values (1, 'Montreal', 'A', sysdate, 10)
  97. into warehouse values (7, 'Fredericton', 'B', sysdate, 10)
  98. into warehouse values (10, 'Toronto', 'A', sysdate, 10)
  99. select 1 from dual;
  100.  
  101. insert all
  102. into section values (1, 1, 'Whse 1 Floor 1', 2000)
  103. into section values (1, 2, 'Whse 1 Floor 2', 500)
  104. into section values (7, 1, 'Whse 7 Floor 1', 15000)
  105. select 1 from dual;
  106.  
  107. 2 (3 marks) After creating all tables add column MGR_ID to table SECTION as a FK column that is related to the PK column EMPLOYEE_ID in table EMPLOYEE
  108.  
  109. alter table section
  110. add (mgr_id number(6,0)
  111. constraint sec_mgr_id_fk references employees(employee_id));
  112.  
  113. 3 (3 marks) Modify the CHECK constraint on column RATING in table WAREHOUSE, so that it also may accept a new value F.
  114.  
  115. alter table warehouse
  116. drop constraint wh_rating_ck;
  117.  
  118. alter table warehouse
  119. add constraint wh_rating_ck check (rating in('A','B','C','D','F'));
  120.  
  121. 4 (3 marks) Create a new Sequence called Whse_id_seq that will generate unique numbers for PK values in table WAREHOUSE, so that the numbers start at 320 with the step of 15 and upper limit is 900 and will have NO values stored in the memory.
  122.  
  123. create sequence whse_id_seq
  124. increment by 15
  125. start with 320
  126. maxvalue 900
  127. nocache
  128. nocycle;
  129.  
  130. 5 (3 marks) Add new row to table WAREHOUSE by using this sequence for a city in Atlanta with unknown rating and division 30. You will assume today’s date as a foundation date. The date is to be entered automatically, meaning you cannot enter a specific date.
  131.  
  132. insert into warehouse (warehouse_id, city, division_id)
  133. values (whse_id_seq.nextval, 'Atlanta', 30);
  134.  
  135. 6 (5 marks) Create table CITIES from table LOCATIONS, but only for location numbers less than 2000 (do NOT create this table from scratch).  You will have 5 to 18 rows
  136.  
  137. create table cities as
  138. (select * from locations
  139. where location_id < 2000);
  140.  
  141. 7 (2 marks) Issue command to show the structure of the table CITIES
  142.  
  143. describe cities;
  144.  
  145. 8 (1 mark) Issue the SELECT command on cities and show result here.
  146.  
  147. Select * from cities;
  148.  
  149. LOCATION_ID | STREET_ADDRESS | POSTAL_CODE | CITY | STATE_PROVINCE | COUNTRY_ID
  150. 1000 1297 Via Cola di Rie 00989 Roma IT
  151. 1100 93091 Calle della Testa 10934 Venice IT
  152. 1200 2017 Shinjuku-ku 1689 Tokyo Tokyo Prefecture JP
  153. 1300 9450 Kamiya-cho 6823 Hiroshima JP
  154. 1400 2014 Jabberwocky Rd 26192 Southlake Texas US
  155. 1500 2011 Interiors Blvd 99236 South San Francisco California US
  156. 1600 2007 Zagora St 50090 South Brunswick New Jersey US
  157. 1700 2004 Charade Rd 98199 Seattle Washington US
  158. 1800 147 Spadina Ave M5V 2L7 Toronto Ontario CA
  159. 1900 6092 Boxwood St YSW 9T2 Whitehorse Yukon CA
  160.  
  161. 9 (5 marks) Create a View called WhsSec_Man_vu that will display for each Warehouse_id and Section_id, the City, Division and manager’s Last_name.
  162. Alias for Last_name should be LName and for Division should be Group.
  163.  
  164. create view whsSec_man_vu as
  165. select s.warehouse_id, s.section_id, w.city, w.division_id "Group", s.mgr_id Lname
  166. from warehouse w, section s
  167. where s.warehouse_id = w.warehouse_id;
  168.  
  169. 10 (1 mark) What is the SELECT command to issue if in 2 months I want to test if a view was actually created
  170.  
  171. select * from whssec_man_vu;
  172.  
  173. 11 (1 mark) If you want to modify the view what is the first line of the command
  174.  
  175. replace view whssec_man_vu;
  176.  
  177. The following is not on test 2, but you are responsible to have done self-study on the topic.
  178. 12 Issue a SET operator to show the rows that were in LOCATIONS but not in CITIES
  179.  
  180. select * from locations minus select * from cities;
  181.  
  182. Using the following diagram as a hint and not a perfect representation.
  183. Answer 13, 14, 15 and 16
  184.  
  185.  
  186.  
  187.  
  188. 13 All the rows in A and all the rows in B with no duplicates is the set operator called
  189. [Symbol]
  190.  
  191. Union
  192.  
  193. Select * from A union select * from B;
  194.  
  195. 14 All the rows in A and all the rows in B with duplicates [Symbol]
  196.  
  197. Union All
  198.  
  199. Select * from A union all select * from B;
  200.  
  201.  
  202. 15 The rows in common to BOTH A and B tables [Symbol]
  203.  
  204. Intersect
  205.  
  206. Select * from A intersect select * from B;
  207.  
  208.  
  209. 16 Rows that are in A but not in B would use the word [Symbol]
  210.  
  211. Minus
  212.  
  213. Select * from A minus select * from B;
Add Comment
Please, Sign In to add comment