Sei sulla pagina 1di 3

1

#DROP SCHEMA patient;

2

3

CREATE SCHEMA IF NOT EXISTS PATIENT;

4

USE patient;

5

CREATE TABLE IF NOT EXISTS Department(

6

DepartmentID INT NOT NULL auto_increment,

7

DepartmentName VARCHAR(25) NOT NULL,

8

constraint DeptID_PK PRIMARY KEY(DepartmentID)

9

);

10

11

CREATE TABLE if not exists EMPLOYEE(

12

EmployeeID INT NOT NULL auto_increment PRIMARY KEY,

13

EmployeeType CHAR(1) not null CHECK(EmployeeType IN('D','N')),

14

FirstName VARCHAR(25) NOT NULL,

15

LastName VARCHAR(25) NOT NULL,

16

PhoneNumber VARCHAR(50) NOT NULL,

17

email VARCHAR(50) NOT NULL,

18

Address1 VARCHAR(50) NOT NULL,

19

Address2 VARCHAR(50),

20

City VARCHAR(50) NOT NULL,

21

State CHAR(2) NOT NULL,

22

ZipCode VARCHAR(20) NOT NULL,

23

UNIQUE(EmployeeID, EmployeeType)

24

#constraint EmpID_PK PRIMARY KEY(EmployeeID)

25

);

26

27

28

CREATE TABLE IF NOT EXISTS Doctor(

29

EmployeeID int NOT NULL PRIMARY KEY,

30

EmployeeType CHAR(1) DEFAULT 'D' NOT NULL, CHECK(EmployeeType='D'),

31

DepartmentID INT NOT NULL,

32

FOREIGN KEY(EmployeeID, EmployeeType)

33

references Employee(EmployeeID,EmployeeType),

34

CONSTRAINT DocDept_FK FOREIGN KEY(DepartmentID)

35

REFERENCES Department(DepartmentID)

36

ON UPDATE NO ACTION

37

ON DELETE NO ACTION

38

);

39

40

CREATE TABLE IF NOT EXISTS Nurse(

41

EmployeeID int NOT NULL primary key,

42

EmployeeType CHAR(1) DEFAULT 'N' NOT NULL, CHECK(EmployeeType='N'),

43

ShiftStart TIME NOT NULL,

44

ShiftEnd TIME NOT NULL,

45

CONSTRAINT foreign key (EmployeeID, EmployeeType) references Employee(EmployeeID, EmployeeType)

46

);

47

48

49

CREATE TABLE IF NOT EXISTS PATIENT(

50

PatientID INT NOT NULL auto_increment,

51

FirstName VARCHAR(25) NOT NULL,

52

LastName VARCHAR(25) NOT NULL,

53

Gender CHAR(1) CHECK(Gender IN ('M','F')),

54

Address1 VARCHAR(50) NOT NULL,

55

Address2 VARCHAR(50),

56

City VARCHAR(50) NOT NULL,

57

State VARCHAR(2) NOT NULL,

58

ZipCode VARCHAR(20) NOT NULL,

59

PhoneNumber VARCHAR(15) NOT NULL,

60

EmailAddress VARCHAR(45) NULL,

61

constraint PatientID_PK PRIMARY KEY(PatientID)

62

);

63

CREATE TABLE IF NOT EXISTS PatientRecord

64

(

65

RecordNumber INT NOT NULL AUTO_INCREMENT,

66

PatientID INT NOT NULL,

67

CheckInDate DATE NOT NULL,

68

CheckOutDate DATE,

69

CONSTRAINT RecNum_PK PRIMARY KEY(RecordNumber),

70

CONSTRAINT PatntID_FK FOREIGN KEY(PatientID)

71

references Patient(PatientID)

72

ON DELETE NO ACTION

73

ON UPDATE NO ACTION

74

);

75

76

CREATE TABLE IF NOT EXISTS PatientInvoice(

77

InvoiceNumber INT NOT NULL AUTO_INCREMENT,

78

PatientID INT NOT NULL,

79

InvoiceDate DATE NOT NULL,

80

TotalAmount decimal(9,2),

81

AmountPaid decimal(9,2),

82

CONSTRAINT InvoiceNum_PK PRIMARY KEY(InvoiceNumber),

83

CONSTRAINT PattID_FK FOREIGN KEY(PatientID)

84

references Patient(PatientID)

85

ON DELETE NO ACTION

86

ON UPDATE NO ACTION

87

)auto_increment=263142;

88

89

CREATE TABLE IF NOT EXISTS Patient_has_doctor (

90

PatientID INT NOT NULL,

91

DoctorID INT(11) NOT NULL,

92

PRIMARY KEY (PatientID, DoctorID),

93

CONSTRAINT PatDoc_FK FOREIGN KEY(PatientID)

94

REFERENCES Patient(PatientID)

95

ON DELETE NO ACTION

96

ON UPDATE NO ACTION,

97

CONSTRAINT fk_Patient_has_doctor_doctor1 FOREIGN KEY(DoctorID)

98

REFERENCES Doctor(EmployeeID)

99

ON DELETE NO ACTION

100

ON UPDATE NO ACTION

101

);

102

103

CREATE TABLE IF NOT EXISTS EmergencyContact (

104

PatientID INT NOT NULL,

105

FirstName VARCHAR(25) NOT NULL,

106

LastName VARCHAR(25) NOT NULL,

107

Address1 VARCHAR(50) NOT NULL,

108

Address2 VARCHAR(50),

109

City VARCHAR(50) NOT NULL,

110

State VARCHAR(2) NOT NULL,

111

ZipCode VARCHAR(20) NOT NULL,

112

PhoneNumber VARCHAR(15) NOT NULL,

113

EmailAddress VARCHAR(45),

114

PRIMARY KEY (PatientID),

115

constraint PatienID_FK FOREIGN KEY(PatientID)

116

REFERENCES Patient(PatientID)

117

ON UPDATE NO ACTION

118

ON DELETE NO ACTION)

119

;

120

CREATE TABLE IF NOT EXISTS Rooms(

121

RoomNumber INT NOT NULL PRIMARY KEY,

122

PatientID INT,

123

CONSTRAINT PatID_FK FOREIGN KEY(PatientID)

124

references Patient (PatientID)

125

ON DELETE CASCADE

126

ON UPDATE CASCADE

127

);

128

CREATE TABLE IF NOT EXISTS NurseGovernsRoom(

129

EmployeeID INT NOT NULL,

130

RoomNumber INT NOT NULL,

131

PRIMARY KEY(EmployeeID, RoomNumber),

132 Constraint RoomNurse_FK foreign key(EmployeeId)

133 references Nurse(EmployeeID)

134 ON UPDATE NO ACTION

135 ON DELETE NO ACTION,

136 Constraint RoomNum_FK foreign key(RoomNumber)

137 references Rooms(RoomNumber)

138 ON UPDATE NO ACTION

139 ON DELETE NO ACTION

140 );

141 #VIEW

142 CREATE VIEW Patient_Emergency_Contact_Information AS

143 SELECT T.PatientID,T.PatientName, T.RoomNumber, Concat_WS(' ', em.FirstName, em.LastName) AS Emergency_Contact,

144 em.PhoneNumber AS Emergency_Contact_Phone

145 FROM emergencycontact em JOIN

146 (SELECT CONCAT_WS(' ',FirstName, LastName) AS PatientName, p.PatientID, RoomNumber

147 FROM patient p JOIN rooms r

148 ON p.PatientID=r.PatientID)T

149 ON em.PatientID=T.PatientID

150 ORDER BY T.PatientID;