Sie sind auf Seite 1von 10

-- ------------------------------------------------------------------------------------------------------------------------------------------------------- Casey Hargarther Project 2 LIS3784 - Information, Organization and Communication Fall 2014 - Dr.

Mark K. Jowett - Florida State University


-- ------------------------------------------------------------------------------------------------------------------------------------------------------

SET ANSI_WARNINGS ON;


GO

use master;
GO

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'cbh12e')


DROP DATABASE cbh12e;
GO

IF NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'cbh12e')


CREATE DATABASE cbh12e;
GO

use cbh12e;
GO

-- ------------------------------ Table dbo.patient


-- -----------------------------

IF OBJECT_ID (N'dbo.patient', N'U') IS NOT NULL


DROP TABLE dbo.patient;

GO

CREATE TABLE dbo.patient


(
pat_id SMALLINT NOT NULL identity(1,1),
pat_ssn INT NOT NULL check (app_ssn > 0 and app_ssn <=999999999),
pat_fname VARCHAR(15) NOT NULL,
pat_lname VARCHAR(30) NOT NULL,
pat_street VARCHAR(30) NOT NULL,
pat_city VARCHAR(30) NOT NULL,
pat_state CHAR(2) NOT NULL DEFAULT 'FL',
pat_zip INT NOT NULL check (app_zip >0 and app_zip <=999999999),
pat_phone BIGINT NOT NULL CHECK (pat_phone > 0 and pat_phone <=9999999999),
pat_email VARCHAR(100) NULL,
pat_dob DATE NOT NULL,
pat_gender CHAR(1) NOT NULL CHECK (app_gender IN('m','f')),
pat_notes VARCHAR(255) NULL,
PRIMARY KEY (pat_id),

CONSTRAINT ux_pat_ssn unique nonclustered (pat_ssn ASC),


);

-- ----------------------------- Table dbo.physician


-- ----------------------------

IF OBJECT_ID (N'dbo.physician', N'U') IS NOT NULL


DROP TABLE dbo.physician;

CREATE TABLE dbo.physician


(
phy_id SMALLINT NOT NULL identity(1,1),
phy_specialty VARCHAR(25) NOT NULL,
phy_fname VARCHAR(15) NOT NULL,
phy_lname VARCHAR(30) NOT NULL,
phy_street VARCHAR(30) NOT NULL,
phy_city VARCHAR(30) NOT NULL,
phy_state CHAR(2) NOT NULL DEFAULT 'FL',
phy_zip INT NOT NULL check (prp_zip >0 and prp_zip <=999999999),
phy_phone BIGINT NOT NULL CHECK (phy_phone > 0 and phy_phone <=9999999999),
phy_fax BIGINT NOT NULL CHECK (phy_fax > 0 and phy_fax <=9999999999),
phy_email VARCHAR(100) NOT NULL,
phy_url VARCHAR(100) NOT NULL,
phy_notes VARCHAR(255) NULL,
PRIMARY KEY (phy_id)
);

------------------------------ Table dbo.prescription


-----------------------------

IF OBJECT_ID (N'dbo.prescription',N'U') IS NOT NULL


DROP TABLE dbo.prescription;

CREATE TABLE dbo.prescription


(
pre_id SMALLINT NOT NULL identity(1,1),

pat_id SMALLINT NOT NULL,


med_id SMALLINT NOT NULL,
pre_date DATE NOT NULL,
pre_dosage VARCHAR(255) NOT NULL,
pre_num_refills VARCHAR(3) NOT NULL,
pre_notes VARCHAR(255) NULL,
PRIMARY KEY (pre_id),

CONSTRAINT ux_pat_id_pre_id_pre_date unique nonclustered


(pat_id ASC, med_id ASC, pre_date ASC),

CONSTRAINT pk_prescription_patient
FOREIGN KEY (pat_id)
REFERENCES dbo.patient (pat_id)
ON DELETE CASCADE
ON UPDATE CASCADE,

CONSTRAINT fk_prescription_medication
FOREIGN KEY (med_id)
REFERENCES dbo.medication (med_id)
ON DELETE CASCADE
ON UPDATE CASCADE
);

-- ------------------------------- Table dbo.administration_lu


-- ------------------------------

IF OBJECT_ID (N'dbo.administration_lu', N'U') IS NOT NULL

DROP TABLE dbo.administration_lu;

CREATE TABLE dbo.administration_lu


(
pre_id SMALLINT NOT NULL identity (1,1),
ptr_id SMALLINT NOT NULL identity (1,1),
PRIMARY KEY (trt_id,ptr_id)
);

-- ------------------------------- Table dbo.medication


-- -----------------------------IF OBJECT_ID (N'dbo.medication', N'U') IS NOT NULL
DROP TABLE dbo.medication;

CREATE TABLE dbo.medication


(
med_id SMALLINT NOT NULL identity(1,1),
med_name VARCHAR(100) NOT NULL,
med_price DECIMAL(5,2) NOT NULL CHECK (med_price > 0),
med_shelf_life DATE NOT NULL,
med_notes VARCHAR(255) NULL,
PRIMARY KEY (med_id)
);

-- ----------------------------- Table patient_treatment


-- ---------------------------IF OBJECT_ID (N'dbo.patient_treatment', N'U') IS NOT NULL

DROP TABLE dbo.patient_treatment;

CREATE TABLE dbo.patient_treatment


(
ptr_id SMALLINT NOT NULL identity(1,1),
pat_id SMALLINT NOT NULL,
phy_id SMALLINT NOT NULL,
trt_id SMALLINT NOT NULL,
ptr_date DATE NOT NULL,
ptr_start TIME NOT NULL,
ptr_end TIME NOT NULL
ptr_results VARCHAR(255) NOT NULL,
rom_notes VARCHAR(255) NULL,
PRIMARY KEY (ptr_id),

CONSTRAINT patient_treatment_patient
FOREIGN KEY (pat_id)
REFERENCES dbo.patient (pat_id)
ON DELETE CASCADE
ON UPDATE CASCADE,

CONSTRAINT fk_patient_treatment_physician
FOREIGN KEY (phy_id)
REFERENCES dbo.physician (phy_id)
ON DELETE CASCADE
ON UPDATE CASCADE,

CONSTRAINT fk_patient_treatment_treatment
FOREIGN KEY (trt_id)

REFERENCES dbo.treatment (trt_id)


ON DELETE CASCADE
ON UPDATE CASCADE
);

-- ----------------------------- Table dbo.treatment


-- ---------------------------IF OBJECT_ID (N'dbo.treatment', N'U') IS NOT NULL
DROP TABLE dbo.treatment;

CREATE TABLE dbo.treatment


(
trt_id SMALLINT NOT NULL identity(1,1),
trt_name VARCHAR(255) NOT NULL,
trt_price DECIMAL(5,2) NOT NULL CHECK (trt_price > 0),
trt_notes VARCHAR(255) NULL,
PRIMARY KEY (trt_id),
);

SELECT * FROM information_schema.tables;

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

SELECT * FROM information_schema.tables;

EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

-- ------------------------------------- Data for table dbo.patient


-- -----------------------------------INSERT INTO dbo.patient
(pat_ssn, pat_fname, pat_lname, pat_street, pat_city, pat_zip, pat_phone, pat_email, pat_dob,
pat_gender, pat_notes)

VALUES
('123456789','Ryan','Roe','111 E Las Olas Blvd','Fort
Lauderdale','FL','333011234','9545551234','rroe@gmail.com','1993-03-25','M',NULL),
('178923219','Lindsay','Smith','90 High Road','Tallahassee','FL','323071234',
'8505557209','lindsay@lindsaysmith.com','1980-05-28','F',NULL),
('103239129','Charlie','Smith','1090 Lynn Haven Blvd','Lynn
Haven','FL','324371234','8505551234','me@charliesmith.com','1955-09-19','M',NULL),
('123894639','John','Doe','40 Orange St','St
Augustine','FL','320841234','9045558300','johndoe1021@gmail.com','1974-08-07','M',NULL),
('123532109','Jane','Doe','1201 W. Cypress Creek Blvd','Fort
Lauderdale','FL','333090000','9549583635','jdoe@cruiseone.com','1970-04-25','F',NULL);

-- --------------------------------------- Data for table dbo.physician


-- -------------------------------------INSERT INTO dbo.physician
(phy_specialty,phy_fname,phy_lname, phy_street, phy_city, phy_state, phy_zip, phy_phone, phy_fax,
phy_email, phy_url, phy_notes)

VALUES
('Nueology Medicine', 'Ellen', 'Doe', '1201 W. Cypress Creek Blvd', 'Fort
Lauderdale','FL','33091234','9545551345','9545551354','edoe@cruiseone.com','http://www.cruiseone.c
om/jdoe',NULL)

('Addictive Medicine', 'Tina', 'Smith','1720 Monroe St','Tallahassee','FL', '323011234', '8505551234',


'8505552234', 'tina@aol.com', 'http://www.tinasmithmd.com',NULL),
('Internal Medicine', 'Sandy', 'Doe','600 W. College Ave','Tallahassee','FL', '323061111', '8506441234',
'8505558513', 'sandysmith@yahoo.com', 'http://www.fsu.edu',NULL),
('Addictive Medicine', 'Amy', 'Jowett','123 Main St','Orlando','FL', '328041245', '4045551010',
'4045552020', 'amy@qtir.com', 'http://www.qtir.com',NULL),
('Sports Medicine', 'Sean', 'Lawson','5119 Tampa Blvd','Tampa','FL', '336031223', '8135551010',
'8135553030', 'slawson@gmail.com', 'http://www.slawson.com',NULL);

-- --------------------------------------- Data for table dbo.medication


-- -------------------------------------INSERT INTO dbo.medication
(med_name, med_price, med_shelf_life, med_notes)

VALUES
('Vyvanse', 250.00, '2015-01-10',NULL),
('Concerta', 500.00, '2019-01-01', NULL),
('Adderall', 350.00, '2016-01-01', NULL),
('Lithium' 1000.00, '2014-12-05', NULL),
('Ritalin', 750.00, '2015-06-16',NULL);

-- -------------------------------------- Data for table dbo.prescription


-- ------------------------------------INSERT INTO dbo.prescription
(pre_date,pre_dosage,pre_num_refills,pre_notes)

VALUES
('2014-01-05', '10', '1',NULL),

('2014-01-06', '150', '3',NULL),


('2014-01-07', '20', '2',NULL),
('2014-01-08', '200', '1',NULL),
('2014-01-09', '50', '2',NULL);

-- --------------------------------------- Data for table dbo.treatment


-- --------------------------------------

-- --------------------------------------- Data for table dbo.patient_treatment


-- --------------------------------------

-- --------------------------------------- Data for table dbo.administration_lu


-- --------------------------------------

exec sp_msforeachtable "ALTER TABLE? WITH CHECK CHECK CONSTRAINT all"

-- show data
select * from dbo.patient;
select * from dbo.physician;
select * from dbo.medication;
select * from dbo.prescription;
select * from dbo.treatment;
select * from dbo.patient_treatment;
select * from dbo.administration_lu;