database project

58
Database Systems Introduction to Databases and Data Warehouses (Solutions to mini cases: MC1, MC2, MC3, MC4, MC6, MC7 )

Upload: valerii-klymchuk

Post on 16-Apr-2017

669 views

Category:

Data & Analytics


5 download

TRANSCRIPT

Page 1: Database Project

Database SystemsIntroduction to Databases and Data Warehouses

(Solutions to mini cases: MC1, MC2, MC3, MC4, MC6, MC7 )

Page 2: Database Project

MC1 Investco ScoutInvestco Scout is an investment research company. Create the ER diagram for the Investco Scout Funds Database, based on the following requirements.• The Investco Scout Funds Database will keep track of investment companies,

the mutual funds they manage, and securities contained in the mutual funds.• For each investment company, Investco Scout will keep track of a unique

investment company identifier and a unique investment company name as well as the names of multiple locations of the investment company.• For each mutual fund, Investco Scout will keep track of a unique mutual fund

identifier as well as the mutual fund name and inception date.

Page 3: Database Project

• For each security, Investco Scout will keep track of a unique security identifier as well as security name and type.• Investment companies can manage multiple mutual funds. Investco

Scout does not keep track of investment companies that do not manage any mutual funds. A mutual fund is managed by one investment company.• A mutual fund contains one or many securities. A security can be

included in many mutual funds. Investco Scout keeps track of securities that are not included in any mutual funds.• For each instance of a security included in a mutual fund, Investco

Scout keeps track of the amount included.

Page 4: Database Project

MC1 Investco Scout Funds: ER Diagram

Page 5: Database Project

MC1 Investco Scout Funds: Relational Schema

Page 6: Database Project

Additions to the initial requirements• Investco Scout will keep track of the CEOFName and CEOLName for

each investment company (in addition to keeping track of a unique investment company identifier, a unique investment company name, and names of multiple locations of the investment company for each investment company).

Page 7: Database Project

CREATE TABLE statements:CREATE DATABASE IF NOT EXISTS MC1_Investco_Funds;

USE MC1_Investco_Funds;

CREATE TABLE COMPANY ( CompID CHAR(3) NOT NULL, CompName VARCHAR(45) NOT NULL, CEOFname VARCHAR(25) NOT NULL, CEOLname VARCHAR(25) NOT NULL, PRIMARY KEY (CompID), UNIQUE (CompName));

CREATE TABLE COMPANY_LocationName ( LocationName VARCHAR(25) NOT NULL, CompID CHAR(3) NOT NULL, PRIMARY KEY (LocationName , CompID), FOREIGN KEY (CompID) REFERENCES COMPANY (CompID));

Page 8: Database Project

CREATE TABLE FUND ( FundID CHAR(2) NOT NULL, FundName VARCHAR(25) NOT NULL, InceptionDate DATE NOT NULL, Mix VARCHAR(128) NOT NULL, CompID CHAR(3) NOT NULL, PRIMARY KEY (FundID), FOREIGN KEY (CompID) REFERENCES COMPANY (CompID));

CREATE TABLE SECURITY ( SecurityID CHAR(3) NOT NULL, SecName VARCHAR(25) NOT NULL, SecType VARCHAR(25) NOT NULL, PRIMARY KEY (SecurityID));

CREATE TABLE Contain ( Amount NUMERIC(7 , 2 ) NOT NULL, FundID CHAR(2) NOT NULL, SecurityID CHAR(3) NOT NULL, PRIMARY KEY (FundID , SecurityID), FOREIGN KEY (FundID) REFERENCES FUND (FundID), FOREIGN KEY (SecurityID) REFERENCES SECURITY (SecurityID));

Page 9: Database Project

INSERT INTO statements:INSERT INTO COMPANY VALUES ('ACF','Acme Finance','Mick','Dempsey'); INSERT INTO COMPANY VALUES ('TCA','Tara Capital','Ava','Newton'); INSERT INTO COMAPANY VALUES ('ALB','Albritton','Lena','Dollar'); INSERT INTO COMPANY_LocationName VALUES('Chicago','ACF'); INSERT INTO COMPANY_LocationName VALUES('Denver','ACF'); INSERT INTO COMPANY_LocationName VALUES('Houston','TCA'); INSERT INTO COMPANY_LocationName VALUES('New York City','TCA'); INSERT INTO COMPANY_LocationName VALUES('Atlanta','ALB'); INSERT INTO COMPANY_LocationName VALUES('New York City','ALB'); INSERT INTO SECURITY VALUES ('AE','Abhi Engineering','Stock'); INSERT INTO SECURITY VALUES ('BH','Blues Health','Stock');

Page 10: Database Project

INSERT INTO SECURITY VALUES ('CM', 'County Municipality', 'Bond'); INSERT INTO SECURITY VALUES ('DU', 'Downtown Utility','Bond'); INSERT INTO SECURITY VALUES ('EM', 'Emmitt Machines','Stock'); INSERT INTO FUND(CompID, InceptionDate, FundID, FundName, Mix) VALUES ('ACF', '2005-1-1', 'BG', 'Big Growth', '500 AE Stocks, 300 EM Stocks'); INSERT INTO FUND(CompID, InceptionDate, FundID, FundName, Mix) VALUES ('ACF', '2006-1-1','SG','Steady Growth', '300 AE Stocks, 300 DU Bonds'); INSERT INTO FUND VALUES ('LF', 'Tiger Fund', '2005-1-1','TCA', '1000 EM Stocks, 1000 BH Stocks'); INSERT INTO FUND VALUES ('OF', 'Owl Fund', '2006-1-1','TCA', '1000 CU Bonds, 1000 DU Bonds'); INSERT INTO FUND VALUES ('JU', 'Jupiter', '2005-1-1','ALB', '2000 EM Stock, 1000 DU Bonds'); INSERT INTO FUND VALUES ('SA', 'Saturn', '2006-1-1','ALB', '1000 EM Stock, 2000 DU Bonds');

Page 11: Database Project

MC1 Investco Scout Funds in MySQL Workbench

Page 12: Database Project

MC2 Funky BizzMC2 Funky Bizz Funky Bizz is a rental business that rents musical instruments to bands. Create the ER diagram for the Funky Bizz Operations Database, based on the following requirements. • The Funky Bizz Operations Database will keep track of instruments, bands,

repair technicians, and shows. For each instrument, Funky will keep track of a unique instrument serial number as well as the instrument model and brand the year when the instrument was made, and the age (measured in years) of the instrument. • The customers of Funky Bizz are bands. For each band, Funky Bizz will

keep track of the unique band name and unique band identifier as well as the band's address, contact person's name, and multiple phone numbers.

Page 13: Database Project

• Repair technicians maintain the instruments. For each technician, Funky Bizz will keep track of a unique SSN as well as a name, ad dress, and multiple phone numbers.

• Funky Bizz will record information about shows that its customer bands perform in. For each show, it will keep track of a unique show identifier composed of the show venue name and date.

• For each show it will also keep track of show type and show name (a show may or may not have a name)

• A band does not have to rent any instruments, but may rent up to 30. Each instrument may be rented by one band or by no bands at all

• A repair technician may repair many or no instruments, and an instrument may be repaired by many or no technicians

• A band may perform in many shows, but does not have to perform in any. Each show must have at least one band performing, but may have many bands performing

• For each band, Funky Bizz keeps track of the number of shows that each band performs in.

Page 14: Database Project

MC2 Funky Bizz Operations: ER Diagram

Page 15: Database Project

MC2 Funky Bizz Operations: Relational Schema

* Each band may rent up to 30 instruments

Page 16: Database Project

CREATE TABLE Statements:CREATE TABLE SHOWS ( Venue VARCHAR(25) NOT NULL, Date DATE NOT NULL, Type VARCHAR(25) NOT NULL, Name VARCHAR(128), PRIMARY KEY (Venue , Date));

CREATE TABLE BAND ( BandName VARCHAR(25) NOT NULL, BandID CHAR(3) NOT NULL, Address VARCHAR(128) NOT NULL, ContactName VARCHAR(25) NOT NULL, PRIMARY KEY (BandID), UNIQUE (BandName));

CREATE TABLE BAND_PhoneNo ( PhoneNo VARCHAR(15) NOT NULL, BandID CHAR(3) NOT NULL, PRIMARY KEY (PhoneNo , BandID), FOREIGN KEY (BandID) REFERENCES BAND (BandID));

CREATE TABLE Performs ( BandID CHAR(3) NOT NULL, Venue VARCHAR(25) NOT NULL, Date DATE NOT NULL, PRIMARY KEY (BandID , Venue , Date), FOREIGN KEY (BandID) REFERENCES BAND (BandID), FOREIGN KEY (Venue , Date) REFERENCES SHOWS (Venue , Date));

Page 17: Database Project

CREATE TABLE INSTRUMENT ( InstrumentSN VARCHAR(128) NOT NULL, Model VARCHAR(25) NOT NULL, Brand VARCHAR(25) NOT NULL, Year INT NOT NULL, BandID CHAR(3), PRIMARY KEY (InstrumentSN), FOREIGN KEY (BandID) REFERENCES BAND (BandID));

CREATE TABLE REPAIRTECH ( RTSSN INT NOT NULL, RTName INT NOT NULL, RTAddress INT NOT NULL, PRIMARY KEY (RTSSN)); CREATE TABLE REPAIRTECH_PhoneNo ( RTPhoneNo INT NOT NULL, RTSSN INT NOT NULL, PRIMARY KEY (RTPhoneNo , RTSSN), FOREIGN KEY (RTSSN) REFERENCES REPAIRTECH (RTSSN));

CREATE TABLE Repairs ( InstrumentSN VARCHAR(128) NOT NULL, RTSSN INT NOT NULL, PRIMARY KEY (InstrumentSN , RTSSN), FOREIGN KEY (InstrumentSN) REFERENCES INSTRUMENT (InstrumentSN), FOREIGN KEY (RTSSN) REFERENCES REPAIRTECH (RTSSN));

Page 18: Database Project

BEFORE INSERT / UPDATE TriggerCREATE DEFINER = CURRENT_USER TRIGGER `MC2_Funky_Bizz`.`INSTRUMENT_BEFORE_INSERT` BEFORE INSERT ON `INSTRUMENT` FOR EACH ROW BEGIN

DECLARE totalinstruments INT DEFAULT 0; SELECT count(InstrumentSN) INTO totalinstruments FROM INSTRUMENT WHERE BandID = NEW.BandID; IF (totalinstruments>=30) THEN SET NEW.BandID = NULL; SET NEW.InstrumentSN = NULL; END IF;

END;

Page 19: Database Project

MC2 Funky Bizz Operations in MySQL Workbench

Page 20: Database Project

MC3 Snooty FashionsSnooty Fashions is an exclusive custom fashion designer business. Create the ER diagram for the snooty Fashions Database based on the following requirements . The Fashions Operations Operations Database will keep track of the following:• SSN as For each designer: a unique designer identifier and unique well as the

name (composed of first and last name)• For each customer: a unique customer's identifier as well as his or her name

and multiple phone numbers. • For each tailoring technician: a unique SSN as well as his or her name

(composed of and last name) • For each outfit: a unique outfit's identifier as well as the outfit's planned date

of completion and its unreasonable price

Page 21: Database Project

• For each fashion show: a unique show identifier as well as the date of the show and location • Each designer designs many outfits. • Each outfit has only one designer. Each outfit is sold (in advance) to exactly

one customer. Customers can buy one or many outfits (snooty Fashions will not keep track of customers that have not made any purchases yet) • Each tailoring technician must work on at least one outfit but can work on

many. Each outfit has at least one tailoring technician working on it but can have many snooty Fashions will keep track of the date when a tailoring technician started working on a particular outfit • Each designer can participate in a number of fashion shows, but does not

have to participate in any. Each fashion show can feature one or two Snooty Fashions designers (Snooty Fashions will not keep track of fashion shows that do not feature Snooty Fashions designers).

Page 22: Database Project

MC3 Fashions Operations: ER Diagram

Page 23: Database Project

MC3 Fashions Operations: Relational Schema

*Each fashion show can feature one or two designers

Page 24: Database Project

CREATE TABLE Statements:CREATE DATABASE IF NOT EXISTS MC3_Fashions_Operations; USE MC3_Fashions_Operations;CREATE TABLE IF NOT EXISTS DESIGNER ( DesignerID CHAR(3) NOT NULL, DesSSN CHAR(9) NOT NULL, DesFName VARCHAR(25) NOT NULL, DesLName VARCHAR(25) NOT NULL, PRIMARY KEY (DesignerID), UNIQUE (DesSSN)); CREATE TABLE IF NOT EXISTS FASHIONSHOW ( ShowID CHAR(3) NOT NULL, Date DATE NOT NULL, Location VARCHAR(45) NOT NULL, PRIMARY KEY (ShowID)); CREATE TABLE IF NOT EXISTS Participates ( DesignerID CHAR(3) NOT NULL, ShowID CHAR(3) NOT NULL, PRIMARY KEY (DesignerID , ShowID), FOREIGN KEY (DesignerID) REFERENCES DESIGNER (DesignerID), FOREIGN KEY (ShowID) REFERENCES FASHIONSHOW (ShowID));

Page 25: Database Project

CREATE TABLE IF NOT EXISTS CUSTOMER ( CustID VARCHAR(3) NOT NULL, CustName VARCHAR(45) NOT NULL, PRIMARY KEY (CustID));CREATE TABLE IF NOT EXISTS CUSTOMER_PhoneNo ( PhoneNo VARCHAR(15) NOT NULL, CustID VARCHAR(3) NOT NULL, PRIMARY KEY (PhoneNo , CustID), FOREIGN KEY (CustID) REFERENCES CUSTOMER (CustID)); CREATE TABLE IF NOT EXISTS OUTFIT ( OutfitID CHAR(10) NOT NULL, DateOfCompletion DATE NOT NULL, Price NUMERIC(7 , 2 ) NOT NULL, DesignerID CHAR(3) NOT NULL, CustID VARCHAR(3) NOT NULL, PRIMARY KEY (OutfitID), FOREIGN KEY (DesignerID) REFERENCES DESIGNER (DesignerID), FOREIGN KEY (CustID) REFERENCES CUSTOMER (CustID)); CREATE TABLE IF NOT EXISTS TECHNITIAN ( TailorSSN CHAR(9) NOT NULL, TechLName VARCHAR(25) NOT NULL, TechFName VARCHAR(25) NOT NULL, PRIMARY KEY (TailorSSN)); CREATE TABLE IF NOT EXISTS WorksOn ( StartDate DATE NOT NULL, TailorSSN CHAR(9) NOT NULL, OutfitID CHAR(10) NOT NULL, PRIMARY KEY (TailorSSN , OutfitID), FOREIGN KEY (TailorSSN) REFERENCES TECHNITIAN (TailorSSN), FOREIGN KEY (OutfitID) REFERENCES OUTFIT (OutfitID));

Page 26: Database Project

BEFORE INSERT TriggerCREATE DEFINER = CURRENT_USER TRIGGER `MC3_Fashions_Operations`.`Participates_BEFORE_INSERT` BEFORE INSERT ON `Participates` FOR EACH ROW

BEGIN DECLARE totaldesigners INT DEFAULT 0 ;SELECT COUNT(*) INTO totaldesigners FROM Participates WHERE ShowID = NEW.ShowID; IF (totaldesigners>=2) THENSET NEW.ShowID = NULL;SET NEW.DesignerID = NULL; END IF; END;

Page 27: Database Project

BEFORE UPDATE TriggerCREATE DEFINER = CURRENT_USER TRIGGER `MC3_Fashions_Operations`.`Participates_BEFORE_UPDATE` BEFORE UPDATE ON `Participates` FOR EACH ROW

BEGIN DECLARE totaldesigners INT DEFAULT 0;SELECT COUNT(*) INTO totaldesigners FROM Participates WHERE ShowID = NEW.ShowID; IF (totaldesigners>=2) THEN SET NEW.ShowID = NULL; SET NEW.DesignerID = NULL; END IF; END;

Page 28: Database Project

MC3 Fashions Operations in MySQL Workbench

Page 29: Database Project

MC4 Signum LibriSignum Libri (SL) is a publishing company. Create the ER diagram for the SL Operations Database based on the following requirements. SL operations Database will keep track of the following: • For each book SL publishes: a book name, genre, date of publication and number of pages • For each writer: a unique writer identifier as well as the writer's name • For each agent: a unique agent identifier as well as the agent's name • For each editor: a unique editor identifier as well as the editor's name.• Each SL book is written by one writer, and each writer can write many SL books. SL will not keep

track of writers who did not write a book for SL. All books written by the same writer have a different book name. However, two writers can write two different books with the same book name.

• Each writer is represented by one agent. Each agent represents at least one writer, but can represent many.

• Each book has one editor. Each editor edits at least book, but can one edit many books.• Each editor can mentor one or more other not have editors, but does to mentor any. Each editor

can have at most one mentor editor, but does not have to have any.

Page 30: Database Project

MC4 SL Operations: ER Diagram

Page 31: Database Project

MC4 SL Operations: Relational Schema

* Each editor can have at most one mentor editor

Page 32: Database Project

CREATE TABLE statements:CREATE TABLE AGENT ( AgentID VARCHAR(11) NOT NULL, AgentName VARCHAR(25) NOT NULL, PRIMARY KEY (AgentID));CREATE TABLE WRITER ( WriterID VARCHAR(11) NOT NULL, WriterName VARCHAR(25) NOT NULL, AgentID VARCHAR(11) NOT NULL, PRIMARY KEY (WriterID), FOREIGN KEY (AgentID) REFERENCES AGENT (AgentID));CREATE TABLE EDITOR ( EditorID VARCHAR(11) NOT NULL, EditorName VARCHAR(25) NOT NULL, Mentors_EditorID VARCHAR(11) NULL, PRIMARY KEY (EditorID), FOREIGN KEY (Mentors_EditorID) REFERENCES EDITOR (EditorID)); CREATE TABLE BOOK ( BookName VARCHAR(45) NOT NULL, Genre VARCHAR(25) NOT NULL, DateOfPublication DATE NOT NULL, NoOfPages INT(11) NOT NULL, WriterID VARCHAR(11) NOT NULL, EditorID VARCHAR(11) NOT NULL, PRIMARY KEY (WriterID, BookName), FOREIGN KEY (WriterID) REFERENCES WRITER (WriterID), FOREIGN KEY (EditorID) REFERENCES EDITOR (EditorID));

Page 33: Database Project

INSERT INTO statementsINSERT INTO AGENT (AgentID, AgentName) VALUES ('AG1', 'Genady');INSERT INTO AGENT (AgentID, AgentName) VALUES ('AG2', 'Lola');INSERT INTO AGENT (AgentID, AgentName) VALUES ('AG3', 'Jose');INSERT INTO WRITER (WriterID, WriterName, AgentID) VALUES ('WR1', 'Pascual Lorca', 'AG1');INSERT INTO WRITER (WriterID, WriterName, AgentID) VALUES ('WR2', 'Miguel Topaz', 'AG1');INSERT INTO WRITER (WriterID, WriterName, AgentID) VALUES ('WR3', 'Jannet Jane', 'AG2');INSERT INTO WRITER (WriterID, WriterName, AgentID) VALUES ('WR4', 'Juan Martines', 'AG3');

Page 34: Database Project

INSERT INTO EDITOR (EditorID, EditorName, Mentors_EditorID) VALUES ('ED81', 'Julian', NULL);INSERT INTO EDITOR (EditorID, EditorName, Mentors_EditorID) VALUES ('ED82', 'Marsha', 'ED81');INSERT INTO EDITOR (EditorID, EditorName, Mentors_EditorID) VALUES ('ED83', 'John', NULL);INSERT INTO BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID) VALUES ('Jorney to it', 'Scince', '2013-01-01', 279, 'WR1', 'ED81');INSERT INTO BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID) VALUES ('Having Done', 'Fantazy', '2014-03-04', 670, 'WR1', 'ED82');INSERT INTO BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID) VALUES ('Upheval', 'Drama', '2015-02-05', 350, 'WR2', 'ED83');INSERT INTO BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID) VALUES ('Upheval', 'Comedy', '2010-08-08', 400, 'WR3', 'ED83');

Page 35: Database Project

MC4 SL Operations in MySQL Workbench

Page 36: Database Project

BEFORE INSERT TriggerCREATE DEFINER = CURRENT_USER TRIGGER `MC4_Libri_Operations`.`EDITOR_BEFORE_INSERT` BEFORE INSERT ON `EDITOR` FOR EACH ROWBEGIN

DECLARE totalmentors INT DEFAULT 0 ; SELECT count(*) INTO totalmentorsFROM EDITOR

WHERE Mentors_EditorID = NEW.Mentors_EditorID; IF (totalmentors>=1) THEN

SET NEW.EditorID = NULL; SET NEW.Mentors_EditorID = NULL;

END IF; END;

Page 37: Database Project

BEFORE UPDATE TriggerCREATE DEFINER = CURRENT_USER TRIGGER `MC4_Libri_Operations`.`EDITOR_BEFORE_UPDATE` BEFORE UPDATE ON `EDITOR` FOR EACH ROWBEGIN

DECLARE totalmentors INT DEFAULT 0 ; SELECT count(*) INTO totalmentorsFROM EDITOR WHERE Mentors_EditorID = NEW.Mentors_EditorID; IF (totalmentors>=1) THENSET NEW.EditorID = NULL; SET NEW.Mentors_EditorID = NULL;END IF;

END;

Page 38: Database Project

MC6 Jones DozersJones Dozers is a construction equipment company.

• Map the ER diagram for the Jones Dozers Sales and Rentals Database into a relational schema.

Page 39: Database Project

MC6 JD Sales and Rentals: ER Diagram

Page 40: Database Project

MC6_JD Sales_Rentals: Relational Schema

* Each sales representative can be a protégé to maximum 1, and can mentor up to 3 other representatives

Page 41: Database Project

MC6_JD_Sales_Rentals in MySQL Workbench

Page 42: Database Project

MC6 Jones Dozers Data WarehouseJones Dozers wants to create an analytical database to analyze its sales and rentals revenue. The only available data source is the Jones Sales and Rentals Database (depicted by the ER diagram and the relational schema presented above. Create a dimensional model for a data warehouse that enables analysis of sales and rentals revenue by:• date type of revenue (sale or rental) • customer • equipment • sales rep. Each row in the fact table will represent the monetary amount of revenue taken in during one sale or rental transaction.

Page 43: Database Project

MC6 JD Sales Rentals: Star Schema

Page 44: Database Project

MC6 Star Schema in MySQL Workbench

Page 45: Database Project

BEFORE UPDATE / INSERT TriggerCREATE DEFINER = CURRENT_USER TRIGGER `JD_Sales_Rentals`.`SALESREP_BEFORE_INSERT` BEFORE INSERT ON `SALESREP` FOR EACH ROW BEGIN

DECLARE totalproteges, totalmentors INT DEFAULT 0; SET totalproteges = (SELECT count(Mentors_SRepID) FROM

JD_Sales_Rentals.SALESREP WHERE SRepID = NEW.SRepID); SELECT count(SRepID) FROM JD_Sales_Rentals.SALESREP WHERE

Mentors_SRepID = NEW.Mentors_SRepID INTO totalmentors; IF (totalproteges>=3 OR totalmentors >=1) THEN

SET NEW.SRepID = NULL; END IF;

END;

Page 46: Database Project

ETL INSERT INTO StatementsINSERT INTO JD_SR_Dimensional.CALENDAR (FullDate, DayOfWeek, DayOfMonth, MONTH, Quarter, YEAR)SELECT DISTINCT Date AS FullDate, DAYOFWEEK(Date) AS DayOfWeek, dayofmonth(Date) AS DayOfMonth, month(Date) AS MONTH, quarter(Date) AS Qtr, year(Date) AS YEAR FROM JD_Sales_Rentals.RENTAL UNION SELECT DISTINCT Date AS FullDate, DAYOFWEEK(Date) AS DayOfWeek, dayofmonth(Date) AS DayOfMonth, month(Date) AS MONTH, quarter(Date) AS Qtr, year(Date) AS YEAR FROM JD_Sales_Rentals.SALE;INSERT INTO JD_SR_Dimensional.CUSTOMER (CustID, CustName, CustCategory) SELECT *FROM JD_Sales_Rentals.CUSTOMER;INSERT INTO JD_SR_Dimensional.EQUIPMENT (SerialNo, LastInspectDate, DateMade, Make, Type, Model) SELECT E.SerialNo, E.LastInspectDate, E.DateMade, ED.Make, ED.Type, ED.Model FROM JD_Sales_Rentals.EQUIPMENT E, JD_Sales_Rentals.EQUIPMENTDETAIL ED WHERE E.EquipDetailID = ED.EquipDetailID;INSERT INTO JD_SR_Dimensional.REVTYPE (RevType) VALUES ('rental'), ('sale');

Page 47: Database Project

INSERT INTO JD_SR_Dimensional.SALESREP (SRepID, SRepFName, SRepLName, Rank, NoOfProteges, NoOfMentors)SELECT t1.SRepID, t1.SRepFName, t1.SRepLName, t1.Rank, count(t1.Mentors_SRepID) as NoOfProteges, count(t2.SRepID) as NoOfMentors FROM JD_Sales_Rentals.SALESREP t1 LEFT JOIN JD_Sales_Rentals.SALESREP t2 ON t1.SRepID=t2.Mentors_SRepIdGROUP BY SRepID;INSERT INTO JD_SR_Dimensional.FACT (CalendarKey, RevTypeKey, CustomerKey, EquipmentKey, SRepKey, TID, RevenueAmount) SELECT C.CalendarKey, RT.RevTypeKey, CU.CustomerKey, EQ.EquipmentKey, SR.SRepKey, R.RentTransID, R.Price FROM JD_SR_Dimensional.CALENDAR AS C, JD_SR_Dimensional.REVTYPE AS RT, JD_SR_Dimensional.CUSTOMER AS CU, JD_SR_Dimensional.EQUIPMENT AS EQ, JD_SR_Dimensional.SALESREP AS SR, JD_Sales_Rentals.RENTAL AS R WHERE R.Date = C.FullDate AND RT.RevType = 'rental' AND R.CustID = CU.CustID AND R.SerialNo = EQ.SerialNo AND R.SRepID = SR.SRepID UNION SELECT C.CalendarKey, RT.RevTypeKey, CU.CustomerKey, EQ.EquipmentKey, SR.SRepKey, S.SaleTransID, S.Price FROM JD_SR_Dimensional.CALENDAR AS C, JD_SR_Dimensional.REVTYPE AS RT, JD_SR_Dimensional.CUSTOMER AS CU, JD_SR_Dimensional.EQUIPMENT AS EQ, JD_SR_Dimensional.SALESREP AS SR, JD_Sales_Rentals.SALE AS S WHERE S.Date = C.FullDate AND RT.RevType = 'sale' AND S.CustID = CU.CustID AND S.SerialNo = EQ.SerialNo AND S.SRepID = SR.SRepID;

Page 48: Database Project

MC7 Midtown MemorialMidtown Memorial is a hospital.

• Map the ER diagram for the Midtown Memorial Patients Drug Dispense Database into a relational schema.

Page 49: Database Project

MC7 Patients Drug Dispense: ER Diagram

Page 50: Database Project

MC7 Patients Drug Dispense: Relational Schema

*

Page 51: Database Project

BEFORE UPDATE / INSERT TriggerCREATE DEFINER = CURRENT_USER TRIGGER `MC7_Patients_Drug_Dispensal`.`Includes_BEFORE_INSERT` BEFORE INSERT ON `Includes` FOR EACH ROW BEGIN

DECLARE t INT DEFAULT 0; SELECT count(*) into t FROM

MC7_Patients_Drug_Dispensal.NotToTakeWith WHERE (DrugID_1 = NEW.DrugID AND NotTotakeWithDrugID_2 IN (SELECT DrugID FROM MC7_Patients_Drug_Dispensal.Includes WHERE DIENumber = NEW.DIENumber and PatientID = NEW.PatientID)) or (NotToTakeWithDrugID_2 = NEW.DrugID AND DrugID_1 IN (SELECT DrugID FROM MC7_Patients_Drug_Dispensal.Includes WHERE DIENumber = NEW.DIENumber and PatientID = NEW.PAtientID));

IF (t>0) THENSET NEW.DrugID = NULL;

END IF;END;

Page 52: Database Project

MC7 Patients Drug Dispense in MySQL Workbench

Page 53: Database Project

MC7 Midtown Memorial Data WarehouseMidtown Memorial wants to create an analytical database to analyze drug ingredient intake by their patients. The only avail- able data source is the Midtown Memorial Patients Drug Dispensal Database and the relational schema depicted above. Create a dimensional model for a data warehouse that enables analysis of ingredient intake events by: • date • time • patient • nurse • drug • ingredient. Each row in the fact table will represent the amount (calculated as: quantity x dosage) of one ingredient taken by one patient during one drug intake event.

Page 54: Database Project

MC7 Drug Ingredient Intake: Star Schema

Page 55: Database Project

MC7 Star Schema in MySQL Workbench

Page 56: Database Project

ETL INSERT INTO StatementsINSERT INTO MC7_Ingredient_Intake.CALENDAR (FullDate, DayOfWeek, DayOfMonth, Month, Qtr, YEAR)SELECT DISTINCT DIEDate AS FullDate, DAYOFWEEK(DIEDate) AS DayOfWeek, dayofmonth(DIEDate) AS DayOfMonth, month(DIEDate) AS MONTH, quarter(DIEDate) AS Qtr, year(DIEDate) AS YEAR FROM MC7_Patients_Drug_Dispensal.DRUGINTAKEEVENT;INSERT INTO MC7_Ingredient_Intake.PATIENT (PatientID, PatientLName, PatientFName, PatientGender, PatientBDate) SELECT *FROM MC7_Patients_Drug_Dispensal.PATIENT;INSERT INTO MC7_Ingredient_Intake.NURSE (NurseID, NurseName)SELECT *FROM MC7_Patients_Drug_Dispensal.NURSE;INSERT INTO MC7_Ingredient_Intake.DRUG (DrugID, DrugName, DrugCoatingType) SELECT *FROM MC7_Patients_Drug_Dispensal.DRUG;INSERT INTO MC7_Ingredient_Intake.INGREDIENT (IngID, IngName) SELECT *FROM MC7_Patients_Drug_Dispensal.INGREDIENT;

Page 57: Database Project

INSERT INTO MC7_Ingredient_Intake.DRUG_INTAKE_EVENT (CalendarKey, PatientKey, NurseKey, DrugKey, IngredientKey, DIENumber, QuantityxDosage, DIETime)SELECT C.CalendarKey, P.PatientKey, N.NurseKey, D.DrugKey, I.IngredientKey, DIE.DIENumber, INC.Quantity * CON.Dosage, DIE.DIETime FROM MC7_Ingredient_Intake.CALENDAR AS C, MC7_Ingredient_Intake.PATIENT AS P, MC7_Ingredient_Intake.NURSE AS N, MC7_Ingredient_Intake.DRUG AS D, MC7_Ingredient_Intake.INGREDIENT AS I, MC7_Patients_Drug_Dispensal.DRUGINTAKEEVENT AS DIE, MC7_Patients_Drug_Dispensal.Includes AS INC, MC7_Patients_Drug_Dispensal.Contain AS CON WHERE DIE.DIEDate = C.FullDate AND DIE.PatientID = P.PatientID AND DIE.NurseID = N.NurseID AND INC.DrugID = D.DrugID AND CON.IngID = I.IngID AND CON.DrugID = INC.DrugID AND INC.DIENumber = DIE.DIENumber AND INC.PatientID = DIE.PatientID ORDER BY DIENumber, DrugKey;

Page 58: Database Project

References:Jukic N., Vrbsky S., Nestorov S. “Database Systems. Introduction to Databases and Data Warehouses”. Pearson Education Inc., 2014.