create database HospitalSystem;
create table person(
National_ID int PRIMARY key,
FirstName varchar(50) check(FirstName NOT LIKE '%[^A-Za-z]%') not null,
LastName varchar(50) check(LastName NOT 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 NOT 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 '%_@_%.__%')
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 '%[0-9]%' -- Ensure it has digits
AND (Dosage LIKE '%mg' OR Dosage LIKE '%ml' OR Dosage LIKE '%g' OR Dosage LIKE '%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)
)
Y3JlYXRlIGRhdGFiYXNlIEhvc3BpdGFsU3lzdGVtOwpjcmVhdGUgdGFibGUgcGVyc29uKApOYXRpb25hbF9JRCBpbnQgUFJJTUFSWSBrZXksCkZpcnN0TmFtZSB2YXJjaGFyKDUwKSBjaGVjayhGaXJzdE5hbWUgTk9UIExJS0UgJyVbXkEtWmEtel0lJykgbm90IG51bGwsCkxhc3ROYW1lICB2YXJjaGFyKDUwKSBjaGVjayhMYXN0TmFtZSAgTk9UIExJS0UgJyVbXkEtWmEtel0lJykgbm90IG51bGwsCkJpcnRoRGF0ZSBkYXRlIENIRUNLKEJpcnRoRGF0ZTw9IENBU1QoU1lTREFURV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