create database HospitalSystem;
create table person(
National_ID int PRIMARY key,
FirstName varchar(50) check(FirstName like '^[A-Za-z]+$') not null,
LastName varchar(50) check(LastName like '^[A-Za-z]+$') not null,
BirthDate date CHECK(BirthDate<= CAST(SYSDATETIME() AS DATE)) not null,
City varchar(50) not null,
street varchar(50) not null,
gender char check(gender in ('M','F')),
state varchar(50) not null
)
create table Contact(
National_ID int ,
Contact varchar(15) check(Contact like '^[0-9]+$'),
PRIMARY KEY (National_ID, Contact),
FOREIGN KEY (National_ID) REFERENCES person(National_ID)
)
create table Email(
National_ID int,
email varchar(50) check(email like '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
PRIMARY KEY (National_ID,email)
FOREIGN KEY (National_ID) REFERENCES person(National_ID)
)
create table Employee(
National_ID int ,
Employee_ID int PRIMARY key IDENTITY,
Salary money CHECK(Salary>=0) not null,
Hire_Date date CHECK(Hire_Date<= CAST(SYSDATETIME() AS DATE)) not null,
Resign_Date date CHECK(Resign_Date<= CAST(SYSDATETIME() AS DATE)) null,
FOREIGN KEY (National_ID) REFERENCES person(National_ID)
)
create table Nurse(
Nurse_ID int PRIMARY key IDENTITY,
Employee_ID int,
shift varchar(50),
FOREIGN KEY (Employee_ID) REFERENCES Employee(Employee_ID)
)
create table Department(
Department_Name varchar(50) PRIMARY key,
Department_Floor int CHECK(Department_Floor>=0) not null,
Department_Building int CHECK(Department_Building>=0) not null
)
create table Nurse_Department(
Nurse_ID int,
Department_Name varchar(50),
PRIMARY KEY (Nurse_ID,Department_Name),
FOREIGN KEY (Nurse_ID) REFERENCES Nurse(Nurse_ID),
FOREIGN KEY (Department_Name) REFERENCES Department(Department_Name)
)
create table Doctor(
Doctor_ID int PRIMARY KEY IDENTITY,
Department_Name varchar(50),
Employee_ID int,
Year_Of_Experience int CHECK(Year_Of_Experience>=0) DEFAULT 0,
FOREIGN KEY (Department_Name) REFERENCES Department(Department_Name),
FOREIGN KEY (Employee_ID) REFERENCES Employee(Employee_ID)
)
create table Doctor_Qualification(
Doctor_ID int,
Qualification varchar(50),
PRIMARY KEY(Doctor_ID,Qualification),
FOREIGN KEY (Doctor_ID) REFERENCES Doctor(Doctor_ID)
)
create table NonScientific_Employee(
NSE_ID INT PRIMARY KEY IDENTITY,
NSE_ROLE varchar(50),
Department_Name varchar(50),
Employee_ID int,
FOREIGN KEY (Department_Name) REFERENCES Department(Department_Name),
FOREIGN KEY (Employee_ID) REFERENCES Employee(Employee_ID)
)
create table Device(
Device_ID int PRIMARY KEY,
Device_Name varchar(50) not null,
Device_Type varchar(50) not null
)
create table Room(
Room_ID int PRIMARY KEY,
Room_Type varchar(50) not null,
Room_Capacity int CHECK(Room_Capacity>=0) not null,
Department_Name varchar(50),
Device_ID int,
FOREIGN KEY (Department_Name) REFERENCES Department(Department_Name),
FOREIGN KEY (Device_ID) REFERENCES Device(Device_ID),
)
create table Nurse_AssignedRoom(
Room_ID int,
Nurse_ID int
PRIMARY KEY(Room_ID,Nurse_ID),
FOREIGN KEY (Room_ID) REFERENCES Room(Room_ID),
FOREIGN KEY (Nurse_ID) REFERENCES Nurse(Nurse_ID),
)
create table Patient(
Patient_ID int PRIMARY KEY IDENTITY,
Residency_Start_Date date CHECK(Residency_Start_Date<= CAST(SYSDATETIME() AS DATE)) not null,
Residency_End_Date date CHECK(Residency_End_Date<= CAST(SYSDATETIME() AS DATE)) null,
Room_ID int,
National_ID int,
FOREIGN KEY (Room_ID) REFERENCES Room(Room_ID),
FOREIGN KEY (National_ID) REFERENCES person(National_ID)
)
create table Prescription(
Prescription_ID int PRIMARY KEY IDENTITY,
Prescription_Date date CHECK(Prescription_Date<= CAST(SYSDATETIME() AS DATE)) not null,
Instructions varchar(50),
Diagnosis_Code varchar(50) not null,
Patient_ID int
FOREIGN KEY (Patient_ID) REFERENCES Patient(Patient_ID)
)
create table Medicine(
Medicine_ID int PRIMARY KEY,
Medicine_Name varchar(50),
Expiry_Date date CHECK(Expiry_Date<= CAST(SYSDATETIME() AS DATE))
)
create table Prescription_Medicine(
Prescription_ID int,
Medicine_ID int,
Quantity int CHECK(Quantity> 0) not null,
Dosage varchar(50) check(Dosage like '^\d+(\.\d+)?(mg|ml|g|L)$') not null,
Frequency int CHECK(Frequency> 0) not null,
PRIMARY KEY(Prescription_ID,Medicine_ID),
FOREIGN KEY (Prescription_ID) REFERENCES Prescription(Prescription_ID),
FOREIGN KEY (Medicine_ID) REFERENCES Medicine(Medicine_ID)
)
create table Appointment(
Appointment_ID int PRIMARY KEY IDENTITY,
Appointment_Status varchar(50)
check(Appointment_Status in('Pending','Confirme','Complete','Canceled','Reschedule','No Show')),
Room_ID int,
Appointment_Date datetime2 CHECK(Appointment_Date<=SYSDATETIME()) not null,
Patient_ID int,
Doctor_ID int ,
FOREIGN KEY (Patient_ID) REFERENCES Patient(Patient_ID),
FOREIGN KEY (Room_ID) REFERENCES Room(Room_ID),
FOREIGN KEY (Doctor_ID) REFERENCES Doctor(Doctor_ID)
)
create table Bill(
Appointment_ID int PRIMARY KEY,
amount money CHECK(amount>=0) not null,
Tax money CHECK(Tax>=0) not null ,
Discount decimal(4,2) CHECK (Discount BETWEEN 0 AND 100) default(0)
FOREIGN KEY (Appointment_ID) REFERENCES Appointment(Appointment_ID)
)
Y3JlYXRlIGRhdGFiYXNlIEhvc3BpdGFsU3lzdGVtOwpjcmVhdGUgdGFibGUgcGVyc29uKApOYXRpb25hbF9JRCBpbnQgUFJJTUFSWSBrZXksCkZpcnN0TmFtZSB2YXJjaGFyKDUwKSBjaGVjayhGaXJzdE5hbWUgbGlrZSAnXltBLVphLXpdKyQnKSBub3QgbnVsbCwKTGFzdE5hbWUgIHZhcmNoYXIoNTApIGNoZWNrKExhc3ROYW1lICBsaWtlICdeW0EtWmEtel0rJCcpIG5vdCBudWxsLApCaXJ0aERhdGUgZGF0ZSBDSEVDSyhCaXJ0aERhdGU8PSBDQVNUKFNZU0RBVEVUSU1FKCkgQVMgREFURSkpIG5vdCBudWxsLApDaXR5IHZhc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