SQL Code On Database

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

CREATE DATABASE HFESTS;

show databases;
use HFESTS;
show tables;

/* A person can have only one residence at a time, and the system records and
maintains the current
residence of a person. Every person in the system must have a residence that is
currently living in.
*/
CREATE TABLE Residence(
residence_id INT PRIMARY KEY AUTO_INCREMENT,
residence_type ENUM('Apartment','Condominium','Semidetached House','House'),
address varchar(100),
city varchar(50),
province_pid varchar(50),
postal_code char(6),
residence_phone_number varchar(15),
number_of_bedrooms int,
foreign key(province_pid) references province(PID)
);

CREATE TABLE Persons(


person_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL ,
dob DATE, -- Date of Birth
ssn VARCHAR(10) UNIQUE NOT NULL, -- SSN cannot be null for any person, no two
people can have same SSN
medicare VARCHAR(12) UNIQUE,
phone_number VARCHAR(15),
address VARCHAR(100),
city VARCHAR(50),
province_pid varchar(50),
postal_code CHAR(6),
citizenship VARCHAR(50),
residence_id INT NOT NULL,
email VARCHAR(50),
FOREIGN KEY(residence_id) REFERENCES Residence(residence_id),
FOREIGN KEY(province_pid) REFERENCES Province(PID)
);

CREATE TABLE Employee(


employee_id int PRIMARY KEY AUTO_INCREMENT,
person_id int UNIQUE,
medicare VARCHAR(12) UNIQUE NOT NULL, -- No employees with same medicare num
foreign key(person_id) references Persons(person_id)
);

CREATE table Job_Type(


job_type_id INT PRIMARY KEY AUTO_INCREMENT,
name varchar(50) NOT NULL
);
/* Facility attributes:
type : CLSC, Clinic, Pharamacy, Special Installment
name,
address,
province : References table Province(PID,abbr,name) ,
city,
postal-code,
phone number,
web address,
capacity,
general manager (ONE to ONE) References Employee
*/
CREATE TABLE Facility(
facility_id INT PRIMARY KEY AUTO_INCREMENT,
facility_type ENUM('CLSC', 'Clinic', 'Pharmacy', 'Special Installment'),
facility_name varchar(50),
address varchar(100),
province_pid varchar(50),
city varchar(50),
postal_code char(6),
phone_number varchar(15),
web_address varchar(50),
type_id int,
capacity int,
general_manager_id int UNIQUE, -- Ensures only an employee can only be
general manager of one facility
foreign key(province_pid) references Province(PID),
foreign key (general_manager_id) references Employee(employee_id)

);

/* Employment maintains the role of each employee


An employee can work at only one facility at the same time.
An employee can work at different facilities at different times.
If date_quit is NULL it means employee is still working there.
*/
CREATE TABLE Employment(
employment_id int PRIMARY KEY AUTO_INCREMENT,
employee_id int,
facility_id int,
date_started date NOT NULL,
date_quit date,
role_type ENUM('Nurse','Doctor','Cashier','Pharmacist',
'Receptionist','Administrative Personnel','Security Personnel',
'Regular Employee'), -- A general manager is considered administrative personel
foreign key (employee_id) references Employee(employee_id),
foreign key (facility_id) references Facility(facility_id)
);

/*Many to many relation between person and employee


The relationship between the person and the employee can be :
Roommate,
Partner,
Parent,
Dependent
*/
CREATE TABLE LivesWith (
lives_with_id INT PRIMARY KEY AUTO_INCREMENT,
person_id INT,
employee_id INT,
relationship ENUM ('Roommate','Partner','Parent','Dependent'),
FOREIGN KEY (person_id) REFERENCES Persons (person_id),
FOREIGN KEY (employee_id) REFERENCES Employee (employee_id),
UNIQUE(person_id, employee_id)
);

CREATE TABLE Vaccination(


person_id INT PRIMARY KEY,
type_vaccine ENUM('Pfizer','Moderna','AstraZeneca','Johnson & Johnson'),
dose_number int,
vaccination_date date,
facility_id int,
foreign key(facility_id) references Facility(facility_id),
foreign key (person_id)references Persons(person_id)
);

/*Possible types are COVID 19, SARS-COV-2, or OTHERS*/


CREATE table Infection_Type(
infection_type_id int primary key auto_increment,
name varchar(100)
);
CREATE TABLE IsInfected(
infections_id int primary key auto_increment,
person_id int,
date_of_infection date NOT NULL,
infection_type_id int NOT NULL,
foreign key(infection_type_id) references Infection_type(infection_type_id),
foreign key(person_id) references Persons(person_id)

);

You might also like