Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1.  
  2. DROP TABLE departments CASCADE CONSTRAINTS;
  3.  
  4. DROP TABLE employees CASCADE CONSTRAINTS;
  5.  
  6. DROP TABLE jobs CASCADE CONSTRAINTS;
  7.  
  8. DROP TABLE job_history CASCADE CONSTRAINTS;
  9.  
  10. DROP TABLE locations CASCADE CONSTRAINTS;
  11.  
  12. DROP TABLE countries CASCADE CONSTRAINTS;
  13.  
  14. DROP TABLE regions CASCADE CONSTRAINTS;
  15.  
  16.  
  17. DROP TYPE department_t;
  18.  
  19. DROP TYPE employee_t;
  20.  
  21. DROP TYPE jobs_t;
  22.  
  23. DROP TYPE job_history_t;
  24.  
  25. DROP TYPE zone_t;
  26.  
  27. DROP TYPE region_t;
  28.  
  29. DROP TYPE location_t;
  30.  
  31. DROP TYPE country_t;
  32.  
  33. DROP TYPE department_employees;
  34.  
  35. DROP TYPE department_jobs;
  36.  
  37.  
  38. -- Departments --
  39. CREATE OR REPLACE TYPE department_employees AS VARRAY(107) of NUMBER(5);
  40. CREATE OR REPLACE TYPE department_jobs AS VARRAY(19) of NUMBER(2);
  41.  
  42. CREATE OR REPLACE TYPE department_t AS OBJECT(
  43. department_id NUMBER(4,0),
  44. department_name VARCHAR2(30),
  45. location_id REF location_t,
  46. manager_id REF employee_t,
  47. dep_employees department_employees,
  48. dep_jobs department_jobs,
  49. map member function get_ID return NUMBER,
  50. member function get_total_jobs return NUMBER,
  51. member function get_total_employees return NUMBER,
  52. member function get_highest_salary return NUMBER
  53. );
  54.  
  55. CREATE OR REPLACE TYPE BODY department_t AS
  56. map member function get_ID return NUMBER is
  57. begin
  58. return department_id;
  59. end get_ID;
  60. member function get_total_jobs return NUMBER is
  61. total number(3) := 0;
  62. begin
  63. for m in 1..dep_jobs.count loop
  64. if not(dep_jobs(m) is NULL) then
  65. total := total + dep_jobs(m);
  66. end if;
  67. end loop;
  68. return total;
  69. end get_total_jobs;
  70. member function get_total_employees return NUMBER is
  71. amount NUMBER(3) := 0;
  72. BEGIN
  73. FOR i in 1..dep_employees.count
  74. loop
  75. if(dep_employees(i) is not null)
  76. then amount := amount + 1;
  77. end if;
  78. end loop;
  79. end get_total_employees;
  80. member function get_highest_salary return NUMBER is
  81. employee NUMBER(3) := 0;
  82. BEGIN
  83. FOR i in 1..dep_employees.count
  84. loop
  85. if(dep_employees(i)>dep_employees(employee))
  86. then employee := i;
  87. end if;
  88. end loop;
  89. employee := employee + 100 - 1;
  90. END get_highest_salary;
  91. end;
  92.  
  93. -- Employees --
  94. CREATE OR REPLACE TYPE employee_t AS OBJECT(
  95. employee_id NUMBER(6,0),
  96. first_name VARCHAR2(20),
  97. last_name VARCHAR2(25),
  98. email VARCHAR2(25),
  99. phone_number VARCHAR2(20),
  100. hire_date DATE,
  101. salary NUMBER(8,2),
  102. commission_pct NUMBER(2,2),
  103. department_id ref department_t,
  104. job_id ref jobs_t,
  105. manager_id ref employee_t
  106. );
  107.  
  108. -- Jobs --
  109. CREATE OR REPLACE TYPE jobs_t AS OBJECT(
  110. job_id VARCHAR2(10),
  111. job_title VARCHAR2(35),
  112. min_salary NUMBER(6,0),
  113. max_salary NUMBER(6,0)
  114. );
  115.  
  116. -- Job History --
  117. CREATE OR REPLACE TYPE job_history_t AS OBJECT(
  118. start_date DATE,
  119. end_date DATE,
  120. job_id REF jobs_t,
  121. employee_id REF employee_t,
  122. department_id REF department_t
  123. );
  124.  
  125. -- Zone --
  126. CREATE OR REPLACE TYPE zone_t AS OBJECT(
  127. id NUMBER(4,0),
  128. name VARCHAR2(40),
  129. map member function get_id return number,
  130. member function get_name return varchar2
  131. ) not final;
  132.  
  133. CREATE OR REPLACE TYPE BODY zone_t AS
  134. map member function get_id return number is
  135. begin
  136. return id;
  137. end get_id;
  138. member function get_name return varchar2 is
  139. begin
  140. return name;
  141. end get_name;
  142. end;
  143.  
  144.  
  145. -- Regions --
  146. CREATE OR REPLACE TYPE region_t UNDER zone_t(
  147. overriding member function get_name return varchar2
  148. );
  149.  
  150. CREATE OR REPLACE TYPE BODY region_t AS
  151. overriding member function get_name return varchar2 is
  152. begin
  153. return 'Region: ' || name;
  154. end get_name;
  155. end;
  156.  
  157. -- Countries --
  158. CREATE OR REPLACE TYPE country_t UNDER zone_t(
  159. region ref region_t,
  160. overriding member function get_name return varchar2
  161. );
  162.  
  163. CREATE OR REPLACE TYPE BODY country_t AS
  164. overriding member function get_name return varchar2 is
  165. begin
  166. return 'Country: ' || name;
  167. end get_name;
  168. end;
  169.  
  170. -- Locations --
  171. CREATE OR REPLACE TYPE location_t UNDER zone_t (
  172. postal_code VARCHAR2(12),
  173. city VARCHAR2(30),
  174. state_province VARCHAR2(25),
  175. country ref country_t,
  176. overriding member function get_name return varchar2
  177. );
  178.  
  179. CREATE OR REPLACE TYPE BODY location_t AS
  180. overriding member function get_name return varchar2 is
  181. begin
  182. return 'Location: ' || name;
  183. end get_name;
  184. end;
  185.  
  186.  
  187. --------------------------------------------------------
  188. ------------------ CREATE TABLES -------------------
  189. --------------------------------------------------------
  190.  
  191. CREATE TABLE departments OF department_t;
  192. CREATE TABLE employees OF employee_t;
  193. CREATE TABLE jobs OF jobs_t;
  194. CREATE TABLE job_history OF job_history_t;
  195. CREATE TABLE locations OF location_t;
  196. CREATE TABLE countries OF country_t;
  197. CREATE TABLE regions OF region_t;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement