create table jobs (
job_id constraint job_pk primary key,
job_title, min_salary, max_salary
) as
select * from hr.jobs
Table created.
create table employees (
employee_id primary key,
first_name not null, last_name not null,
hire_date not null, department_id not null,
salary not null, job_id not null
) as
select employee_id, first_name, last_name, hire_date, department_id, salary, job_id
from hr.employees
where department_id is not null
Table created.
create table departments (
department_id primary key, department_name not null, manager_id not null
) as
select department_id, department_name, manager_id
from hr.departments
where manager_id is not null
Table created.
create table job_history (
employee_id,
start_date not null, end_date,
job_id not null, department_id not null,
constraint job_history_pk
primary key ( employee_id, start_date ),
constraint job_history_u
unique ( employee_id, end_date )
) as
select employee_id, start_date,
lead ( start_date, 1, end_date ) over (
partition by employee_id order by start_date
) end_date,
job_id, department_id
from hr.job_history
Table created.
Bidirectional FKs
alter table departments
add constraint dept_manager_fk
foreign key ( manager_id )
references employees
Table altered.
alter table employees
add constraint empl_department_fk
foreign key ( department_id )
references departments
Table altered.
insert into employees ( employee_id, first_name, last_name, hire_date, department_id, salary, job_id )
values ( 42, 'Tess', 'Ting', sysdate, 42, 5000, 'IT_PROG' )
ORA-02291: integrity constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.EMPL_DEPARTMENT_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
insert into departments ( department_id, department_name, manager_id )
values ( 42, 'Test dept', 42 )
ORA-02291: integrity constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.DEPT_MANAGER_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
alter table departments
modify constraint dept_manager_fk
deferrable
ORA-00933: SQL command not properly endedMore Details: https://docs.oracle.com/error-help/db/ora-00933
alter table departments
drop constraint dept_manager_fk
Table altered.
Deferrable FK
alter table departments
add constraint dept_manager_fk
foreign key ( manager_id )
references employees ( employee_id )
deferrable
Table altered.
insert into departments ( department_id, department_name, manager_id )
values ( 42, 'Test dept', 42 )
ORA-02291: integrity constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.DEPT_MANAGER_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
Delay constraint validation
alter session set constraints = deferred
Statement processed.
insert into employees ( employee_id, first_name, last_name, hire_date, department_id, salary, job_id )
values ( 42, 'Tess', 'Ting', sysdate, 42, 5000, 'IT_PROG' )
ORA-02291: integrity constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.EMPL_DEPARTMENT_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
insert into departments ( department_id, department_name, manager_id )
values ( 42, 'Test dept', 42 )
1 row(s) inserted.
commit
ORA-02091: transaction rolled back ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02091
insert into departments ( department_id, department_name, manager_id )
values ( 42, 'Test dept', 42 )
1 row(s) inserted.
insert into employees ( employee_id, first_name, last_name, hire_date, department_id, salary, job_id )
values ( 42, 'Tess', 'Ting', sysdate, 42, 5000, 'IT_PROG' )
1 row(s) inserted.
commit
Statement processed.
create materialized view log on employees
with rowid, primary key ( department_id, salary ),
sequence
including new values
Statement processed.
create materialized view log on departments
with rowid, primary key,
sequence
including new values
Statement processed.
Using MVs to ensure every parent row has a child
create materialized view department_employees_mv
refresh fast on commit
as
select e.rowid empl_rid, d.rowid dept_rid,
e.employee_id, d.department_id
from employees e, departments d
where d.department_id = e.department_id (+)
ORA-12006: materialized view or zonemap "SQL_FPDCOIUZTVEPDOYPHSLHKCGST"."DEPARTMENT_EMPLOYEES_MV" already existsMore Details: https://docs.oracle.com/error-help/db/ora-12006
alter table department_employees_mv
modify employee_id
constraint deem_employee_nn
not null
deferrable
ORA-02264: name already used by an existing constraintMore Details: https://docs.oracle.com/error-help/db/ora-02264
alter table departments
drop constraint dept_manager_fk
Table altered.
insert into departments ( department_id, department_name, manager_id )
values ( 99, 'test', 99 )
1 row(s) inserted.
select e.employee_id, d.department_id
from employees e, departments d
where d.department_id = e.department_id (+)
and d.department_id = 99
EMPLOYEE_ID | DEPARTMENT_ID | - | 99 |
---|
commit
Statement processed.
select e.employee_id, d.department_id
from employees e, departments d
where d.department_id = e.department_id (+)
and d.department_id = 99
EMPLOYEE_ID | DEPARTMENT_ID | - | 99 |
---|
insert into departments ( department_id, department_name, manager_id )
values ( 99, 'test', 99 )
ORA-00001: unique constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.SYS_C00116351545) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-00001
insert into employees ( employee_id, first_name, last_name, hire_date, department_id, salary, job_id )
values ( 99, 'Tess', 'Ting', sysdate, 99, 5000, 'IT_PROG' )
1 row(s) inserted.
commit
Statement processed.
select * from department_employees_mv
where department_id = 99
EMPL_RID | DEPT_RID | EMPLOYEE_ID | DEPARTMENT_ID | AJ75+kAAkAAAB4VAAB | AJ75+mAAkAAAB4lAAB | 99 | 99 |
---|
Using MVs to check values in one table fall in a range on another table
select * from jobs
JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY | AD_PRES | President | 20080 | 40000 | AD_VP | Administration Vice President | 15000 | 30000 | AD_ASST | Administration Assistant | 3000 | 6000 | FI_MGR | Finance Manager | 8200 | 16000 | FI_ACCOUNT | Accountant | 4200 | 9000 | AC_MGR | Accounting Manager | 8200 | 16000 | AC_ACCOUNT | Public Accountant | 4200 | 9000 | SA_MAN | Sales Manager | 10000 | 20080 | SA_REP | Sales Representative | 6000 | 12008 | PU_MAN | Purchasing Manager | 8000 | 15000 | PU_CLERK | Purchasing Clerk | 2500 | 5500 | ST_MAN | Stock Manager | 5500 | 8500 | ST_CLERK | Stock Clerk | 2008 | 5000 | SH_CLERK | Shipping Clerk | 2500 | 5500 | IT_PROG | Programmer | 4000 | 10000 | MK_MAN | Marketing Manager | 9000 | 15000 | MK_REP | Marketing Representative | 4000 | 9000 | HR_REP | Human Resources Representative | 4000 | 9000 | PR_REP | Public Relations Representative | 4500 | 10500 |
---|
create materialized view log on jobs
with rowid, primary key ( min_salary, max_salary ),
sequence
including new values
Statement processed.
create materialized view employee_jobs_mv
refresh fast on commit
as
select e.rowid empl_rid, j.rowid job_rid,
e.salary, j.min_salary, j.max_salary
from employees e, jobs j
where e.job_id = j.job_id
ORA-12006: materialized view or zonemap "SQL_FPDCOIUZTVEPDOYPHSLHKCGST"."EMPLOYEE_JOBS_MV" already existsMore Details: https://docs.oracle.com/error-help/db/ora-12006
alter table employee_jobs_mv
add constraint emjo_salary_c
check ( salary between min_salary and max_salary )
deferrable
ORA-02264: name already used by an existing constraintMore Details: https://docs.oracle.com/error-help/db/ora-02264
update employees
set salary = salary * 10
108 row(s) updated.
commit
Statement processed.
alter table departments
add ( total_salary number )
Table altered.
update departments d
set total_salary = (
select sum ( salary ) from employees e
where d.department_id = e.department_id
)
13 row(s) updated.
commit
Statement processed.
drop materialized view log on departments
Statement processed.
create materialized view log on departments
with rowid, primary key ( total_salary ),
sequence
including new values
Statement processed.
exec dbms_mview.refresh ( 'DEPARTMENT_EMPLOYEES_MV', 'C' )
Statement processed.
create materialized view department_salaries_mv
refresh fast on commit as
select d.department_id, d.total_salary,
sum ( e.salary ) emp_salaries,
count(*)
from employees e, departments d
where d.department_id = e.department_id
group by d.department_id, d.total_salary
ORA-12006: materialized view or zonemap "SQL_FPDCOIUZTVEPDOYPHSLHKCGST"."DEPARTMENT_SALARIES_MV" already existsMore Details: https://docs.oracle.com/error-help/db/ora-12006
alter table department_salaries_mv
add constraint desa_total_salaries_c
check ( total_salary = emp_salaries )
deferrable
ORA-02264: name already used by an existing constraintMore Details: https://docs.oracle.com/error-help/db/ora-02264
update employees
set salary = salary * 1.1
where job_id = 'IT_PROG'
7 row(s) updated.
commit
Statement processed.
update employees
set salary = salary * 1.1
where job_id = 'IT_PROG'
7 row(s) updated.
update departments d
set total_salary = (
select sum ( salary ) from employees e
where d.department_id = e.department_id
)
13 row(s) updated.
commit
Statement processed.
Enforce consecutive start/end dates with FKs
select * from job_history
order by employee_id, start_date
EMPLOYEE_ID | START_DATE | END_DATE | JOB_ID | DEPARTMENT_ID | 101 | 21-SEP-97 | 28-OCT-01 | AC_ACCOUNT | 110 | 101 | 28-OCT-01 | 15-MAR-05 | AC_MGR | 110 | 102 | 13-JAN-01 | 24-JUL-06 | IT_PROG | 60 | 114 | 24-MAR-06 | 31-DEC-07 | ST_CLERK | 50 | 120 | 18-JUL-04 | 16-MAR-19 | ST_MAN | 50 | 122 | 01-JAN-07 | 31-DEC-07 | ST_CLERK | 50 | 176 | 24-MAR-06 | 01-JAN-07 | SA_REP | 80 | 176 | 01-JAN-07 | 31-DEC-07 | SA_MAN | 80 | 200 | 17-SEP-95 | 01-JUL-02 | AD_ASST | 90 | 200 | 01-JUL-02 | 31-DEC-06 | AC_ACCOUNT | 90 | 201 | 17-FEB-04 | 19-DEC-07 | MK_REP | 20 |
---|
alter table job_history
add constraint johi_start_end_fk
foreign key ( employee_id, start_date )
references job_history ( employee_id, end_date )
novalidate
Table altered.
select * from job_history
where employee_id = 101
order by start_date
EMPLOYEE_ID | START_DATE | END_DATE | JOB_ID | DEPARTMENT_ID | 101 | 21-SEP-97 | 28-OCT-01 | AC_ACCOUNT | 110 | 101 | 28-OCT-01 | 15-MAR-05 | AC_MGR | 110 |
---|
insert into job_history ( employee_id, start_date, job_id, department_id )
values ( 101, date'2023-01-01', 'IT_PROG', 20 )
ORA-02291: integrity constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.JOHI_START_END_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
insert into job_history ( employee_id, start_date, job_id, department_id )
values ( 101, date'2005-03-15', 'IT_PROG', 20 )
1 row(s) inserted.
insert into job_history ( employee_id, start_date, job_id, department_id )
values ( 999, date'2023-01-01', 'IT_PROG', 20 )
ORA-02291: integrity constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.JOHI_START_END_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
alter table job_history
modify constraint johi_start_end_fk
disable
Table altered.
insert into job_history ( employee_id, start_date, job_id, department_id )
values ( 999, date'2023-01-01', 'IT_PROG', 20 )
1 row(s) inserted.
alter table job_history
modify constraint johi_start_end_fk
enable
novalidate
Table altered.
update job_history
set end_date = date'2023-03-21'
where employee_id = 999
and end_date is null
1 row(s) updated.
insert into job_history ( employee_id, start_date, job_id, department_id )
values ( 999, date'2023-03-21', 'PROGRAMMER', 20 )
1 row(s) inserted.
commit
Statement processed.
Super/subtype relationships
alter table employees
add constraint empl_employee_job_u
unique ( job_id, employee_id )
Table altered.
create table developers (
employee_id
constraint deve_employee_fk
references employees
constraint developer_pk
primary key,
job_id
-- Ensure only developers can be inserted
constraint deve_job_c
check ( job_id = 'IT_PROG' )
not null,
primary_programming_language varchar2(30)
not null,
-- Ensure only developers can be inserted
constraint deve_employee_job_fk
foreign key ( job_id, employee_id )
references employees ( job_id, employee_id )
)
Table created.
create table sales_staff (
employee_id
constraint sast_employee_fk
references employees
constraint sales_staff_pk
primary key,
job_id
-- Ensure only sales staff can be inserted
constraint sast_job_c
check ( job_id in ( 'SA_MAN', 'SA_REP' ) )
not null,
commission_pct number
not null,
-- Ensure only sales staff can be inserted
constraint sast_employee_job_fk
foreign key ( job_id, employee_id )
references employees ( job_id, employee_id )
)
Table created.
insert into sales_staff
values ( 42, 'IT_PROG', 0.1 )
ORA-02290: check constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.SAST_JOB_C) violated ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02290
insert into sales_staff
values ( 42, 'SA_MAN', 0.1 )
ORA-02291: integrity constraint (SQL_FPDCOIUZTVEPDOYPHSLHKCGST.SAST_EMPLOYEE_JOB_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721More Details: https://docs.oracle.com/error-help/db/ora-02291
insert into developers
values ( 42, 'IT_PROG', 'SQL' )
1 row(s) inserted.
commit
Statement processed.