CAFE

6.오라클 관리 수업

Re: 오늘의 마지막 문제. (이수자 평가 6번 문제)

작성자21기_이풍혁|작성시간25.09.10|조회수28 목록 댓글 0

1. sys 계정에서 유저 생성

create user lph identified by lph;
grant dba to lph;

 

2. 테이블 데이터, 인덱스 용 테이블 스페이스 생성

CREATE TABLESPACE HR_DATA_TS02
DATAFILE
'/u01/app/oracle/oradata/ORCLDW/hr_data_ts02.dbf'
SIZE 1G
AUTOEXTEND OFF
BLOCKSIZE 8K
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

CREATE TABLESPACE HR_INDEX_TS02
DATAFILE
'/u01/app/oracle/oradata/ORCLDW/hr_index_ts02.dbf'
SIZE 1G
AUTOEXTEND OFF
BLOCKSIZE 8K
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

 

3. orange에서 script gen으로 이행 파일 생성

4. 이행 파일에서 create table 실행

CREATE TABLE 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_DATA_TS02
NOMAPPING;

COMMENT ON TABLE COUNTRIES IS 'country table. Contains 25 rows. References with locations table.';
COMMENT ON COLUMN COUNTRIES.COUNTRY_ID IS 'Primary key of countries table.';
COMMENT ON COLUMN COUNTRIES.COUNTRY_NAME IS 'Country name';
COMMENT ON COLUMN COUNTRIES.REGION_ID IS 'Region ID for the country. Foreign key to region_id column in the departments table.';

CREATE TABLE DEPARTMENTS
(
    DEPARTMENT_ID   NUMBER(4),
    DEPARTMENT_NAME VARCHAR2(30) NOT NULL,
    MANAGER_ID      NUMBER(6),
    LOCATION_ID     NUMBER(4)
)
TABLESPACE HR_DATA_TS02
NOCOMPRESS
ENABLE ROW MOVEMENT;

COMMENT ON TABLE 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 DEPARTMENTS.DEPARTMENT_ID IS 'Primary key column of departments table.';
COMMENT ON COLUMN 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 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 DEPARTMENTS.LOCATION_ID IS 'Location id where a department is located. Foreign key to location_id column of locations table.';

CREATE TABLE 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_DATA_TS02
NOCOMPRESS
CACHE;

COMMENT ON TABLE EMPLOYEES IS 'employees table. Contains 107 rows. References with departments,
jobs, job_history tables. Contains a self reference.';
COMMENT ON COLUMN EMPLOYEES.EMPLOYEE_ID IS 'Primary key of employees table.';
COMMENT ON COLUMN EMPLOYEES.FIRST_NAME IS 'First name of the employee. A not null column.';
COMMENT ON COLUMN EMPLOYEES.LAST_NAME IS 'Last name of the employee. A not null column.';
COMMENT ON COLUMN EMPLOYEES.EMAIL IS 'Email id of the employee';
COMMENT ON COLUMN EMPLOYEES.PHONE_NUMBER IS 'Phone number of the employee; includes country code and area code';
COMMENT ON COLUMN EMPLOYEES.HIRE_DATE IS 'Date when the employee started on this job. A not null column.';
COMMENT ON COLUMN 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 EMPLOYEES.SALARY IS 'Monthly salary of the employee. Must be greater
than zero (enforced by constraint emp_salary_min)';
COMMENT ON COLUMN EMPLOYEES.COMMISSION_PCT IS 'Commission percentage of the employee; Only employees in sales
department elgible for commission percentage';
COMMENT ON COLUMN 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 EMPLOYEES.DEPARTMENT_ID IS 'Department id where employee works; foreign key to department_id
column of the departments table';

CREATE TABLE JOBS
(
    JOB_ID      VARCHAR2(10),
    JOB_TITLE   VARCHAR2(35) NOT NULL,
    MIN_SALARY  NUMBER(6),
    MAX_SALARY  NUMBER(6)
)
TABLESPACE HR_DATA_TS02
NOCOMPRESS
ENABLE ROW MOVEMENT;

COMMENT ON TABLE JOBS IS 'jobs table with job titles and salary ranges. Contains 19 rows.
References with employees and job_history table.';
COMMENT ON COLUMN JOBS.JOB_ID IS 'Primary key of jobs table.';
COMMENT ON COLUMN JOBS.JOB_TITLE IS 'A not null column that shows job title, e.g. AD_VP, FI_ACCOUNTANT';
COMMENT ON COLUMN JOBS.MIN_SALARY IS 'Minimum salary for a job title.';
COMMENT ON COLUMN JOBS.MAX_SALARY IS 'Maximum salary for a job title';

CREATE TABLE 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_DATA_TS02
NOCOMPRESS;

COMMENT ON TABLE 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 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 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 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 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 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 LOCATIONS
(
    LOCATION_ID     NUMBER(4),
    STREET_ADDRESS  VARCHAR2(40),
    POSTAL_CODE     VARCHAR2(12),
    CITY            VARCHAR2(30) NOT NULL,
    STATE_PROVINCE  VARCHAR2(25),
    COUNTRY_ID      CHAR(2)
)
TABLESPACE HR_DATA_TS02
NOCOMPRESS
ENABLE ROW MOVEMENT;

COMMENT ON TABLE 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 LOCATIONS.LOCATION_ID IS 'Primary key of locations table';
COMMENT ON COLUMN 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 LOCATIONS.POSTAL_CODE IS 'Postal code of the location of an office, warehouse, or production site
of a company. ';
COMMENT ON COLUMN 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 LOCATIONS.STATE_PROVINCE IS 'State or Province where an office, warehouse, or production site of a
company is located.';
COMMENT ON COLUMN 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 REGIONS
(
    REGION_ID   NUMBER NOT NULL,
    REGION_NAME VARCHAR2(25)
)
TABLESPACE HR_DATA_TS02
NOCOMPRESS;

5. 데이터 이행 PL/SQL 실행

alter session enable parallel dml;
BEGIN
  for tab_rec in (select table_name from user_tables) loop
    DBMS_OUTPUT.PUT_LINE('Table name : ' || tab_rec.table_name);
    execute immediate 'insert /*+ append parallel(nt, 4) nologging */ into ' || tab_rec.table_name || ' nt select * from ' || tab_rec.table_name || '@LINK_ORCL_HR ot';
    commit;
  end loop;
END;
/

 

6.  인덱스, 제약조건, 프로시져, 트리거 등 나머지 객체 생성

CREATE UNIQUE INDEX COUNTRY_C_ID_PK
ON COUNTRIES (COUNTRY_ID)
TABLESPACE HR_INDEX_TS02;

CREATE UNIQUE INDEX DEPT_ID_PK
ON DEPARTMENTS (DEPARTMENT_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX DEPT_LOCATION_IX
ON DEPARTMENTS (LOCATION_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX EMP_DEPARTMENT_IX
ON EMPLOYEES (DEPARTMENT_ID)
TABLESPACE HR_INDEX_TS02;

CREATE UNIQUE INDEX EMP_EMP_ID_PK
ON EMPLOYEES (EMPLOYEE_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX EMP_HIRE_DATE_IX
ON EMPLOYEES (HIRE_DATE)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX EMP_JOB_IX
ON EMPLOYEES (JOB_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX EMP_MANAGER_IX
ON EMPLOYEES (MANAGER_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX EMP_NAME_IX
ON EMPLOYEES (LAST_NAME,FIRST_NAME)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX JHIST_DEPARTMENT_IX
ON JOB_HISTORY (DEPARTMENT_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX JHIST_EMPLOYEE_IX
ON JOB_HISTORY (EMPLOYEE_ID)
TABLESPACE HR_INDEX_TS02;

CREATE UNIQUE INDEX JHIST_EMP_ID_ST_DATE_PK
ON JOB_HISTORY (EMPLOYEE_ID,START_DATE)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX JHIST_JOB_IX
ON JOB_HISTORY (JOB_ID)
TABLESPACE HR_INDEX_TS02;

CREATE UNIQUE INDEX JOB_ID_PK
ON JOBS (JOB_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX LOC_CITY_IX
ON LOCATIONS (CITY)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX LOC_COUNTRY_IX
ON LOCATIONS (COUNTRY_ID)
TABLESPACE HR_INDEX_TS02;

CREATE UNIQUE INDEX LOC_ID_PK
ON LOCATIONS (LOCATION_ID)
TABLESPACE HR_INDEX_TS02;

CREATE INDEX LOC_STATE_PROVINCE_IX
ON LOCATIONS (STATE_PROVINCE)
TABLESPACE HR_INDEX_TS02;

CREATE UNIQUE INDEX REG_ID_PK
ON REGIONS (REGION_ID)
TABLESPACE HR_INDEX_TS02;

ALTER TABLE COUNTRIES
ADD CONSTRAINT COUNTRY_ID_NN CHECK ("COUNTRY_ID" IS NOT NULL);

ALTER TABLE DEPARTMENTS
ADD CONSTRAINT DEPT_NAME_NN CHECK ("DEPARTMENT_NAME" IS NOT NULL);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_EMAIL_NN CHECK ("EMAIL" IS NOT NULL);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_HIRE_DATE_NN CHECK ("HIRE_DATE" IS NOT NULL);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_JOB_NN CHECK ("JOB_ID" IS NOT NULL);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_LAST_NAME_NN CHECK ("LAST_NAME" IS NOT NULL);

ALTER TABLE COUNTRIES
ADD CONSTRAINT COUNTRY_C_ID_PK PRIMARY KEY (COUNTRY_ID);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_EMP_ID_PK PRIMARY KEY (EMPLOYEE_ID);

ALTER TABLE DEPARTMENTS
ADD CONSTRAINT DEPT_MGR_FK FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES (EMPLOYEE_ID);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_EMAIL_UK UNIQUE (EMAIL);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_SALARY_MIN CHECK (salary > 0);

ALTER TABLE JOBS
ADD CONSTRAINT JOB_TITLE_NN CHECK ("JOB_TITLE" IS NOT NULL);

ALTER TABLE JOB_HISTORY
ADD CONSTRAINT JHIST_DATE_INTERVAL CHECK (end_date > start_date);

ALTER TABLE JOB_HISTORY
ADD CONSTRAINT JHIST_EMPLOYEE_NN CHECK ("EMPLOYEE_ID" IS NOT NULL);

ALTER TABLE JOB_HISTORY
ADD CONSTRAINT JHIST_END_DATE_NN CHECK ("END_DATE" IS NOT NULL);

ALTER TABLE JOB_HISTORY
ADD CONSTRAINT JHIST_JOB_NN CHECK ("JOB_ID" IS NOT NULL);

ALTER TABLE JOB_HISTORY
ADD CONSTRAINT JHIST_START_DATE_NN CHECK ("START_DATE" IS NOT NULL);

ALTER TABLE LOCATIONS
ADD CONSTRAINT LOC_CITY_NN CHECK ("CITY" IS NOT NULL);

ALTER TABLE EMPLOYEES
ADD CONSTRAINT EMP_MANAGER_FK FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEES (EMPLOYEE_ID);

ALTER TABLE JOB_HISTORY
ADD CONSTRAINT JHIST_EMP_FK FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEES (EMPLOYEE_ID);

ALTER TABLE REGIONS
ADD CONSTRAINT REGION_ID_NN CHECK ("REGION_ID" IS NOT NULL);

ALTER TABLE LOCATIONS
ADD CONSTRAINT LOC_C_ID_FK FOREIGN KEY (COUNTRY_ID) REFERENCES COUNTRIES (COUNTRY_ID);

CREATE SEQUENCE DEPARTMENTS_SEQ
START WITH 280
INCREMENT BY 10
MINVALUE 1
MAXVALUE 9990
NOCACHE;

CREATE SEQUENCE EMPLOYEES_SEQ
START WITH 207
INCREMENT BY 1
MINVALUE 1
MAXVALUE 9999999999999999999999999999
NOCACHE;

CREATE SEQUENCE LOCATIONS_SEQ
START WITH 3300
INCREMENT BY 100
MINVALUE 1
MAXVALUE 9900
NOCACHE;

CREATE OR REPLACE VIEW 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 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 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 secure_employees
  BEFORE INSERT OR UPDATE OR DELETE ON employees
BEGIN
  secure_dml;
END secure_employees;
/

CREATE OR REPLACE TRIGGER 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;
/

7. 스키마 통계정보 수집

exec DBMS_STATS.GATHER_SCHEMA_STATS('LPH');

8. 중복되는 제약조건 제거

alter table COUNTRIES drop constraint SYS_C007796;
alter table EMPLOYEES drop constraint SYS_C007800;
alter table JOB_HISTORY drop constraint SYS_C007808;
alter table JOBS drop constraint SYS_C007804;
alter table JOB_HISTORY drop constraint SYS_C007805;
alter table LOCATIONS drop constraint SYS_C007809;
alter table EMPLOYEES drop constraint SYS_C007802;
alter table EMPLOYEES drop constraint SYS_C007803;
alter table DEPARTMENTS drop constraint SYS_C007798;
alter table JOB_HISTORY drop constraint SYS_C007806;
alter table JOB_HISTORY drop constraint SYS_C007807;
alter table EMPLOYEES drop constraint SYS_C007801;
alter table REGIONS drop constraint SYS_C007810;
alter table EMPLOYEES drop constraint SYS_C007799;

 

9. 결과 검증

select o.table_name, o.num_rows, n.num_rows, o.last_analyzed
  from  user_tables@LINK_ORCL_HR  o , user_tables  n
  where o.table_name = n.table_name;



select count(*)  
 from user_constraints
 union all
 select count(*)  
 from user_constraints@LINK_ORCL_HR;

select count(*)  
 from user_indexes
union all
select count(*)  
 from user_indexes@LINK_ORCL_HR;

 

다음검색
현재 게시글 추가 기능 열기

댓글

댓글 리스트
맨위로

카페 검색

카페 검색어 입력폼