SQL Server医疗信息管理系统数据库【英文版-源码】--(Medical Management System Database)

友情连接

1、医疗信息管理系统数据库–MySQL【中文版】

医疗信息管理系统数据库–MySQL【中文版】

2、邮件管理数据库设计–MySQL【中文版】

邮件管理数据库设计–MySQL【中文版】

3、学生成绩管理系统数据库设计–MySQL

学生成绩管理系统数据库设计–MySQL

4、点餐系统数据库设计–SQL Server

点餐系统数据库设计–SQL Server

5、商品管理系统数据库设计–SQL Server

商品管理系统数据库设计–SQL Server

6、SQL Server电影院数据库管理系统【英文版-源码】–(Movie Theatre Management System Database)

SQL Server电影院数据库管理系统【英文版-源码】–(Movie Theatre Management System Database)

1. ER图

请添加图片描述

2. 创建数据库及数据表、插入数据


--******Create/Drop Databse******

-- if MedicalManagementSystem exists, kill current connections to Database
-- make single users
IF DB_ID('MedicalManagementSystem') IS NOT NULL
	BEGIN
		USE [MASTER];

		ALTER	DATABASE [MedicalManagementSystem] 
		SET 	SINGLE_USER
		WITH	ROLLBACK IMMEDIATE;

		DROP DATABASE MedicalManagementSystem;
	END
GO

-- create new database called MedicalManagementSystem
CREATE DATABASE MedicalManagementSystem;
GO

USE MedicalManagementSystem;
GO

--******Create Tables*******

--table 1: users_info
DROP TABLE IF EXISTS users_info
GO
CREATE TABLE users_info
(
	id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	first_name VARCHAR(50) NOT NULL,
	last_name VARCHAR(50) NOT NULL,
	login_name VARCHAR(20) NOT NULL,
	password VARCHAR(20) NOT NULL,
	permit VARCHAR(20) NOT NULL,
	user_type VARCHAR(20) NOT NULL,
	gender VARCHAR(10) NOT NULL,
	age TINYINT NOT NULL,
	tel VARCHAR(20) NOT NULL,
	email VARCHAR(50) NOT NULL,
	address VARCHAR(70) NOT NULL,
	hire_date DATE NOT NULL
);
GO


--table 2: patients_info
DROP TABLE IF EXISTS patients_info
GO
CREATE TABLE patients_info
(
	pt_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	first_name VARCHAR(50) NOT NULL,
	last_name VARCHAR(50) NOT NULL,
	gender VARCHAR(10) NOT NULL,
	age TINYINT NOT NULL,
	tel VARCHAR(20) NOT NULL,
	email VARCHAR(50) NOT NULL,
	address VARCHAR(70) NOT NULL,
	creation_date DATE NOT NULL,
	doctor_id INT NOT NULL FOREIGN KEY REFERENCES users_info(id)
);
GO


--table 3:  drugs_devices_info
DROP TABLE IF EXISTS drugs_devices_info
GO
CREATE TABLE drugs_devices_info
(
	d_id VARCHAR(30) NOT NULL PRIMARY KEY,
	name VARCHAR(50) NOT NULL,
	specification VARCHAR(20) NOT NULL,
	quantity INT NOT NULL,
	manufacturer VARCHAR(50) NOT NULL,
	expiry_date DATE NOT NULL,
	cost_price INT NOT NULL,
	selling_price INT NOT NULL
);
GO


-- table 4: treat_price
-- copay means cost of registration
-- consultation_fee means cost of diagnosis
DROP TABLE IF EXISTS treat_price
GO
CREATE TABLE treat_price
(
	copay INT NOT NULL,
	consultation_fee INT NOT NULL,
	doctor_id INT NOT NULL FOREIGN KEY REFERENCES users_info(id)
);
GO


-- table 5: appointment
DROP TABLE IF EXISTS appointment
GO
CREATE TABLE appointment
(
	a_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	apt_date DATE NOT NULL,
	pt_id INT NOT NULL FOREIGN KEY REFERENCES patients_info(pt_id),
	doctor_id INT NOT NULL FOREIGN KEY REFERENCES users_info(id),
	receptionist_id INT NOT NULL FOREIGN KEY REFERENCES users_info(id)
);
GO


-- table 6: case_history 
DROP TABLE IF EXISTS case_history
GO
CREATE TABLE case_history
(
	c_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	description VARCHAR(MAX) NOT NULL,
	diagnosis VARCHAR(MAX) NOT NULL,
	therapy VARCHAR(MAX) NOT NULL,
	pt_id INT NOT NULL FOREIGN KEY REFERENCES patients_info(pt_id)
);
GO


-- table 7: medical_records 
DROP TABLE IF EXISTS medical_records
GO
CREATE TABLE medical_records
(
	id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	quantity TINYINT NOT NULL,
	c_id INT NOT NULL FOREIGN KEY REFERENCES case_history(c_id),
	d_id VARCHAR(30) NOT NULL FOREIGN KEY REFERENCES drugs_devices_info(d_id)
);
GO


-- table 8: nursing
DROP TABLE IF EXISTS nursing
GO
CREATE TABLE nursing
(
	n_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
	start_time DATETIME NOT NULL,
	finish_time DATETIME NOT NULL,
	pt_id INT NOT NULL FOREIGN KEY REFERENCES patients_info(pt_id),
	nurse_id INT NOT NULL FOREIGN KEY REFERENCES users_info(id)
);
GO



--******Database Population*******

INSERT INTO users_info
VALUES	('Andrew', 'Adams', 'python', 'jwjehj', 'lowlevel', 'receptionist', 'female', 30, '123456789', '[email protected]', '585 Robertson Drive Bunbury', convert(datetime, '2/6/2019', 103)),
		('Andy', 'Adams', 'javaee', 'endksjee', 'lowlevel', 'receptionist', 'male', 28, '123545419', '[email protected]', '486 Robertson Drive Bunbury', convert(datetime, '3/6/2019', 103)),
		('Anthony', 'Cook', 'lovepeace', 'loqgdj', 'lowlevel', 'receptionist', 'female', 29, '515556789', '[email protected]', '587 Robertson Drive Bunbury', convert(datetime, '9/6/2019', 103)),
		('Jack', 'Arnold', 'catyyyy', 'jnmbuer', 'midlevel', 'nurse', 'female', 31, '1231355565', '[email protected]', '598 Robertson Drive Bunbury', convert(datetime, '2/6/2019', 103)),
		('Ian', 'David', 'paaaaa', 'jweuuswu', 'midlevel', 'nurse', 'female', 30, '123415135', '[email protected]', '665 Robertson Drive Bunbury', convert(datetime, '3/6/2019', 103)),
		('Jacob', 'Bell', 'faaaaak', 'enhgfee', 'midlevel', 'nurse', 'male', 28, '3698525419', '[email protected]', '486 Robertson Drive Bunbury', convert(datetime, '5/6/2019', 103)),
		('Micheal', 'Carter', 'eeeeeem', 'nbgdj', 'midlevel', 'nurse', 'female', 29, '35476789', '[email protected]', '677 Robertson Drive Bunbury', convert(datetime, '8/6/2019', 103)),
		('Joseph', 'Cecil', 'qqqqquery', 'jhteuer', 'highlevel', 'doctor', 'female', 35, '521475565', '[email protected]', '258 Robertson Drive Bunbury', convert(datetime, '1/6/2019', 103)),
		('Tyler', 'Dale', 'djshuf', 'jijuswu', 'highlevel', 'doctor', 'male', 30, '354175135', '[email protected]', '545 Robertson Drive Bunbury', convert(datetime, '1/6/2019', 103)),
		('Daniel', 'Clark', 'caaaaao', 'qwrsjee', 'highlevel', 'doctor', 'male', 38, '954765419', '[email protected]', '246 Robertson Drive Bunbury', convert(datetime, '2/6/2019', 103)),
		('Dylan', 'Gary', 'pieeeeee', 'lmgadj', 'highlevel', 'doctor', 'female', 49, '521466789', '[email protected]', '333 Robertson Drive Bunbury', convert(datetime, '5/6/2019', 103)),
		('Megan', 'Eddy', 'oooooliga', 'ligeuer', 'highlevel', 'doctor', 'male', 41, '65231355565', '[email protected]', '593 Robertson Drive Bunbury', convert(datetime, '15/7/2019', 103));


INSERT INTO patients_info
VALUES	('Jaeob', 'Beye', 'male', 18, '3698935419', '[email protected]', '486 Robertson Drive Bunbury', convert(datetime, '14/6/2019', 103), 8),
		('Mieoeal', 'Caryede', 'female', 19, '38936789', '[email protected]', '277 Robertson Drive Bunbury', convert(datetime, '14/6/2019', 103), 8),
		('Joeoph', 'Cecye', 'female', 15, '521893565', '[email protected]', '58 Robertson Drive Bunbury', convert(datetime, '14/6/2019', 103), 9),
		('Tyeor', 'Dyede', 'male', 35, '354189335', '[email protected]', '245 Robertson Drive Bunbury', convert(datetime, '24/6/2019', 103), 9),
		('Daeoel', 'Claye', 'male', 38, '954893419', '[email protected]', '46 Robertson Drive Bunbury', convert(datetime, '25/6/2019', 103), 10),
		('Dyeon', 'Gyede', 'female', 49, '521466789', '[email protected]', '233 Robertson Drive Bunbury', convert(datetime, '25/6/2019', 103), 10),
		('Acheal', 'Cartye', 'female', 29, '35450389', '[email protected]', '77 Robertson Drive Bunbury', convert(datetime, '26/6/2019', 103), 8),
		('Aseph', 'Ceyedl', 'female', 45, '521475035', '[email protected]', '258 Robertson Drive Bunbury', convert(datetime, '28/6/2019', 103), 9),
		('Aler', 'Dayed', 'male', 60, '354150335', '[email protected]', '45 Robertson Drive Bunbury', convert(datetime, '28/6/2019', 103), 10),
		('Aniel', 'Clayed', 'male', 58, '954765039', '[email protected]', '246 Robertson Drive Bunbury', convert(datetime, '1/7/2019', 103), 11);


-- drugs : dr00000XX
-- devices : de0000XX
INSERT INTO drugs_devices_info
VALUES	('dr000001', 'Abacavir', '300mg/box', 100, 'A&Z Pharmaceutical Inc', convert(datetime, '25/8/2020', 103), 17, 86),
		('dr000002', 'Abamectin', '100mg/box', 100, 'A&Z Pharmaceutical Inc', convert(datetime, '25/9/2020', 103), 18, 98),
		('dr000003', 'Batilo', '500mg/box', 100, 'Abbott Laboratories Ltd', convert(datetime, '25/10/2020', 103), 19, 76),
		('dr000004', 'Acamprosate', '320mg/box', 100, 'Abbott Laboratories Ltdc', convert(datetime, '25/11/2020', 103), 25, 89),
		('dr000005', 'Omeprazole', '600mg/box', 100, 'Allergan Pharmaceutical Co Ltd', convert(datetime, '25/5/2020', 103), 87, 256),
		('de000001', 'Abame', '20PCs/bag', 120, 'A&Z Pharmaceutical Inc', convert(datetime, '25/9/2021', 103), 15, 68),
		('de000002', 'Baseddd', '50PCs/bag', 120, 'Abbott Laboratories Ltd', convert(datetime, '25/10/2021', 103), 19, 86),
		('de000003', 'Acampro', '100PCs/bag', 120, 'Abbott Laboratories Ltdc', convert(datetime, '25/12/2020', 103), 23, 89),
		('de000004', 'Omepra', '180PCs/bag', 120, 'Allergan Pharmaceutical Co Ltd', convert(datetime, '25/5/2021', 103), 86, 256);


INSERT INTO treat_price
VALUES	(8, 66, 8),
		(9, 68, 9),
		(8, 76, 10),
		(10, 59, 11),
		(10, 88, 12);


INSERT INTO appointment
VALUES	(convert(datetime, '14/6/2019', 103), 1, 8, 2),
		(convert(datetime, '14/6/2019', 103), 2, 8, 1),
		(convert(datetime, '14/6/2019', 103), 3, 9, 3),
		(convert(datetime, '24/6/2019', 103), 4, 9, 2),
		(convert(datetime, '25/6/2019', 103), 5, 10, 1),
		(convert(datetime, '25/6/2019', 103), 6, 10, 3),
		(convert(datetime, '26/6/2019', 103), 7, 8, 2),
		(convert(datetime, '28/6/2019', 103), 8, 9, 1),
		(convert(datetime, '28/6/2019', 103), 9, 10, 3);


INSERT INTO case_history
VALUES	('problem describe above mention various actors. ', 'use caseshe above system to be developed', 'Carefully go through the scenariapcovered in lectures', 1),
		('rolem des above mention various actors. ', 'use cases in thve system to be developed', 'Carefully go through the scenarios andthe conlectures', 2),
		('blem describe mention various actors. ', 'use cases in the abostem to be developed', 'Carefully go through the scenarios and then apcovered in lectures', 3),
		('pr describe above mention various actors. ', 'use cases in thee system to be developed', 'Carefully go through thand then apcovered in lectures', 4),
		('blem describe above on various actors. ', 'use cases in the asystem to be developed', 'Carefully go through the scenarios and then apcovered in lectures', 5),
		('problem describe abontion various actors. ', 'use cases in thve system to be developed', 'Carefully go through the scenarios and theconcepts covered in lectures', 6),
		('roble describe above mentionous actors. ', 'use cases in the  system to be developed', 'Carefully go through the scenariotcovered in lectures', 7),
		('lemribe above mention variouors. ', 'use cases in the above m to be developed', 'Carefully go through the scthen apcovered in lectures', 8),
		('roble describe above mentionous actors. ', 'use cases in the  system to be developed', 'Carefully go through the scenariotcovered in lectures', 9),
		('lemribe above mention variouors. ', 'use cases in the above m to be developed', 'Carefully go through the scthen apcovered in lectures', 10);


INSERT INTO medical_records
VALUES	(1, 1, 'dr000001'),
		(1, 1, 'de000001'),
		(3, 2, 'dr000001'),
		(1, 3, 'de000001'),
		(6, 4, 'dr000001'),
		(2, 4, 'dr000002'),
		(1, 4, 'de000002'),
		(5, 5, 'de000003'),
		(3, 6, 'dr000003'),
		(10, 7, 'de000004'),
		(2, 8, 'dr000004'),
		(4, 9, 'de000004'),
		(2, 10, 'dr000004');


INSERT INTO nursing
VALUES	(convert(datetime,'14/6/2019 10:00:000', 103), convert(datetime,'14/6/2019 18:00:000', 103), 2, 4),
		(convert(datetime,'25/6/2019 10:00:000', 103), convert(datetime,'25/6/2019 15:00:000', 103), 6, 5),
		(convert(datetime,'28/6/2019 10:00:000', 103), convert(datetime,'28/6/2019 20:00:000', 103), 9, 5);

3. 查询数据


USE MedicalManagementSystem;
GO

-- Query 1: Young Doctors
-- SELECT doctor's first name, age, user_type
-- WHERE:
--		age less than 40
-- Order the results by age

SELECT		first_name,
			age,
			user_type
FROM 		dbo.users_info
WHERE 		user_type = 'doctor' AND
			age < 40
ORDER BY 	age;


-- Query 2: The Number of Patients in Each Doctor's Care
-- SELECT doctor's first name, the number of patients,
-- WHERE:
-- 		the doctor's hire_date in June 2019
-- Order the results by the number of patients

SELECT 		u.first_name,
			p.number
FROM 		dbo.users_info AS u INNER JOIN
				(SELECT doctor_id, COUNT(*) AS number FROM dbo.patients_info GROUP BY doctor_id) AS p
				ON u.id = p.doctor_id
WHERE 		MONTH(u.hire_date) = 6
ORDER BY 	p.number;


-- Query 3: Drugs Being Used
-- SELECT drug's id, name, specification, quantity, expriry_date and the number of being used
-- WHERE:
--    it was being used in prescription
-- Order the results by the number of being used, in descending order

SELECT 		d.d_id,
			d.name,
			d.specification,
			d.quantity,
			d.expiry_date,
			m.number
FROM 		dbo.drugs_devices_info AS d INNER JOIN
				(SELECT d_id, SUM(quantity) AS number FROM dbo.medical_records GROUP BY d_id) AS m
				ON d.d_id = m.d_id AND
				d.d_id LIKE 'dr%'
ORDER BY 	m.number DESC;


-- Query 4: Appointments and Nurse Care
-- SELECT the patient's name who had an appointment, the whole time of being cared by nurse
-- show all the patients, even if the patient was not being cared by nurse
-- Order by the time of being cared, in descending order

SELECT 		a.pt_id,
			ISNULL(DATEDIFF(hour, n.start_time, n.finish_time), 0) AS care_time
FROM 		dbo.nursing AS n RIGHT JOIN
				dbo.appointment AS a ON a.pt_id = n.pt_id
ORDER BY 	care_time DESC;


-- Query 5: Patient's Case History and Prescription
-- SELECT patient's id, first name, case history
-- WHERE:
-- 		the number of sum total drugs and devices that being used to patients more than 1
-- Order by the patient's id

SELECT 		pa.pt_id,
			pa.first_name,
			c.description,
			c.diagnosis,
			c.therapy,
			me.number
FROM 		dbo.patients_info AS pa, dbo.case_history AS c,
			(SELECT c_id, COUNT(c_id) AS number FROM dbo.medical_records GROUP BY c_id) AS me
WHERE 		pa.pt_id = c.pt_id AND
			c.c_id = me.c_id AND
			me.number > 1
ORDER BY 	pa.pt_id;


-- Query 6: The Patients Who Was Diagnosed by the Most Expensive Doctor
-- SELECT patient's id, full name, age, gender and doctor's full name,
-- WHERE:
-- 		the doctor have the most expensive consultation_fee and have been in charge of patients
-- Order the results by patient's id

SELECT 		p.pt_id,
			p.first_name + ' ' + p.last_name AS patient_name,
			p.age,
			p.gender,
			u.first_name + ' ' + u.last_name AS doctor_name,
			t.consultation_fee
FROM 		dbo.patients_info AS p, dbo.users_info AS u, dbo.treat_price AS t
WHERE 		p.doctor_id = u.id AND
			t.doctor_id = u.id AND
			u.id IN (SELECT doctor_id FROM dbo.patients_info GROUP BY doctor_id) AND
			t.consultation_fee IN (SELECT MAX(consultation_fee) FROM dbo.treat_price WHERE doctor_id IN (SELECT doctor_id FROM dbo.patients_info GROUP BY doctor_id))
ORDER BY 	p.pt_id;


-- Query 7: The Three Oldest Patients
-- SELECT the full name, gender, age, the case history and the docotr's full name of the three oldest patients
-- Order the results by patient's age, in descending order

SELECT 		TOP (3) p.first_name + ' ' + p.last_name AS patient_name,
			p.gender,
			p.age,
			c.description,
			c.diagnosis,
			c.therapy,
			u.first_name + ' ' + u.last_name AS doctor_name
FROM 		dbo.patients_info AS p INNER JOIN
				dbo.case_history AS c ON p.pt_id = c.pt_id
				INNER JOIN
					dbo.users_info AS u ON p.doctor_id = u.id
ORDER BY 	p.age DESC;


-- Query 8: Patients With The Same Drug
-- SELECT the full name, gender, age, the case history and drug's name, drug's dosage of patients
-- WHERE:
-- 		more than 1 people use the same drug
-- Order the results by drug's name ASC, drug's dosage in descending order	

SELECT 		dr.name AS drug_name,
			me.quantity AS dosage,
			pa.first_name + ' ' + pa.last_name AS patient_name,
			pa.gender,
			pa.age,
			ca.description,
			ca.diagnosis,
			ca.therapy
FROM 		dbo.patients_info AS pa INNER JOIN
				dbo.case_history AS ca ON pa.pt_id = ca.pt_id
				INNER JOIN
					dbo.medical_records AS me ON me.c_id = ca.c_id
					INNER JOIN
						dbo.drugs_devices_info AS dr ON dr.d_id = me.d_id
WHERE 		dr.d_id IN (SELECT m.d_id FROM dbo.medical_records AS m, dbo.case_history AS c WHERE m.c_id = c.c_id AND m.d_id LIKE 'dr%' GROUP BY m.d_id HAVING COUNT(c.pt_id) > 1)
ORDER BY 	drug_name ASC, dosage DESC;


-- Query 9: Total Cost of Patients
-- SELECT every patient's cost of copay, consultation_fee, drugs, devices, total
--		the cost of copay, consultation_fee are calculated in times
--		and the doctors who are in the patients_info table for one time means one registration and one diagnosis
-- Order the results by total cost, in descending order

SELECT 		c.pt_id, 
			t.copay, 
			t.consultation_fee, 
			ISNULL(SUM(m1.quantity * d.selling_price), 0) AS drug_cost, 
			ISNULL(SUM(m2.quantity * d.selling_price), 0) AS device_cost, 
			(t.consultation_fee + t.consultation_fee + ISNULL(SUM(m1.quantity * d.selling_price), 0) + ISNULL(SUM(m2.quantity * d.selling_price), 0)) AS total_cost
FROM 		dbo.patients_info AS p LEFT JOIN 
				dbo.treat_price AS t ON t.doctor_id = p.doctor_id LEFT JOIN 
					dbo.case_history AS c ON p.pt_id = c.pt_id LEFT JOIN 
						(SELECT * FROM dbo.medical_records WHERE d_id LIKE 'dr%') AS m1 ON m1.c_id = c.pt_id LEFT JOIN 
							(SELECT * FROM dbo.medical_records WHERE d_id LIKE 'de%') AS m2 ON m2.c_id = c.pt_id LEFT JOIN 
								dbo.drugs_devices_info AS d ON d.d_id = m1.d_id OR d.d_id = m2.d_id
GROUP BY 	c.pt_id, t.copay, t.consultation_fee
ORDER BY 	total_cost DESC;

4. 创建视图


USE MedicalManagementSystem;
GO

-- Patients View 
-- Create a view that selects the patient's id, full name, gender, age, description, diagnosis, therapy

DROP VIEW IF EXISTS v_patients
GO
CREATE VIEW v_patients AS
SELECT 		pa.pt_id,
			pa.first_name + ' ' + pa.last_name AS patient_name,
			pa.gender,
			pa.age,
			ca.description,
			ca.diagnosis,
			ca.therapy
FROM 		dbo.patients_info AS pa INNER JOIN
				dbo.case_history AS ca ON pa.pt_id = ca.pt_id;
GO


-- Doctors View
-- Create a view that selects the following details:
-- 		The doctor's id, full name, gender, age, tel, email, hired date
-- 		The number of patients who were diagnosed by the doctor
-- 		The total revenue of copay and consultation_fee of the doctor

DROP VIEW IF EXISTS v_doctors
GO
CREATE VIEW v_doctors AS
SELECT 		u.id,
			u.first_name + ' ' + u.last_name AS doctor_name,
			u.gender,
			u.age,
			u.tel,
			u.email,
			u.hire_date,
			ISNULL(pa.patients_num, 0) AS patients_num,
			ISNULL((t.copay + t.consultation_fee) * pa.patients_num, 0) AS total_revenue
FROM 		dbo.users_info AS u LEFT JOIN
				(SELECT doctor_id, COUNT(*) AS patients_num FROM dbo.patients_info GROUP BY doctor_id) AS pa ON pa.doctor_id = u.id LEFT JOIN
					dbo.treat_price AS t ON t.doctor_id = pa.doctor_id
WHERE 		u.user_type = 'doctor';
GO

猜你喜欢

转载自blog.csdn.net/Artificial_idiots/article/details/122095909
今日推荐