Created
April 5, 2020 17:26
-
-
Save pedrominicz/3f21e839cac58fead3d9b70201c1e56f to your computer and use it in GitHub Desktop.
Simple Oracle Database schema.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- The database schema used in the second half of the following tutorial: | |
-- | |
-- https://www.youtube.com/watch?v=HXV3zeQKqGY | |
-- | |
-- Adapted to Oracle Database. | |
create table employee ( | |
id int not null, | |
first_name varchar(20) not null, | |
last_name varchar(20) not null, | |
birth_date date not null, | |
-- A cool thing to learn is if I can require this to be 'f' or 'm'. | |
sex varchar(1) not null, | |
salary int not null, | |
-- Only supervisor and branch can be `NULL`. | |
supervisor int, | |
branch int | |
); | |
alter table employee add constraint employee_pk primary key (id); | |
-- `on delete` specifies what to do if the referenced employee is deleted. | |
-- Without this, referenced employees won't be deleteable. | |
alter table employee add constraint employee_supervisor_fk foreign key (supervisor) references employee (id) on delete set null; | |
create table branch ( | |
id int not null, | |
branch_name varchar(20) not null, | |
manager int not null, | |
manager_start_date date not null | |
); | |
alter table branch add constraint branch_pk primary key (id); | |
alter table branch add constraint branch_manager_fk foreign key (manager) references employee (id) on delete set null; | |
alter table employee add constraint employee_branch_fk foreign key (branch) references branch (id) on delete set null; | |
create table client ( | |
id int not null, | |
name varchar(20) not null, | |
branch int not null | |
); | |
alter table client add constraint client_pk primary key (id); | |
alter table client add constraint client_branch_fk foreign key (branch) references branch (id) on delete set null; | |
create table works_with ( | |
employee int not null, | |
client int not null, | |
total_sales int default 0 not null | |
); | |
alter table works_with add constraint works_with_pk primary key (employee, client); | |
-- If the referenced employee or client is deleted so will the corresponding | |
-- entries in this table. | |
alter table works_with add constraint works_with_employee_fk foreign key (employee) references employee (id) on delete cascade; | |
alter table works_with add constraint works_with_client_fk foreign key (client) references client (id) on delete cascade; | |
create table branch_supplier ( | |
branch int not null, | |
name varchar(20) not null, | |
type varchar(20) not null | |
); | |
alter table branch_supplier add constraint branch_supplier_pk primary key (branch, name); | |
alter table branch_supplier add constraint branch_supplier_branch_fk foreign key (branch) references branch (id) on delete cascade; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment