Script - HR Objects and Data
Script - HR Objects and Data
Script - HR Objects and Data
HR Objects and Data For Live SQL View All Scripts Login to Run Script
Description This script will create a the HR Sample Schema objects and data in your
local schema. If you want just query-only, you can instead use the HR
sample schema by referencing hr.regions, etc. To drop the objects once
created, you can run "Drop HR Sample Schema" -
https://livesql.oracle.com/apex/livesql/file/content_GWKN7QJBHHC8F1RJTE
Please note that this schema was initially created in 2000 and last updated
March 19, 2015 so the constructs are not necessarily what we would
recommend today.
Contributor Oracle
Statement Copyright
1 begin
dbms_output.put_line('Copyright (c) 2018, Oracle and/or its affiliates. All rights re
dbms_output.put_line('Permission is hereby granted, free of charge, to any person obta
dbms_output.put_line('a copy of this software and associated documentation files (the
dbms_output.put_line('"Software"), to deal in the Software without restriction, includ
dbms_output.put_line('without limitation the rights to use, copy, modify, merge, publi
dbms_output.put_line('distribute, sublicense, and/or sell copies of the Software, and
dbms_output.put_line('permit persons to whom the Software is furnished to do so, subje
dbms_output.put_line('the following conditions: ');
dbms_output.put_line(' ');
dbms_output.put_line('The above copyright notice and this permission notice shall be
dbms_output.put_line('included in all copies or substantial portions of the Software.
dbms_output.put_line(' ');
dbms_output.put_line('THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
dbms_output.put_line('EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
dbms_output.put_line('MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ');
dbms_output.put_line('NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLD
dbms_output.put_line('LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
dbms_output.put_line('OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE
dbms_output.put_line('WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
end;
Statement processed.
Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Statement
HR.LOCATIONS table has a foreign key to
REGIONS table holds region information for
2 locations
this table.
Table created.
Statement
OE.CUSTOMERS table and HR.LOCATIONS
COUNTRIES table holds country information for
3 customers and company locations.
have a foreign key to this table.
Table created.
Statement
HR.DEPARTMENTS has a foreign key to
LOCATIONS table holds address information for
4 company departments.
this table.
Table created.
Statement
Starts with 3300
Useful for any subsequent addition of rows to LOCATIONS table
5
CREATE SEQUENCE locations_seq
START WITH 3300
INCREMENT BY 100
MAXVALUE 9900
NOCACHE
NOCYCLE
Sequence created.
Statement
HR.EMPLOYEES and HR.JOB_HISTORY
DEPARTMENTS table holds company department
6 information.
have a foreign key to this table.
Table created.
Statement
Starts with 280
Useful for any subsequent addition of rows to DEPARTMENTS table
7
CREATE SEQUENCE departments_seq
START WITH 280
INCREMENT BY 10
MAXVALUE 9990
NOCACHE
NOCYCLE
Sequence created.
Statement JOBS table holds the different names of job roles HR.EMPLOYEES has a foreign key to this
8 within the company. table.
Table created.
Statement
HR.EMPLOYEES has a self referencing
EMPLOYEES table holds the employee personnel
9 information for the company.
foreign key to this table.
Table created.
Statement
ALTER TABLE departments The foreign key can now be created to
10 ADD (CONSTRAINT dept_mgr_fk the EMPLOYEES table.
FOREIGN KEY (manager_id)
REFERENCES employees (employee_id)
)
Table altered.
Statement
Starts with 207
Useful for any subsequent addition of rows to employees table.
11
CREATE SEQUENCE employees_seq
START WITH 207
INCREMENT BY 1
NOCACHE
NOCYCLE
Sequence created.
Statement
HR.JOBS, HR_DEPARTMENTS, and
JOB_HISTORY table holds the history of jobs that
12 employees have held in the past.
HR.EMPLOYEES have a foreign key to this
table.
Table created.
Statement
CREATE OR REPLACE VIEW emp_details_view EMP_DETAILS_VIEW joins the employees,
13 (employee_id, jobs, departments, jobs, countries, and
locations table to provide details about
job_id,
employees.
manager_id,
department_id,
location_id,
country_id,
first_name,
last_name,
salary,
commission_pct,
department_name,
job_title,
city,
state_province,
country_name,
region_name)
AS SELECT
e.employee_id,
e.job_id,
e.manager_id,
e.department_id,
d.location_id,
l.country_id,
e.first_name,
e.last_name,
e.salary,
e.commission_pct,
d.department_name,
j.job_title,
l.city,
l.state_province,
c.country_name,
r.region_name
FROM
employees e,
departments d,
jobs j,
locations l,
countries c,
regions r
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND l.country_id = c.country_id
AND c.region_id = r.region_id
AND j.job_id = e.job_id
WITH READ ONLY
View created.
Statement processed.
Statement processed.
Statement processed.
Statement
ALTER TABLE departments disable integrity constraint to EMPLOYEES
17 DISABLE CONSTRAINT dept_mgr_fk to load data
Table altered.
Statement processed.
Statement processed.
Statement
Broken out due to 10k limit per statement
insert data into the EMPLOYEES table 2
21
begin
INSERT INTO employees VALUES
( 120
, 'Matthew'
, 'Weiss'
, 'MWEISS'
, '650.123.1234'
, TO_DATE('18-07-2004', 'dd-MM-yyyy')
, 'ST_MAN'
, 8000
, NULL
, 100
, 50
);
Statement processed.
Statement
Broken out due to 10k limit per statement
insert data into the EMPLOYEES table 3
22
begin
INSERT INTO employees VALUES
( 130
, 'Mozhe'
, 'Atkinson'
, 'MATKINSO'
, '650.124.6234'
, TO_DATE('30-10-2005', 'dd-MM-yyyy')
, 'ST_CLERK'
, 2800
, NULL
, 121
, 50
);
Statement processed.
Statement processed.
Statement
Broken out due to 10k limit per statement
insert data into the EMPLOYEES table 5
24
begin
INSERT INTO employees VALUES
( 170
, 'Tayler'
, 'Fox'
, 'TFOX'
, '011.44.1343.729268'
, TO_DATE('24-01-2006', 'dd-MM-yyyy')
, 'SA_REP'
, 9600
, .20
, 148
, 80
);
Statement processed.
Statement
Broken out due to 10k limit per statement
insert data into the EMPLOYEES table 6
25
begin
INSERT INTO employees VALUES
( 190
, 'Timothy'
, 'Gates'
, 'TGATES'
, '650.505.3876'
, TO_DATE('11-07-2006', 'dd-MM-yyyy')
, 'SH_CLERK'
, 2900
, NULL
, 122
, 50
);
Statement processed.
Statement
ALTER TABLE departments enable integrity constraint to
27 ENABLE CONSTRAINT dept_mgr_fk DEPARTMENTS
Table altered.
Statement
CREATE OR REPLACE PROCEDURE secure_dml procedure to limit edit of EMPLOYEES to
28 IS normal office hours
BEGIN
IF TO_CHAR (SYSDATE, 'HH24:MI') NOT BETWEEN '08:00
OR TO_CHAR (SYSDATE, 'DY') IN ('SAT', 'SUN')
RAISE_APPLICATION_ERROR (-20205,
'You may only make changes during no
END IF;
END secure_dml;
Procedure created.
Statement
CREATE OR REPLACE TRIGGER secure_employees trigger on EMPLOYEES to invoke the
29 BEFORE INSERT OR UPDATE OR DELETE ON employees SECURE_DML procedure
BEGIN
secure_dml;
END secure_employees;
Trigger created.
Statement
ALTER TRIGGER secure_employees DISABLE disable SECURE_EMPLOYEES trigger so
30 data can be used at any time (given that
this is a sample schema)
Trigger altered.
Statement
CREATE OR REPLACE PROCEDURE add_job_history procedure to add a row to the
31 ( p_emp_id job_history.employee_id%type JOB_HISTORY table
, p_start_date job_history.start_date%type
, p_end_date job_history.end_date%type
, p_job_id job_history.job_id%type
, p_department_id job_history.department_id%typ
)
IS
BEGIN
INSERT INTO job_history (employee_id, start_date,
job_id, department_id)
VALUES(p_emp_id, p_start_date, p_end_date, p_job
END add_job_history;
Procedure created.
Statement
CREATE OR REPLACE TRIGGER update_job_history row trigger to call the ADD_JOB_HISTORY
32 AFTER UPDATE OF job_id, department_id ON employees procedure when data is updated in the
job_id or department_id columns in the
FOR EACH ROW
EMPLOYEES table
BEGIN
add_job_history(:old.employee_id, :old.hire_date,
:old.job_id, :old.department_id);
END;
Trigger created.
Statement processed.
Statement
COMMENT ON COLUMN regions.region_id
34 IS 'Primary key of regions table.'
Statement processed.
Statement
COMMENT ON COLUMN regions.region_name
IS 'Names of regions. Locations are in the countries of these regions.'
35
Statement processed.
Statement
COMMENT ON TABLE locations
36 IS 'Locations table that contains specific address of a specific office, warehouse, and/o
Statement processed.
Statement
COMMENT ON COLUMN locations.location_id
37 IS 'Primary key of locations table'
Statement processed.
Statement
COMMENT ON COLUMN locations.street_address
38 IS 'Street address of an office, warehouse, or production site of a company. Contains bui
Statement processed.
Statement
COMMENT ON COLUMN locations.postal_code
39 IS 'Postal code of the location of an office, warehouse, or production site of a company
Statement processed.
Statement
COMMENT ON COLUMN locations.city
40 IS 'A not null column that shows city where an office, warehouse, or production site of a
Statement processed.
Statement
COMMENT ON COLUMN locations.state_province
41 IS 'State or Province where an office, warehouse, or production site of a company is loca
Statement processed.
Statement
COMMENT ON COLUMN locations.country_id
42 IS 'Country where an office, warehouse, or production site of a company is located. Forei
Statement processed.
Statement
Statement processed.
Statement
COMMENT ON COLUMN departments.department_id
44 IS 'Primary key column of departments table.'
Statement processed.
Statement
COMMENT ON COLUMN departments.department_name
45 IS 'A not null column that shows name of a department. Administration, Marketing, Purchas
Statement processed.
Statement
COMMENT ON COLUMN departments.manager_id
46 IS 'Manager_id of a department. Foreign key to employee_id column of employees table. The
Statement processed.
Statement
COMMENT ON COLUMN departments.location_id
47 IS 'Location id where a department is located. Foreign key to location_id column of locat
Statement processed.
Statement
COMMENT ON TABLE job_history
48 IS 'Table that stores job history of the employees. If an employee changes departments w
Statement processed.
Statement
COMMENT ON COLUMN job_history.employee_id
49 IS 'A not null column in the complex primary key employee_id+start_date. Foreign key to e
Statement processed.
Statement
COMMENT ON COLUMN job_history.start_date
50 IS 'A not null column in the complex primary key employee_id+start_date. Must be less th
Statement processed.
Statement
COMMENT ON COLUMN job_history.end_date
51 IS 'Last day of the employee in this job role. A not null column. Must be greater than th
Statement processed.
Statement
COMMENT ON COLUMN job_history.job_id
52 IS 'Job role in which the employee worked in the past; foreign key to job_id column in th
Statement processed.
Statement
COMMENT ON COLUMN job_history.department_id
53 IS 'Department id in which the employee worked in the past; foreign key to deparment_id c
Statement processed.
Statement
COMMENT ON TABLE countries
54 IS 'country table. Contains 25 rows. References with locations table.'
Statement processed.
Statement
COMMENT ON COLUMN countries.country_id
55 IS 'Primary key of countries table.'
Statement processed.
Statement
COMMENT ON COLUMN countries.country_name
56 IS 'Country name'
Statement processed.
Statement
COMMENT ON COLUMN countries.region_id
57 IS 'Region ID for the country. Foreign key to region_id column in the departments table.
Statement processed.
Statement
COMMENT ON TABLE jobs
58 IS 'jobs table with job titles and salary ranges. Contains 19 rows. References with emplo
Statement processed.
Statement
COMMENT ON COLUMN jobs.job_id
59 IS 'Primary key of jobs table.'
Statement processed.
Statement
COMMENT ON COLUMN jobs.job_title
60 IS 'A not null column that shows job title, e.g. AD_VP, FI_ACCOUNTANT'
Statement processed.
Statement
COMMENT ON COLUMN jobs.min_salary
61 IS 'Minimum salary for a job title.'
Statement processed.
Statement
COMMENT ON COLUMN jobs.max_salary
62 IS 'Maximum salary for a job title'
Statement processed.
Additional Information