AS-IS 쪽의 HR 계정의 모든 데이터를 TO-BE 쪽에 HR_DW 로 이행해야함
- 테이블
- 인덱스
- 제약
- sequence
- synonym
- 프로시져, 함수와 같은 PL/SQL 오브젝트
- 기타 (IOT, MVIEW)
SYS @ ora19dw > create user hr2 identified by hr2;
사용자가 생성되었습니다.
SYS @ ora19dw > grant dba to hr2;
권한이 부여되었습니다.
SYS @ ora19dw >
as-is 는 하얀색, to-be 는 파란색으로 색깔을 변경해놓기
as-is 쪽에 er 다이어그램을 추출합니다.
ㄴ Tools - ER viewer 클릭
💡 erwin 프로그램과 db랑 연동해서도 실습 er 다이어그램을 추출해봐야함
as-is 쪽의 테이블 생성 스크립트를 추출합니다.
CREATE TABLE HR.COUNTRIES
(
COUNTRY_ID CHAR(2) NOT NULL,
COUNTRY_NAME VARCHAR2(40),
REGION_ID NUMBER,
CONSTRAINT COUNTRY_C_ID_PK PRIMARY KEY (COUNTRY_ID)
)
ORGANIZATION INDEX
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOMAPPING;
COMMENT ON TABLE HR.COUNTRIES IS 'country table. Contains 25 rows. References with locations table.';
COMMENT ON COLUMN HR.COUNTRIES.COUNTRY_ID IS 'Primary key of countries table.';
COMMENT ON COLUMN HR.COUNTRIES.COUNTRY_NAME IS 'Country name';
COMMENT ON COLUMN HR.COUNTRIES.REGION_ID IS 'Region ID for the country. Foreign key to region_id column in the departments table.';
CREATE TABLE HR.DEPARTMENTS
(
DEPARTMENT_ID NUMBER(4) NOT NULL,
DEPARTMENT_NAME VARCHAR2(30) NOT NULL,
MANAGER_ID NUMBER(6),
LOCATION_ID NUMBER(4)
)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR.DEPARTMENTS IS 'Departments table that shows details of departments where employees
work. Contains 27 rows; references with locations, employees, and job_history tables.';
COMMENT ON COLUMN HR.DEPARTMENTS.DEPARTMENT_ID IS 'Primary key column of departments table.';
COMMENT ON COLUMN HR.DEPARTMENTS.DEPARTMENT_NAME IS 'A not null column that shows name of a department. Administration,
Marketing, Purchasing, Human Resources, Shipping, IT, Executive, Public
Relations, Sales, Finance, and Accounting. ';
COMMENT ON COLUMN HR.DEPARTMENTS.MANAGER_ID IS 'Manager_id of a department. Foreign key to employee_id column of employees table. The manager_id column of the employee table references this column.';
COMMENT ON COLUMN HR.DEPARTMENTS.LOCATION_ID IS 'Location id where a department is located. Foreign key to location_id column of locations table.';
CREATE TABLE HR.EMPLOYEES
(
EMPLOYEE_ID NUMBER(6) NOT NULL,
FIRST_NAME VARCHAR2(20),
LAST_NAME VARCHAR2(25) NOT NULL,
EMAIL VARCHAR2(25) NOT NULL,
PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),
MANAGER_ID NUMBER(6),
DEPARTMENT_ID NUMBER(4)
)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR.EMPLOYEES IS 'employees table. Contains 107 rows. References with departments,
jobs, job_history tables. Contains a self reference.';
COMMENT ON COLUMN HR.EMPLOYEES.EMPLOYEE_ID IS 'Primary key of employees table.';
COMMENT ON COLUMN HR.EMPLOYEES.FIRST_NAME IS 'First name of the employee. A not null column.';
COMMENT ON COLUMN HR.EMPLOYEES.LAST_NAME IS 'Last name of the employee. A not null column.';
COMMENT ON COLUMN HR.EMPLOYEES.EMAIL IS 'Email id of the employee';
COMMENT ON COLUMN HR.EMPLOYEES.PHONE_NUMBER IS 'Phone number of the employee; includes country code and area code';
COMMENT ON COLUMN HR.EMPLOYEES.HIRE_DATE IS 'Date when the employee started on this job. A not null column.';
COMMENT ON COLUMN HR.EMPLOYEES.JOB_ID IS 'Current job of the employee; foreign key to job_id column of the
jobs table. A not null column.';
COMMENT ON COLUMN HR.EMPLOYEES.SALARY IS 'Monthly salary of the employee. Must be greater
than zero (enforced by constraint emp_salary_min)';
COMMENT ON COLUMN HR.EMPLOYEES.COMMISSION_PCT IS 'Commission percentage of the employee; Only employees in sales
department elgible for commission percentage';
COMMENT ON COLUMN HR.EMPLOYEES.MANAGER_ID IS 'Manager id of the employee; has same domain as manager_id in
departments table. Foreign key to employee_id column of employees table.
(useful for reflexive joins and CONNECT BY query)';
COMMENT ON COLUMN HR.EMPLOYEES.DEPARTMENT_ID IS 'Department id where employee works; foreign key to department_id
column of the departments table';
CREATE TABLE HR.JOBS
(
JOB_ID VARCHAR2(10) NOT NULL,
JOB_TITLE VARCHAR2(35) NOT NULL,
MIN_SALARY NUMBER(6),
MAX_SALARY NUMBER(6)
)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR.JOBS IS 'jobs table with job titles and salary ranges. Contains 19 rows.
References with employees and job_history table.';
COMMENT ON COLUMN HR.JOBS.JOB_ID IS 'Primary key of jobs table.';
COMMENT ON COLUMN HR.JOBS.JOB_TITLE IS 'A not null column that shows job title, e.g. AD_VP, FI_ACCOUNTANT';
COMMENT ON COLUMN HR.JOBS.MIN_SALARY IS 'Minimum salary for a job title.';
COMMENT ON COLUMN HR.JOBS.MAX_SALARY IS 'Maximum salary for a job title';
CREATE TABLE HR.JOB_HISTORY
(
EMPLOYEE_ID NUMBER(6) NOT NULL,
START_DATE DATE NOT NULL,
END_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
DEPARTMENT_ID NUMBER(4)
)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR.JOB_HISTORY IS 'Table that stores job history of the employees. If an employee
changes departments within the job or changes jobs within the department,
new rows get inserted into this table with old job information of the
employee. Contains a complex primary key: employee_id+start_date.
Contains 25 rows. References with jobs, employees, and departments tables.';
COMMENT ON COLUMN HR.JOB_HISTORY.EMPLOYEE_ID IS 'A not null column in the complex primary key employee_id+start_date.
Foreign key to employee_id column of the employee table';
COMMENT ON COLUMN HR.JOB_HISTORY.START_DATE IS 'A not null column in the complex primary key employee_id+start_date.
Must be less than the end_date of the job_history table. (enforced by
constraint jhist_date_interval)';
COMMENT ON COLUMN HR.JOB_HISTORY.END_DATE IS 'Last day of the employee in this job role. A not null column. Must be
greater than the start_date of the job_history table.
(enforced by constraint jhist_date_interval)';
COMMENT ON COLUMN HR.JOB_HISTORY.JOB_ID IS 'Job role in which the employee worked in the past; foreign key to
job_id column in the jobs table. A not null column.';
COMMENT ON COLUMN HR.JOB_HISTORY.DEPARTMENT_ID IS 'Department id in which the employee worked in the past; foreign key to deparment_id column in the departments table';
CREATE TABLE HR.LOCATIONS
(
LOCATION_ID NUMBER(4) NOT NULL,
STREET_ADDRESS VARCHAR2(40),
POSTAL_CODE VARCHAR2(12),
CITY VARCHAR2(30) NOT NULL,
STATE_PROVINCE VARCHAR2(25),
COUNTRY_ID CHAR(2)
)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR.LOCATIONS IS 'Locations table that contains specific address of a specific office,
warehouse, and/or production site of a company. Does not store addresses /
locations of customers. Contains 23 rows; references with the
departments and countries tables. ';
COMMENT ON COLUMN HR.LOCATIONS.LOCATION_ID IS 'Primary key of locations table';
COMMENT ON COLUMN HR.LOCATIONS.STREET_ADDRESS IS 'Street address of an office, warehouse, or production site of a company.
Contains building number and street name';
COMMENT ON COLUMN HR.LOCATIONS.POSTAL_CODE IS 'Postal code of the location of an office, warehouse, or production site
of a company. ';
COMMENT ON COLUMN HR.LOCATIONS.CITY IS 'A not null column that shows city where an office, warehouse, or
production site of a company is located. ';
COMMENT ON COLUMN HR.LOCATIONS.STATE_PROVINCE IS 'State or Province where an office, warehouse, or production site of a
company is located.';
COMMENT ON COLUMN HR.LOCATIONS.COUNTRY_ID IS 'Country where an office, warehouse, or production site of a company is
located. Foreign key to country_id column of the countries table.';
CREATE TABLE HR.REGIONS
(
REGION_ID NUMBER NOT NULL,
REGION_NAME VARCHAR2(25)
)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
CREATE UNIQUE INDEX HR.COUNTRY_C_ID_PK
ON HR.COUNTRIES (COUNTRY_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE UNIQUE INDEX HR.DEPT_ID_PK
ON HR.DEPARTMENTS (DEPARTMENT_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.DEPT_LOCATION_IX
ON HR.DEPARTMENTS (LOCATION_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.EMP_DEPARTMENT_IX
ON HR.EMPLOYEES (DEPARTMENT_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE UNIQUE INDEX HR.EMP_EMAIL_UK
ON HR.EMPLOYEES (EMAIL)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE UNIQUE INDEX HR.EMP_EMP_ID_PK
ON HR.EMPLOYEES (EMPLOYEE_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.EMP_JOB_IX
ON HR.EMPLOYEES (JOB_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.EMP_MANAGER_IX
ON HR.EMPLOYEES (MANAGER_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.EMP_NAME_IX
ON HR.EMPLOYEES (LAST_NAME,FIRST_NAME)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.JHIST_DEPARTMENT_IX
ON HR.JOB_HISTORY (DEPARTMENT_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.JHIST_EMPLOYEE_IX
ON HR.JOB_HISTORY (EMPLOYEE_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE UNIQUE INDEX HR.JHIST_EMP_ID_ST_DATE_PK
ON HR.JOB_HISTORY (EMPLOYEE_ID,START_DATE)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.JHIST_JOB_IX
ON HR.JOB_HISTORY (JOB_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE UNIQUE INDEX HR.JOB_ID_PK
ON HR.JOBS (JOB_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.LOC_CITY_IX
ON HR.LOCATIONS (CITY)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.LOC_COUNTRY_IX
ON HR.LOCATIONS (COUNTRY_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE UNIQUE INDEX HR.LOC_ID_PK
ON HR.LOCATIONS (LOCATION_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE INDEX HR.LOC_STATE_PROVINCE_IX
ON HR.LOCATIONS (STATE_PROVINCE)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
CREATE UNIQUE INDEX HR.REG_ID_PK
ON HR.REGIONS (REGION_ID)
TABLESPACE SYSAUX
STORAGE
(
INITIAL 64K
NEXT 1M
);
ALTER TABLE HR.COUNTRIES
ADD CONSTRAINT COUNTRY_ID_NN CHECK ("COUNTRY_ID" IS NOT NULL);
ALTER TABLE HR.DEPARTMENTS
ADD CONSTRAINT DEPT_NAME_NN CHECK ("DEPARTMENT_NAME" IS NOT NULL);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_EMAIL_NN CHECK ("EMAIL" IS NOT NULL);
ALTER TABLE HR.COUNTRIES
ADD CONSTRAINT COUNTRY_C_ID_PK PRIMARY KEY (COUNTRY_ID);
ALTER TABLE HR.DEPARTMENTS
ADD CONSTRAINT DEPT_ID_PK PRIMARY KEY (DEPARTMENT_ID);
ALTER TABLE HR.COUNTRIES
ADD CONSTRAINT COUNTR_REG_FK FOREIGN KEY (REGION_ID) REFERENCES HR.REGIONS (REGION_ID);
ALTER TABLE HR.DEPARTMENTS
ADD CONSTRAINT DEPT_LOC_FK FOREIGN KEY (LOCATION_ID) REFERENCES HR.LOCATIONS (LOCATION_ID);
ALTER TABLE HR.DEPARTMENTS
ADD CONSTRAINT DEPT_MGR_FK FOREIGN KEY (MANAGER_ID) REFERENCES HR.EMPLOYEES (EMPLOYEE_ID);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_DEPT_FK FOREIGN KEY (DEPARTMENT_ID) REFERENCES HR.DEPARTMENTS (DEPARTMENT_ID);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_EMAIL_UK UNIQUE (EMAIL);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_HIRE_DATE_NN CHECK ("HIRE_DATE" IS NOT NULL);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_JOB_NN CHECK ("JOB_ID" IS NOT NULL);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_LAST_NAME_NN CHECK ("LAST_NAME" IS NOT NULL);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_SALARY_MIN CHECK (salary > 0);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_DATE_INTERVAL CHECK (end_date > start_date);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_EMPLOYEE_NN CHECK ("EMPLOYEE_ID" IS NOT NULL);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_EMP_ID_PK PRIMARY KEY (EMPLOYEE_ID);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES HR.JOBS (JOB_ID);
ALTER TABLE HR.EMPLOYEES
ADD CONSTRAINT EMP_MANAGER_FK FOREIGN KEY (MANAGER_ID) REFERENCES HR.EMPLOYEES (EMPLOYEE_ID);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_DEPT_FK FOREIGN KEY (DEPARTMENT_ID) REFERENCES HR.DEPARTMENTS (DEPARTMENT_ID);
ALTER TABLE HR.JOBS
ADD CONSTRAINT JOB_TITLE_NN CHECK ("JOB_TITLE" IS NOT NULL);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_END_DATE_NN CHECK ("END_DATE" IS NOT NULL);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_JOB_NN CHECK ("JOB_ID" IS NOT NULL);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_START_DATE_NN CHECK ("START_DATE" IS NOT NULL);
ALTER TABLE HR.LOCATIONS
ADD CONSTRAINT LOC_CITY_NN CHECK ("CITY" IS NOT NULL);
ALTER TABLE HR.JOBS
ADD CONSTRAINT JOB_ID_PK PRIMARY KEY (JOB_ID);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_EMP_ID_ST_DATE_PK PRIMARY KEY (EMPLOYEE_ID,START_DATE);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_EMP_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES HR.EMPLOYEES (EMPLOYEE_ID);
ALTER TABLE HR.JOB_HISTORY
ADD CONSTRAINT JHIST_JOB_FK FOREIGN KEY (JOB_ID) REFERENCES HR.JOBS (JOB_ID);
ALTER TABLE HR.LOCATIONS
ADD CONSTRAINT LOC_C_ID_FK FOREIGN KEY (COUNTRY_ID) REFERENCES HR.COUNTRIES (COUNTRY_ID);
ALTER TABLE HR.REGIONS
ADD CONSTRAINT REGION_ID_NN CHECK ("REGION_ID" IS NOT NULL);
ALTER TABLE HR.LOCATIONS
ADD CONSTRAINT LOC_ID_PK PRIMARY KEY (LOCATION_ID);
ALTER TABLE HR.REGIONS
ADD CONSTRAINT REG_ID_PK PRIMARY KEY (REGION_ID);
CREATE SEQUENCE HR.DEPARTMENTS_SEQ
START WITH 280
INCREMENT BY 10
MINVALUE 1
MAXVALUE 9990
NOCACHE;
CREATE SEQUENCE HR.EMPLOYEES_SEQ
START WITH 207
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9999999999999999999999999999
NOCACHE;
CREATE SEQUENCE HR.LOCATIONS_SEQ
START WITH 3300
INCREMENT BY 100
MINVALUE 1
MAXVALUE 9900
NOCACHE;
CREATE OR REPLACE VIEW HR.EMP_DETAILS_VIEW
(
EMPLOYEE_ID,
JOB_ID,
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;
CREATE OR REPLACE PROCEDURE HR.add_job_history
( p_emp_id job_history.employee_id%type
, 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%type
)
IS
BEGIN
INSERT INTO job_history (employee_id, start_date, end_date,
job_id, department_id)
VALUES(p_emp_id, p_start_date, p_end_date, p_job_id, p_department_id);
END add_job_history;
/
CREATE OR REPLACE PROCEDURE HR.secure_dml
IS
BEGIN
IF TO_CHAR (SYSDATE, 'HH24:MI') NOT BETWEEN '08:00' AND '18:00'
OR TO_CHAR (SYSDATE, 'DY') IN ('SAT', 'SUN') THEN
RAISE_APPLICATION_ERROR (-20205,
'You may only make changes during normal office hours');
END IF;
END secure_dml;
/
CREATE OR REPLACE TRIGGER HR.secure_employees
BEFORE INSERT OR UPDATE OR DELETE ON employees
BEGIN
secure_dml;
END secure_employees;
/
CREATE OR REPLACE TRIGGER HR.update_job_history
AFTER UPDATE OF job_id, department_id ON employees
FOR EACH ROW
BEGIN
add_job_history(:old.employee_id, :old.hire_date, sysdate,
:old.job_id, :old.department_id);
END;
/
GRANT INHERIT PRIVILEGES ON HR.HR TO PUBLIC;
스크립트를 전면적으로 수정합니다.
HR을 HR2 로 변경합니다.
DW 쪽 테이블을 담을 TABLESPACE 를 생성합니다.
--> to-be 창에서
create tablespace hr_tables
datafile '/home/oracle/hr_tables01.dbf' size 1024m;
HR 이 가지고 있는 테이블의 데이터 크기를 확인합니다.
select table_name, num_rows, avg_row_len, num_rows*avg_row_len /1024
from user_tables
order by num_rows desc;
select sum(num_rows*avg_row_len /1024 )
from user_tables;
DW 쪽에 테이블을 생성하기 위해서 테이블 생성 스크립트를 hr_tables 테이블 스페이스로 저장하도록 수정합니다.
SYSAUX를 --> HR_TABLES로
--- 1. 테이블 생성 스크립트
CREATE TABLE HR2.COUNTRIES
(
COUNTRY_ID CHAR(2) NOT NULL,
COUNTRY_NAME VARCHAR2(40),
REGION_ID NUMBER,
CONSTRAINT COUNTRY_C_ID_PK PRIMARY KEY (COUNTRY_ID)
)
ORGANIZATION INDEX
TABLESPACE HR_TABLES
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOMAPPING;
COMMENT ON TABLE HR2.COUNTRIES IS 'country table. Contains 25 rows. References with locations table.';
COMMENT ON COLUMN HR2.COUNTRIES.COUNTRY_ID IS 'Primary key of countries table.';
COMMENT ON COLUMN HR2.COUNTRIES.COUNTRY_NAME IS 'Country name';
COMMENT ON COLUMN HR2.COUNTRIES.REGION_ID IS 'Region ID for the country. Foreign key to region_id column in the departments table.';
CREATE TABLE HR2.DEPARTMENTS
(
DEPARTMENT_ID NUMBER(4) NOT NULL,
DEPARTMENT_NAME VARCHAR2(30) NOT NULL,
MANAGER_ID NUMBER(6),
LOCATION_ID NUMBER(4)
)
TABLESPACE HR_TABLES
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR2.DEPARTMENTS IS 'Departments table that shows details of departments where employees
work. Contains 27 rows; references with locations, employees, and job_history tables.';
COMMENT ON COLUMN HR2.DEPARTMENTS.DEPARTMENT_ID IS 'Primary key column of departments table.';
COMMENT ON COLUMN HR2.DEPARTMENTS.DEPARTMENT_NAME IS 'A not null column that shows name of a department. Administration,
Marketing, Purchasing, Human Resources, Shipping, IT, Executive, Public
Relations, Sales, Finance, and Accounting. ';
COMMENT ON COLUMN HR2.DEPARTMENTS.MANAGER_ID IS 'Manager_id of a department. Foreign key to employee_id column of employees table. The manager_id column of the employee table references this column.';
COMMENT ON COLUMN HR2.DEPARTMENTS.LOCATION_ID IS 'Location id where a department is located. Foreign key to location_id column of locations table.';
CREATE TABLE HR2.EMPLOYEES
(
EMPLOYEE_ID NUMBER(6) NOT NULL,
FIRST_NAME VARCHAR2(20),
LAST_NAME VARCHAR2(25) NOT NULL,
EMAIL VARCHAR2(25) NOT NULL,
PHONE_NUMBER VARCHAR2(20),
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2),
COMMISSION_PCT NUMBER(2,2),
MANAGER_ID NUMBER(6),
DEPARTMENT_ID NUMBER(4)
)
TABLESPACE HR_TABLES
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR2.EMPLOYEES IS 'employees table. Contains 107 rows. References with departments,
jobs, job_history tables. Contains a self reference.';
COMMENT ON COLUMN HR2.EMPLOYEES.EMPLOYEE_ID IS 'Primary key of employees table.';
COMMENT ON COLUMN HR2.EMPLOYEES.FIRST_NAME IS 'First name of the employee. A not null column.';
COMMENT ON COLUMN HR2.EMPLOYEES.LAST_NAME IS 'Last name of the employee. A not null column.';
COMMENT ON COLUMN HR2.EMPLOYEES.EMAIL IS 'Email id of the employee';
COMMENT ON COLUMN HR2.EMPLOYEES.PHONE_NUMBER IS 'Phone number of the employee; includes country code and area code';
COMMENT ON COLUMN HR2.EMPLOYEES.HIRE_DATE IS 'Date when the employee started on this job. A not null column.';
COMMENT ON COLUMN HR2.EMPLOYEES.JOB_ID IS 'Current job of the employee; foreign key to job_id column of the
jobs table. A not null column.';
COMMENT ON COLUMN HR2.EMPLOYEES.SALARY IS 'Monthly salary of the employee. Must be greater
than zero (enforced by constraint emp_salary_min)';
COMMENT ON COLUMN HR2.EMPLOYEES.COMMISSION_PCT IS 'Commission percentage of the employee; Only employees in sales
department elgible for commission percentage';
COMMENT ON COLUMN HR2.EMPLOYEES.MANAGER_ID IS 'Manager id of the employee; has same domain as manager_id in
departments table. Foreign key to employee_id column of employees table.
(useful for reflexive joins and CONNECT BY query)';
COMMENT ON COLUMN HR2.EMPLOYEES.DEPARTMENT_ID IS 'Department id where employee works; foreign key to department_id
column of the departments table';
CREATE TABLE HR2.JOBS
(
JOB_ID VARCHAR2(10) NOT NULL,
JOB_TITLE VARCHAR2(35) NOT NULL,
MIN_SALARY NUMBER(6),
MAX_SALARY NUMBER(6)
)
TABLESPACE HR_TABLES
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR2.JOBS IS 'jobs table with job titles and salary ranges. Contains 19 rows.
References with employees and job_history table.';
COMMENT ON COLUMN HR2.JOBS.JOB_ID IS 'Primary key of jobs table.';
COMMENT ON COLUMN HR2.JOBS.JOB_TITLE IS 'A not null column that shows job title, e.g. AD_VP, FI_ACCOUNTANT';
COMMENT ON COLUMN HR2.JOBS.MIN_SALARY IS 'Minimum salary for a job title.';
COMMENT ON COLUMN HR2.JOBS.MAX_SALARY IS 'Maximum salary for a job title';
CREATE TABLE HR2.JOB_HISTORY
(
EMPLOYEE_ID NUMBER(6) NOT NULL,
START_DATE DATE NOT NULL,
END_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
DEPARTMENT_ID NUMBER(4)
)
TABLESPACE HR_TABLES
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR2.JOB_HISTORY IS 'Table that stores job history of the employees. If an employee
changes departments within the job or changes jobs within the department,
new rows get inserted into this table with old job information of the
employee. Contains a complex primary key: employee_id+start_date.
Contains 25 rows. References with jobs, employees, and departments tables.';
COMMENT ON COLUMN HR2.JOB_HISTORY.EMPLOYEE_ID IS 'A not null column in the complex primary key employee_id+start_date.
Foreign key to employee_id column of the employee table';
COMMENT ON COLUMN HR2.JOB_HISTORY.START_DATE IS 'A not null column in the complex primary key employee_id+start_date.
Must be less than the end_date of the job_history table. (enforced by
constraint jhist_date_interval)';
COMMENT ON COLUMN HR2.JOB_HISTORY.END_DATE IS 'Last day of the employee in this job role. A not null column. Must be
greater than the start_date of the job_history table.
(enforced by constraint jhist_date_interval)';
COMMENT ON COLUMN HR2.JOB_HISTORY.JOB_ID IS 'Job role in which the employee worked in the past; foreign key to
job_id column in the jobs table. A not null column.';
COMMENT ON COLUMN HR2.JOB_HISTORY.DEPARTMENT_ID IS 'Department id in which the employee worked in the past; foreign key to deparment_id column in the departments table';
CREATE TABLE HR2.LOCATIONS
(
LOCATION_ID NUMBER(4) NOT NULL,
STREET_ADDRESS VARCHAR2(40),
POSTAL_CODE VARCHAR2(12),
CITY VARCHAR2(30) NOT NULL,
STATE_PROVINCE VARCHAR2(25),
COUNTRY_ID CHAR(2)
)
TABLESPACE HR_TABLES
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
COMMENT ON TABLE HR2.LOCATIONS IS 'Locations table that contains specific address of a specific office,
warehouse, and/or production site of a company. Does not store addresses /
locations of customers. Contains 23 rows; references with the
departments and countries tables. ';
COMMENT ON COLUMN HR2.LOCATIONS.LOCATION_ID IS 'Primary key of locations table';
COMMENT ON COLUMN HR2.LOCATIONS.STREET_ADDRESS IS 'Street address of an office, warehouse, or production site of a company.
Contains building number and street name';
COMMENT ON COLUMN HR2.LOCATIONS.POSTAL_CODE IS 'Postal code of the location of an office, warehouse, or production site
of a company. ';
COMMENT ON COLUMN HR2.LOCATIONS.CITY IS 'A not null column that shows city where an office, warehouse, or
production site of a company is located. ';
COMMENT ON COLUMN HR2.LOCATIONS.STATE_PROVINCE IS 'State or Province where an office, warehouse, or production site of a
company is located.';
COMMENT ON COLUMN HR2.LOCATIONS.COUNTRY_ID IS 'Country where an office, warehouse, or production site of a company is
located. Foreign key to country_id column of the countries table.';
CREATE TABLE HR2.REGIONS
(
REGION_ID NUMBER NOT NULL,
REGION_NAME VARCHAR2(25)
)
TABLESPACE HR_TABLES
STORAGE
(
INITIAL 64K
NEXT 1M
)
NOCOMPRESS;
select count(*)
from user_tables;