You are on page 1of 8

APPENDIX B

1. COMMANDS USED TO CREATE SCHEMA


The commands used to create our database schema were CREATE TABLE and CREATE VIEW: CREATE TABLE customer( customer_ID CHAR(5) NOT NULL CHECK(customer_ID BETWEEN 10000 AND 99999), CLastName VARCHAR2(15) NOT NULL, CFirstName VARCHAR2(15) NOT NULL, Phone CHAR(10) NOT NULL, CStreet_Address VARCHAR2(90) NOT NULL, CCity VARCHAR2(45) DEFAULT 'Philadelphia', Gender CHAR(1) NOT NULL CHECK(Gender in ('M','m','F','f')), PRIMARY KEY(customer_ID) ); CREATE TABLE Insurance_Plan( Plan_No CHAR(5) NOT NULL CHECK(Plan_No BETWEEN 10000 AND 99999), Carrier VARCHAR2(20) NOT NULL, Policy_Name VARCHAR2(20) NOT NULL, PRIMARY KEY (Plan_No) ); CREATE TABLE Doctor( doctor_ID CHAR(5) NOT NULL CHECK(doctor_ID BETWEEN 10000 AND 99999), DLastName VARCHAR2(25) NOT NULL, DFirstName VARCHAR2(25) NOT NULL, DStreet_Address VARCHAR2(90) NOT NULL, DCity VARCHAR(45) DEFAULT 'Philadelphia', PRIMARY KEY(doctor_ID) ); CREATE TABLE Drug( Drug_ID CHAR(5) NOT NULL CHECK(Drug_ID BETWEEN 10000 AND 99999), Drug_Name VARCHAR2(15) NOT NULL, PRIMARY KEY (Drug_ID) ); CREATE TABLE Pharmacist ( ID_No CHAR(5) NOT NULL CHECK(ID_No BETWEEN 10000 AND 99999), PLastName VARCHAR2(25) NOT NULL, PFirstName VARCHAR2(25) NOT NULL, Hours NUMBER(3,1) NOT NULL CHECK(Hours BETWEEN 0 AND 65.0), Salary NUMBER(8,2) NOT NULL CHECK(Salary BETWEEN 0 AND 150000.00), PRIMARY KEY(ID_No) ); CREATE TABLE Covered_By( PlanN CHAR(5), Cust_ID CHAR(5), PRIMARY KEY(PlanN, Cust_ID), FOREIGN KEY(PlanN) REFERENCES Insurance_Plan(Plan_No) ON DELETE CASCADE,

FOREIGN KEY(Cust_ID) REFERENCES customer(customer_ID) ON DELETE CASCADE ); CREATE TABLE covers ( PlanN CHAR(5), DrugN CHAR(5) NOT NULL CHECK(DrugN BETWEEN 10000 AND 99999), PRIMARY KEY(PlanN, DrugN), FOREIGN KEY(PlanN) REFERENCES Insurance_Plan(Plan_No) ON DELETE CASCADE, FOREIGN KEY(DrugN) REFERENCES Drug(Drug_ID) ON DELETE CASCADE ); CREATE TABLE contraindication ( Drug1 CHAR(5) NOT NULL CHECK(Drug1 BETWEEN 10000 AND 99999), Drug2 CHAR(5) NOT NULL CHECK(Drug2 BETWEEN 10000 AND 99999), PRIMARY KEY(DRUG1), FOREIGN KEY(Drug1) REFERENCES Drug(Drug_ID), FOREIGN KEY(Drug2) REFERENCES Drug(Drug_ID) ); CREATE TABLE prescription ( ID_No CHAR(5) NOT NULL CHECK(ID_No BETWEEN 10000 AND 99999), P_ID CHAR(5) NOT NULL CHECK(P_ID BETWEEN 10000 AND 99999), D_ID CHAR(5) NOT NULL CHECK(D_ID BETWEEN 10000 AND 99999), Drug CHAR(5) NOT NULL CHECK(Drug BETWEEN 10000 AND 99999), C_ID CHAR(5) NOT NULL CHECK(C_ID BETWEEN 10000 AND 99999), PRIMARY KEY (ID_No), FOREIGN KEY (P_ID) REFERENCES Pharmacist(ID_No) ON DELETE CASCADE, FOREIGN KEY(D_ID) REFERENCES Doctor(doctor_ID), FOREIGN KEY(Drug) REFERENCES Drug(Drug_ID) ON DELETE CASCADE, FOREIGN KEY(C_ID) REFERENCES customer(customer_ID) ON DELETE CASCADE ); CREATE VIEW femaleCustomers AS SELECT CLastName, CFirstName FROM customer WHERE (gender = 'F' OR gender = 'f') ORDER BY CLastName; CREATE VIEW partTimePharmacists AS SELECT PLastName, PFirstName, Salary FROM pharmacist WHERE (hours < 40.0) ORDER BY PLastName; CREATE VIEW uninsuredCustomers AS SELECT c.CLastName, c.CFirstName FROM customer c WHERE NOT EXISTS (select b.Cust_ID FROM Covered_By b WHERE c.customer_id = b.Cust_ID) ORDER BY c.CLastName; CREATE VIEW customersInPhiladelphia AS SELECT CLastName, CFirstName FROM customer WHERE (Ccity = 'Philadelphia, PA') ORDER BY CLastName; CREATE VIEW custTakingCDrug AS SELECT DISTINCT c.CLastName, c.CFirstName FROM customer c, contraindication co, prescription p1, prescription p2 WHERE c.customer_ID = p1.C_ID AND c.customer_ID = p2.C_ID AND ((p1.drug = co.drug1 AND p2.drug = co.drug2) OR (p2.drug = co.drug1 AND p1.drug = co.drug2)); CREATE VIEW patientDoctor AS SELECT DISTINCT c.CLastName AS "Last Name", c.CFirstName as "First Name", DISTINCT d.DLastName AS "Doctor" FROM doctor d, customer c, prescription p WHERE d.doctor_ID = p.D_ID AND c.customer_ID = p.C_ID ORDER BY c.CLastName, c.CFirstName, d.DLastName; CREATE VIEW numPrescriptions AS SELECT DISTINCT CLastName AS LastName, CFirstName AS FirstName, COUNT(*) OVER (PARTITION BY c.customer_id) AS numPrscpt FROM customer c JOIN prescription p ON c.customer_id = p.c_id; CREATE VIEW drugCoverage AS SELECT i.carrier AS Carrier, i.policy_name AS Plan, d.drug_name AS Drug FROM drug d, insurance_plan i, covers c WHERE d.drug_id = c.drugN AND i.plan_no = c.planN ORDER BY i.carrier, i.policy_name, d.drug_name;

CREATE VIEW patientCoverage AS SELECT c.CLastName AS "Last Name", c.CFirstName AS "First Name", i.carrier AS "Carrier", i.policy_name AS "Plan" FROM customer c, insurance_plan i, covered_by p WHERE c.customer_id = p.cust_ID AND i.plan_no = p.planN ORDER BY c.CLastName, c.CFirstName, i.carrier, i.policy_name;

SQL> describe customer Name Null? Type ----------------------------------------- -------- -----------CUSTOMER_ID CLASTNAME CFIRSTNAME PHONE CSTREET_ADDRESS CCITY GENDER NOT NOT NOT NOT NOT CHAR(5) VARCHAR2(15) VARCHAR2(15) CHAR(10) VARCHAR2(90) VARCHAR2(45) NOT NULL CHAR(1) NULL NULL NULL NULL NULL

SQL> describe Insurance_Plan Name Null? Type ----------------------------------------- -------- -----------PLAN_NO CARRIER POLICY_NAME NOT NULL CHAR(5) NOT NULL VARCHAR2(20) NOT NULL VARCHAR2(20)

SQL> describe doctor Name Null? Type ----------------------------------------- -------- -----------DOCTOR_ID DLASTNAME DFIRSTNAME DSTREET_ADDRESS DCITY NOT NOT NOT NOT NULL NULL NULL NULL CHAR(5) VARCHAR2(25) VARCHAR2(25) VARCHAR2(90) VARCHAR2(45)

SQL> describe drug Name Null? Type ----------------------------------------- -------- -----------DRUG_ID DRUG_NAME NOT NULL CHAR(5) NOT NULL VARCHAR2(15)

SQL> describe pharmacist Name Null? Type ----------------------------------------- -------- -----------ID_NO PLASTNAME PFIRSTNAME HOURS SALARY SQL> describe covered_by Name NOT NOT NOT NOT NOT NULL NULL NULL NULL NULL CHAR(5) VARCHAR2(25) VARCHAR2(25) NUMBER(3,1) NUMBER(8,2) Type

Null?

----------------------------------------- -------- -----------PLANN CUST_ID NOT NULL CHAR(5) NOT NULL CHAR(5)

SQL> describe covers Name Null? Type ----------------------------------------- -------- -----------PLANN DRUGN NOT NULL CHAR(5) NOT NULL CHAR(5)

SQL> describe contraindication Name Null? Type ----------------------------------------- -------- -----------DRUG1 DRUG2 NOT NULL CHAR(5) NOT NULL CHAR(5)

SQL> describe prescription Name Null? Type ----------------------------------------- -------- -----------ID_NO P_ID D_ID DRUG C_ID NOT NOT NOT NOT NOT NULL NULL NULL NULL NULL CHAR(5) CHAR(5) CHAR(5) CHAR(5) CHAR(5)

2. CONTRAINT DEFINITIONS
Unfortunately, on our version of Oracle, the user_constraints table is huge and unwieldy. Because all of our constraints are in our CREATE TABLE statements, which are included in our documentation, I will instead simply explain which parts of the CREATE TABLE commands are constraints. The required data constraints are NOT NULL or a data type. The entity integrity constraints are all the PRIMARY KEY declarations. The referential integrity constraints are the foreign key declarations. The domain constraints are all in the form of CHECK() functions.

3. QUERIES
CREATE VIEW femaleCustomers AS SELECT CLastName, CFirstName FROM customer WHERE (gender = 'F' OR gender = 'f') ORDER BY CLastName; CREATE VIEW partTimePharmacists AS SELECT PLastName, PFirstName, Salary FROM pharmacist WHERE (hours < 40.0) ORDER BY PLastName; CREATE VIEW uninsuredCustomers AS SELECT c.CLastName, c.CFirstName FROM customer c WHERE NOT EXISTS (select b.Cust_ID FROM Covered_By b WHERE c.customer_id = b.Cust_ID) ORDER BY c.CLastName; CREATE VIEW customersInPhiladelphia AS SELECT CLastName, CFirstName FROM customer WHERE (Ccity = 'Philadelphia, PA') ORDER BY CLastName; CREATE VIEW custTakingCDrug AS SELECT DISTINCT c.CLastName, c.CFirstName FROM customer c, contraindication co, prescription p1, prescription p2 WHERE c.customer_ID = p1.C_ID AND c.customer_ID = p2.C_ID AND ((p1.drug = co.drug1 AND p2.drug = co.drug2) OR (p2.drug = co.drug1 AND p1.drug = co.drug2)); CREATE VIEW patientDoctor AS SELECT DISTINCT c.CLastName AS "Last Name", c.CFirstName as "First Name", d.DLastName AS "Doctor" FROM doctor d, customer c, prescription p WHERE d.doctor_ID = p.D_ID AND c.customer_ID = p.C_ID ORDER BY c.CLastName, c.CFirstName, d.DLastName; CREATE VIEW numPrescriptions AS SELECT DISTINCT CLastName AS LastName, CFirstName AS FirstName, COUNT(*) OVER (PARTITION BY c.customer_id) AS numPrscpt FROM customer c JOIN prescription p ON c.customer_id = p.c_id; CREATE VIEW drugCoverage AS SELECT i.carrier AS Carrier, i.policy_name AS Plan, d.drug_name AS Drug FROM drug d, insurance_plan i, covers c WHERE d.drug_id = c.drugN AND i.plan_no = c.planN ORDER BY i.carrier, i.policy_name, d.drug_name; CREATE VIEW patientCoverage AS SELECT c.CLastName AS "Last Name", c.CFirstName AS "First Name", i.carrier AS "Carrier", i.policy_name AS "Plan" FROM customer c, insurance_plan i, covered_by p WHERE c.customer_id = p.cust_ID AND i.plan_no = p.planN ORDER BY c.CLastName, c.CFirstName, i.carrier, i.policy_name; SQL> select * from femaleCustomers; CLASTNAME --------------Finney Patel Stark Stony CFIRSTNAME --------------Narnia Shelby Myrna Debra

SQL> select * from partTimePharmacists; PLASTNAME ------------------------Barrel Shaftman PFIRSTNAME SALARY ------------------------- ---------Jeena 100000 Tom 60000

SQL> select * from uninsuredCustomers; CLASTNAME --------------Patel Touchdown CFIRSTNAME --------------Shelby Cheese

SQL> select * from customersInPhiladelphia; CLASTNAME --------------Beavert Black Fortnight Jordan Patel CFIRSTNAME --------------Harry Noah Noah Harry Shelby

SQL> select * from custTakingCDrug; CLASTNAME CFIRSTNAME --------------- --------------Patel Shelby 6 rows selected. SQL> select * from patientDoctor; Last Name --------------Beavert Black Finney Germany Jordan Patel Patel Smith Stark Stony Touchdown First Name --------------Harry Noah Narnia Conner Harry Shelby Shelby Ralph Myrna Debra Cheese Doctor ------------------------Steinberg Birnbaum Bucket Smith Birnbaum Blumenthal Bucket Bucket Blumenthal Bucket Smith

11 rows selected. SQL> select * from numPrescriptions; LASTNAME --------------Black Finney Patel Stark Jordan Smith Beavert Touchdown Stony Germany FIRSTNAME NUMPRSCPT --------------- ---------Noah 1 Narnia 1 Shelby 6 Myrna 2 Harry 1 Ralph 2 Harry 1 Cheese 1 Debra 1 Conner 1

10 rows selected. SQL> select * from drugCoverage; CARRIER PLAN DRUG -------------------- -------------------- --------------Drugzco Drugzco Cheap Plan Berkowitz

Drugzco Formutra Formutra Formutra Formutra Formutra Weird Shield Weird Shield Weird Shield 10 rows selected.

Drugzco Cheap Plan Alliance Mediminus Alliance Mediminus Alliance Mediminus Alliance Mediplus Alliance Mediplus Blue Plan Plus Package Plus Package

Ramefrin Hard Tylenol Lazerol Rexiprim Corzia Stumapan Chalakol Basketol Chozerex

SQL> select * from patientCoverage; Last Name --------------Bali Beavert Black Finney Fortnight Germany Jordan Smith Stark Stony 10 rows selected. Query femaleCustomers generates a table of the names of all female customers. Query partTimePharmacists generates a table of the names all pharmacists whose weekly hours are fewer than 40. Query uninsuredCustomers generates a table of the names of uninsured customers. It finds all customer ID numbers which do not appear in the covered_By table, which tracks insurance coverage for customers. Query customersInPhiladelphia generates a table of the names of customers whose "city" value is Philadelphia, PA. Query custTakingCDrug generates a table of the customers whose prescriptions would appear to contraindicate one another. This query would be run routinely to insure that no customers are taking dangerous combinations of drug. Query patientDoctor generates a table of all patient's name, with an additonal column containing the patient's doctor. The doctor value is obtained by checking for prescriptions for the customer, and checking the doctor value from the doctor table. Query numPrescriptions generates a table with a count of the number of prescriptions on file for every customer. Query drugCoverage generates a table with complete drug coverage information for all insurance plans. For clarity, rows are grouped by the carrier's name, followed by the plan's name, followed by the covered drug. Query patientCoverage generates a table with a row for each customer, with columns for their name, their insurance carrier, and their plan. First Name --------------Johnny Harry Noah Narnia Noah Conner Harry Ralph Myrna Debra Carrier -------------------Drugzco Drugzco Formutra Formutra Formutra Formutra Weird Shield Weird Shield Formutra Formutra Plan -------------------Drugzco Cheap Plan Drugzco Cheap Plan Alliance Mediplus Alliance Mediminus Alliance Mediminus Alliance Mediplus Plus Package Blue Plan Alliance Mediplus Alliance Mediminus

4. DATA MODIFICATION COMMANDS


DELETE FROM customer WHERE CFirstName = 'Noah'; DELETE FROM customer c WHERE EXISTS (SELECT * FROM prescription p WHERE c.customer_id = p.C_ID AND p.drug = '12345'); UPDATE customer SET gender = 'F'; UPDATE Doctor SET DCity = 'Phila' WHERE DCity = 'Philadelphia'; UPDATE Pharmacist SET salary = 20000.00 WHERE hours < 40; DELETE FROM Pharmacist p WHERE EXISTS (SELECT * FROM prescription p1, prescription p2, contraindication c WHERE p.ID_NO = p1.P_ID AND p.ID_NO = p2.P_ID AND (((p1.drug = c.drug1) AND (p2.drug = c.drug2)) OR ((p2.drug = c.drug1) AND (p1.drug = c.drug2)));

During our mapped translation of our ERD, we were careful not to include any attributes in our entities which depended on any other attribute in the table besides the key value. The key value, in the case of all of our entities, was a 5 digit identification number. As a result, well-normalized table was the natural result of our database creation process. There is no unnecessary duplication of data in our database.

You might also like