PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Resources/hr_cre.sql

https://bitbucket.org/kevdashdev/ited233
SQL | 337 lines | 288 code | 49 blank | 0 comment | 8 complexity | 6d56e53f8af432ce13032c6685a754a1 MD5 | raw file
  1. Rem
  2. Rem $Header: hr_cre.sql 29-aug-2002.11:44:03 hyeh Exp $
  3. Rem
  4. Rem hr_cre.sql
  5. Rem
  6. Rem Copyright (c) 2001, 2002, Oracle Corporation. All rights reserved.
  7. Rem
  8. Rem NAME
  9. Rem hr_cre.sql - Create data objects for HR schema
  10. Rem
  11. Rem DESCRIPTION
  12. Rem This script creates six tables, associated constraints
  13. Rem and indexes in the human resources (HR) schema.
  14. Rem
  15. Rem NOTES
  16. Rem
  17. Rem CREATED by Nancy Greenberg, Nagavalli Pataballa - 06/01/00
  18. Rem
  19. Rem MODIFIED (MM/DD/YY)
  20. Rem hyeh 08/29/02 - hyeh_mv_comschema_to_rdbms
  21. Rem ahunold 09/14/00 - Added emp_details_view
  22. Rem ahunold 02/20/01 - New header
  23. Rem vpatabal 03/02/01 - Added regions table, modified regions
  24. Rem column in countries table to NUMBER.
  25. Rem Added foreign key from countries table
  26. Rem to regions table on region_id.
  27. Rem Removed currency name, currency symbol
  28. Rem columns from the countries table.
  29. Rem Removed dn columns from employees and
  30. Rem departments tables.
  31. Rem Added sequences.
  32. Rem Removed not null constraint from
  33. Rem salary column of the employees table.
  34. SET FEEDBACK 1
  35. SET NUMWIDTH 10
  36. SET LINESIZE 80
  37. SET TRIMSPOOL ON
  38. SET TAB OFF
  39. SET PAGESIZE 100
  40. SET ECHO OFF
  41. REM ********************************************************************
  42. REM Create the REGIONS table to hold region information for locations
  43. REM HR.LOCATIONS table has a foreign key to this table.
  44. Prompt ****** Creating REGIONS table ....
  45. CREATE TABLE regions
  46. ( region_id NUMBER
  47. CONSTRAINT region_id_nn NOT NULL
  48. , region_name VARCHAR2(25)
  49. );
  50. CREATE UNIQUE INDEX reg_id_pk
  51. ON regions (region_id);
  52. ALTER TABLE regions
  53. ADD ( CONSTRAINT reg_id_pk
  54. PRIMARY KEY (region_id)
  55. ) ;
  56. REM ********************************************************************
  57. REM Create the COUNTRIES table to hold country information for customers
  58. REM and company locations.
  59. REM OE.CUSTOMERS table and HR.LOCATIONS have a foreign key to this table.
  60. Prompt ****** Creating COUNTRIES table ....
  61. CREATE TABLE countries
  62. ( country_id CHAR(2)
  63. CONSTRAINT country_id_nn NOT NULL
  64. , country_name VARCHAR2(40)
  65. , region_id NUMBER
  66. , CONSTRAINT country_c_id_pk
  67. PRIMARY KEY (country_id)
  68. )
  69. ORGANIZATION INDEX;
  70. ALTER TABLE countries
  71. ADD ( CONSTRAINT countr_reg_fk
  72. FOREIGN KEY (region_id)
  73. REFERENCES regions(region_id)
  74. ) ;
  75. REM ********************************************************************
  76. REM Create the LOCATIONS table to hold address information for company departments.
  77. REM HR.DEPARTMENTS has a foreign key to this table.
  78. Prompt ****** Creating LOCATIONS table ....
  79. CREATE TABLE locations
  80. ( location_id NUMBER(4)
  81. , street_address VARCHAR2(40)
  82. , postal_code VARCHAR2(12)
  83. , city VARCHAR2(30)
  84. CONSTRAINT loc_city_nn NOT NULL
  85. , state_province VARCHAR2(25)
  86. , country_id CHAR(2)
  87. ) ;
  88. CREATE UNIQUE INDEX loc_id_pk
  89. ON locations (location_id) ;
  90. ALTER TABLE locations
  91. ADD ( CONSTRAINT loc_id_pk
  92. PRIMARY KEY (location_id)
  93. , CONSTRAINT loc_c_id_fk
  94. FOREIGN KEY (country_id)
  95. REFERENCES countries(country_id)
  96. ) ;
  97. Rem Useful for any subsequent addition of rows to locations table
  98. Rem Starts with 3300
  99. CREATE SEQUENCE locations_seq
  100. START WITH 3300
  101. INCREMENT BY 100
  102. MAXVALUE 9900
  103. NOCACHE
  104. NOCYCLE;
  105. REM ********************************************************************
  106. REM Create the DEPARTMENTS table to hold company department information.
  107. REM HR.EMPLOYEES and HR.JOB_HISTORY have a foreign key to this table.
  108. Prompt ****** Creating DEPARTMENTS table ....
  109. CREATE TABLE departments
  110. ( department_id NUMBER(4)
  111. , department_name VARCHAR2(30)
  112. CONSTRAINT dept_name_nn NOT NULL
  113. , manager_id NUMBER(6)
  114. , location_id NUMBER(4)
  115. ) ;
  116. CREATE UNIQUE INDEX dept_id_pk
  117. ON departments (department_id) ;
  118. ALTER TABLE departments
  119. ADD ( CONSTRAINT dept_id_pk
  120. PRIMARY KEY (department_id)
  121. , CONSTRAINT dept_loc_fk
  122. FOREIGN KEY (location_id)
  123. REFERENCES locations (location_id)
  124. ) ;
  125. Rem Useful for any subsequent addition of rows to departments table
  126. Rem Starts with 280
  127. CREATE SEQUENCE departments_seq
  128. START WITH 280
  129. INCREMENT BY 10
  130. MAXVALUE 9990
  131. NOCACHE
  132. NOCYCLE;
  133. REM ********************************************************************
  134. REM Create the JOBS table to hold the different names of job roles within the company.
  135. REM HR.EMPLOYEES has a foreign key to this table.
  136. Prompt ****** Creating JOBS table ....
  137. CREATE TABLE jobs
  138. ( job_id VARCHAR2(10)
  139. , job_title VARCHAR2(35)
  140. CONSTRAINT job_title_nn NOT NULL
  141. , min_salary NUMBER(6)
  142. , max_salary NUMBER(6)
  143. ) ;
  144. CREATE UNIQUE INDEX job_id_pk
  145. ON jobs (job_id) ;
  146. ALTER TABLE jobs
  147. ADD ( CONSTRAINT job_id_pk
  148. PRIMARY KEY(job_id)
  149. ) ;
  150. REM ********************************************************************
  151. REM Create the EMPLOYEES table to hold the employee personnel
  152. REM information for the company.
  153. REM HR.EMPLOYEES has a self referencing foreign key to this table.
  154. Prompt ****** Creating EMPLOYEES table ....
  155. CREATE TABLE employees
  156. ( employee_id NUMBER(6)
  157. , first_name VARCHAR2(20)
  158. , last_name VARCHAR2(25)
  159. CONSTRAINT emp_last_name_nn NOT NULL
  160. , email VARCHAR2(25)
  161. CONSTRAINT emp_email_nn NOT NULL
  162. , phone_number VARCHAR2(20)
  163. , hire_date DATE
  164. CONSTRAINT emp_hire_date_nn NOT NULL
  165. , job_id VARCHAR2(10)
  166. CONSTRAINT emp_job_nn NOT NULL
  167. , salary NUMBER(8,2)
  168. , commission_pct NUMBER(2,2)
  169. , manager_id NUMBER(6)
  170. , department_id NUMBER(4)
  171. , CONSTRAINT emp_salary_min
  172. CHECK (salary > 0)
  173. , CONSTRAINT emp_email_uk
  174. UNIQUE (email)
  175. ) ;
  176. CREATE UNIQUE INDEX emp_emp_id_pk
  177. ON employees (employee_id) ;
  178. ALTER TABLE employees
  179. ADD ( CONSTRAINT emp_emp_id_pk
  180. PRIMARY KEY (employee_id)
  181. , CONSTRAINT emp_dept_fk
  182. FOREIGN KEY (department_id)
  183. REFERENCES departments
  184. , CONSTRAINT emp_job_fk
  185. FOREIGN KEY (job_id)
  186. REFERENCES jobs (job_id)
  187. , CONSTRAINT emp_manager_fk
  188. FOREIGN KEY (manager_id)
  189. REFERENCES employees
  190. ) ;
  191. ALTER TABLE departments
  192. ADD ( CONSTRAINT dept_mgr_fk
  193. FOREIGN KEY (manager_id)
  194. REFERENCES employees (employee_id)
  195. ) ;
  196. Rem Useful for any subsequent addition of rows to employees table
  197. Rem Starts with 207
  198. CREATE SEQUENCE employees_seq
  199. START WITH 207
  200. INCREMENT BY 1
  201. NOCACHE
  202. NOCYCLE;
  203. REM ********************************************************************
  204. REM Create the JOB_HISTORY table to hold the history of jobs that
  205. REM employees have held in the past.
  206. REM HR.JOBS, HR_DEPARTMENTS, and HR.EMPLOYEES have a foreign key to this table.
  207. Prompt ****** Creating JOB_HISTORY table ....
  208. CREATE TABLE job_history
  209. ( employee_id NUMBER(6)
  210. CONSTRAINT jhist_employee_nn NOT NULL
  211. , start_date DATE
  212. CONSTRAINT jhist_start_date_nn NOT NULL
  213. , end_date DATE
  214. CONSTRAINT jhist_end_date_nn NOT NULL
  215. , job_id VARCHAR2(10)
  216. CONSTRAINT jhist_job_nn NOT NULL
  217. , department_id NUMBER(4)
  218. , CONSTRAINT jhist_date_interval
  219. CHECK (end_date > start_date)
  220. ) ;
  221. CREATE UNIQUE INDEX jhist_emp_id_st_date_pk
  222. ON job_history (employee_id, start_date) ;
  223. ALTER TABLE job_history
  224. ADD ( CONSTRAINT jhist_emp_id_st_date_pk
  225. PRIMARY KEY (employee_id, start_date)
  226. , CONSTRAINT jhist_job_fk
  227. FOREIGN KEY (job_id)
  228. REFERENCES jobs
  229. , CONSTRAINT jhist_emp_fk
  230. FOREIGN KEY (employee_id)
  231. REFERENCES employees
  232. , CONSTRAINT jhist_dept_fk
  233. FOREIGN KEY (department_id)
  234. REFERENCES departments
  235. ) ;
  236. REM ********************************************************************
  237. REM Create the EMP_DETAILS_VIEW that joins the employees, jobs,
  238. REM departments, jobs, countries, and locations table to provide details
  239. REM about employees.
  240. Prompt ****** Creating EMP_DETAILS_VIEW view ...
  241. CREATE OR REPLACE VIEW emp_details_view
  242. (employee_id,
  243. job_id,
  244. manager_id,
  245. department_id,
  246. location_id,
  247. country_id,
  248. first_name,
  249. last_name,
  250. salary,
  251. commission_pct,
  252. department_name,
  253. job_title,
  254. city,
  255. state_province,
  256. country_name,
  257. region_name)
  258. AS SELECT
  259. e.employee_id,
  260. e.job_id,
  261. e.manager_id,
  262. e.department_id,
  263. d.location_id,
  264. l.country_id,
  265. e.first_name,
  266. e.last_name,
  267. e.salary,
  268. e.commission_pct,
  269. d.department_name,
  270. j.job_title,
  271. l.city,
  272. l.state_province,
  273. c.country_name,
  274. r.region_name
  275. FROM
  276. employees e,
  277. departments d,
  278. jobs j,
  279. locations l,
  280. countries c,
  281. regions r
  282. WHERE e.department_id = d.department_id
  283. AND d.location_id = l.location_id
  284. AND l.country_id = c.country_id
  285. AND c.region_id = r.region_id
  286. AND j.job_id = e.job_id
  287. WITH READ ONLY;
  288. COMMIT;