diff --git a/数据库脚本/创建数据库脚本.sql b/数据库脚本/创建数据库脚本.sql new file mode 100644 index 0000000..7c9058f --- /dev/null +++ b/数据库脚本/创建数据库脚本.sql @@ -0,0 +1,9 @@ +-- 创建新数据库 +CREATE DATABASE hms; + +-- 创建用户 +CREATE USER gaussdb WITH CREATEDB password "Gauss@123"; + +-- 给用户赋予数据库的权限 +GRANT ALL PRIVILEGES TO gaussdb; + diff --git a/数据库脚本/存储脚本.sql b/数据库脚本/存储脚本.sql new file mode 100644 index 0000000..757af0e --- /dev/null +++ b/数据库脚本/存储脚本.sql @@ -0,0 +1,53 @@ +-- 存储过程:根据病人ID查询病人信息。 +CREATE OR REPLACE FUNCTION get_patient_info(patient_id INT) +RETURNS TABLE(patient_name VARCHAR, gender CHAR, birthdate DATE, contact_number VARCHAR) AS $$ +BEGIN + RETURN QUERY + SELECT name, gender, birthdate, contact_number + FROM Patient + WHERE patient_id = patient_id; +END; +$$ LANGUAGE plpgsql; + +-- 存储过程:根据医生ID查询该医生的所有处方。 +CREATE OR REPLACE FUNCTION get_prescriptions_by_doctor(doctor_id INT) +RETURNS TABLE(patient_name VARCHAR, medication_name VARCHAR, total_amount NUMERIC) AS $$ +BEGIN + RETURN QUERY + SELECT p.name, m.name, pr.total_amount + FROM Prescription pr + JOIN Patient p ON pr.patient_id = p.patient_id + JOIN Medication m ON pr.medication_id = m.medication_id + WHERE pr.doctor_id = doctor_id; +END; +$$ LANGUAGE plpgsql; + +-- 存储过程:更新病人的联系方式。 +CREATE OR REPLACE FUNCTION update_patient_contact(patient_id INT, new_contact_number VARCHAR) +RETURNS VOID AS $$ +BEGIN + UPDATE Patient + SET contact_number = new_contact_number + WHERE patient_id = patient_id; +END; +$$ LANGUAGE plpgsql; + +-- 存储过程:删除病人的就诊记录。 +CREATE OR REPLACE FUNCTION delete_medical_record(patient_id INT) +RETURNS VOID AS $$ +BEGIN + DELETE FROM Medical_Record + WHERE patient_id = patient_id; +END; +$$ LANGUAGE plpgsql; + +-- 存储过程:查询药品库存不足的药品。 +CREATE OR REPLACE FUNCTION get_low_stock_medications(min_stock INT) +RETURNS TABLE(medication_name VARCHAR, stock_quantity INT) AS $$ +BEGIN + RETURN QUERY + SELECT name, stock_quantity + FROM Medication + WHERE stock_quantity < min_stock; +END; +$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/数据库脚本/建表脚本.sql b/数据库脚本/建表脚本.sql new file mode 100644 index 0000000..b098df2 --- /dev/null +++ b/数据库脚本/建表脚本.sql @@ -0,0 +1,90 @@ +-- 1. 病人信息表(Patient) +CREATE TABLE Patient ( + patient_id SERIAL PRIMARY KEY, -- 病人ID,主键 + name VARCHAR(50) NOT NULL, -- 病人姓名 + gender CHAR(1) CHECK(gender IN ('M', 'F')), -- 性别,M表示男性,F表示女性 + birthdate DATE NOT NULL, -- 出生日期 + contact_number VARCHAR(15), -- 联系电话 + address VARCHAR(100), -- 联系地址 + insurance_id VARCHAR(50) UNIQUE NOT NULL, -- 医保号,主键,确保唯一 + attending_doctor_id INTEGER, -- 主治医生ID,外键,关联医生表 + visit_type VARCHAR(20) CHECK(visit_type IN ('Inpatient', 'Outpatient')) -- 就诊类型 + FOREIGN KEY(attending_doctor_id) REFERENCES Doctor(doctor_id) +); + +-- 2. 医生信息表(Doctor) +CREATE TABLE Doctor ( + doctor_id SERIAL PRIMARY KEY, -- 医生ID,主键 + name VARCHAR(50) NOT NULL, -- 医生姓名 + phone_number VARCHAR(15), -- 联系电话 + email VARCHAR(50) UNIQUE NOT NULL, -- 医生邮箱,主键 + years_of_experience INTEGER, -- 工作年限 + department_id INTEGER, -- 科室ID,引用 department 表的 department_id + FOREIGN KEY(department_id) REFERENCES Department(department_id) +); + +-- 3. 病房信息表(Ward) +CREATE TABLE Ward ( + ward_id SERIAL PRIMARY KEY, -- 病房ID,主键 + total_beds INTEGER, -- 总床位数 + available_beds INTEGER, -- 可用床位数 + department_id INTEGER, -- 科室ID,引用 department 表的 department_id + FOREIGN KEY(department_id) REFERENCES Department(department_id) +); + +-- 4. 药品信息表(Medication) +CREATE TABLE Medication ( + medication_id SERIAL PRIMARY KEY, -- 药品ID,主键 + name VARCHAR(100) NOT NULL, -- 药品名称 + dosage VARCHAR(50), -- 剂量 + unit_price NUMERIC(10, 2), -- 单价 + stock_quantity INTEGER, -- 库存数量 + expiration_date DATE -- 药品有效期 +); + +-- 5. 处方信息表(Prescription) +CREATE TABLE Prescription ( + prescription_id SERIAL PRIMARY KEY, -- 处方ID,主键 + doctor_id INTEGER, -- 医生ID,引用 Doctor 表的 doctor_id + patient_id INTEGER, -- 病人ID,引用 Patient 表的 patient_id + medication_id INTEGER, -- 药品ID,引用 Medication 表的 medication_id + prescription_date DATE NOT NULL, -- 开具日期 + total_amount NUMERIC(10, 2), -- 处方总费用 + FOREIGN KEY(doctor_id) REFERENCES Doctor(doctor_id), + FOREIGN KEY(patient_id) REFERENCES Patient(patient_id), + FOREIGN KEY(medication_id) REFERENCES Medication(medication_id) +); + +-- 6. 就诊记录表(Medical_Record) +CREATE TABLE Medical_Record ( + record_id SERIAL PRIMARY KEY, -- 就诊记录ID,主键 + patient_id INTEGER, -- 病人ID,引用 Patient 表的 patient_id + doctor_id INTEGER, -- 医生ID,引用 Doctor 表的 doctor_id + visit_date DATE NOT NULL, -- 就诊日期 + diagnosis VARCHAR(500), -- 诊断结果 + treatment_plan VARCHAR(500), -- 治疗方案 + FOREIGN KEY(patient_id) REFERENCES Patient(patient_id), + FOREIGN KEY(doctor_id) REFERENCES Doctor(doctor_id) +); + +-- 7. 医嘱信息表(Order) +CREATE TABLE "Order" ( + order_id SERIAL PRIMARY KEY, -- 医嘱ID,主键 + prescription_id INTEGER, -- 处方ID,引用 Prescription 表的 prescription_id + doctor_id INTEGER, -- 医生ID,引用 Doctor 表的 doctor_id + patient_id INTEGER, -- 病人ID,引用 Patient 表的 patient_id + order_date DATE NOT NULL, -- 医嘱开具日期 + order_type VARCHAR(50) CHECK(order_type IN ('Medication', 'Treatment', 'Procedure')), -- 医嘱类型 + order_details TEXT, -- 医嘱详细内容 + status VARCHAR(20) CHECK(status IN ('Active', 'Completed', 'Cancelled')), -- 医嘱状态 + FOREIGN KEY(prescription_id) REFERENCES Prescription(prescription_id), + FOREIGN KEY(doctor_id) REFERENCES Doctor(doctor_id), + FOREIGN KEY(patient_id) REFERENCES Patient(patient_id) +); + +-- 8. 科室信息表(Department) +CREATE TABLE Department ( + department_id SERIAL PRIMARY KEY, -- 科室ID,主键 + department_name VARCHAR(20) NOT NULL, -- 科室名称 + location VARCHAR(50) NOT NULL -- 科室地址 +); \ No newline at end of file diff --git a/数据库脚本/插入医嘱表.sql b/数据库脚本/插入医嘱表.sql new file mode 100644 index 0000000..d8ee981 --- /dev/null +++ b/数据库脚本/插入医嘱表.sql @@ -0,0 +1,12 @@ +INSERT INTO "order"(prescription_id, doctor_id, patient_id, order_date, order_type, order_details, status) +VALUES +(1, 1, 1, '2024-12-21', 'Medication', '服用抗生素治疗感冒,持续5天', 'Active'), -- 医嘱1 +(2, 2, 2, '2024-12-21', 'Treatment', '进行体检,抽血化验', 'Active'), -- 医嘱2 +(3, 3, 3, '2024-12-20', 'Medication', '服用抗病毒药物治疗流感,持续7天', 'Active'), -- 医嘱3 +(4, 4, 4, '2024-12-19', 'Procedure', '准备手术,清洁手术部位', 'Active'), -- 医嘱4 +(5, 5, 5, '2024-12-18', 'Medication', '服用止痛药缓解疼痛,持续3天', 'Active'), -- 医嘱5 +(6, 6, 6, '2024-12-17', 'Treatment', '做胸部CT检查,检查肺部健康', 'Completed'), -- 医嘱6 +(7, 7, 7, '2024-12-16', 'Medication', '服用退烧药物,持续3天', 'Completed'), -- 医嘱7 +(8, 8, 8, '2024-12-15', 'Procedure', '做胃镜检查,排除胃部疾病', 'Cancelled'), -- 医嘱8 +(9, 9, 9, '2024-12-14', 'Treatment', '进行抗生素治疗,缓解感染症状', 'Active'), -- 医嘱9 +(10, 10, 10, '2024-12-13', 'Medication', '服用抗过敏药物,缓解过敏反应', 'Active'); -- 医嘱10 \ No newline at end of file diff --git a/数据库脚本/插入医生表.sql b/数据库脚本/插入医生表.sql new file mode 100644 index 0000000..06dc0fc --- /dev/null +++ b/数据库脚本/插入医生表.sql @@ -0,0 +1,12 @@ +INSERT INTO Doctor (name, phone_number, email, department_id, years_of_experience) +VALUES +('徐圣源', '202211070123', 'lisi@hospital.com', 1, 10), +('张世鹏', '202211070123', 'wangwu@hospital.com', 2, 8), +('赵六', '13900000003', 'zhaoliu@hospital.com', 3, 12), +('孙七', '13900000004', 'sunqi@hospital.com', 4, 6), +('周八', '13900000005', 'zhouba@hospital.com', 5, 7), +('钱九', '13900000006', 'qianjiu@hospital.com', 6, 9), +('吴十', '13900000007', 'wushi@hospital.com', 7, 4), +('郑十一', '13900000008', 'zheng11@hospital.com', 8, 11), +('冯十二', '13900000009', 'feng12@hospital.com', 9, 5), +('杨十三', '13900000010', 'yang13@hospital.com', 10, 15); diff --git a/数据库脚本/插入处方表.sql b/数据库脚本/插入处方表.sql new file mode 100644 index 0000000..72a9886 --- /dev/null +++ b/数据库脚本/插入处方表.sql @@ -0,0 +1,13 @@ + +INSERT INTO Prescription (doctor_id, patient_id, medication_id, prescription_date, total_amount) +VALUES +(1, 1, 2, '2024-12-21', 150.00), -- 处方1,医生1为病人1开具药品2 +(2, 2, 3, '2024-12-21', 200.00), -- 处方2,医生2为病人2开具药品3 +(3, 3, 4, '2024-12-20', 180.00), -- 处方3,医生3为病人3开具药品4 +(4, 4, 1, '2024-12-19', 120.00), -- 处方4,医生4为病人4开具药品1 +(5, 5, 5, '2024-12-18', 250.00), -- 处方5,医生5为病人5开具药品5 +(6, 6, 2, '2024-12-17', 160.00), -- 处方6,医生6为病人6开具药品2 +(7, 7, 3, '2024-12-16', 210.00), -- 处方7,医生7为病人7开具药品3 +(8, 8, 4, '2024-12-15', 190.00), -- 处方8,医生8为病人8开具药品4 +(9, 9, 1, '2024-12-14', 130.00), -- 处方9,医生9为病人9开具药品1 +(10, 10, 5, '2024-12-13', 220.00); -- 处方10,医生10为病人10开具药品5 diff --git a/数据库脚本/插入就诊记录表.sql b/数据库脚本/插入就诊记录表.sql new file mode 100644 index 0000000..019d19a --- /dev/null +++ b/数据库脚本/插入就诊记录表.sql @@ -0,0 +1,12 @@ +INSERT INTO medical_record (patient_id, doctor_id, visit_date, diagnosis, treatment_plan) +VALUES +(1, 1, '2024-12-21', '感冒', '服用抗生素治疗,注意休息和多喝水'), -- 就诊记录1 +(2, 2, '2024-12-21', '体检', '抽血检查、胸透检查,结果良好'), -- 就诊记录2 +(3, 3, '2024-12-20', '流感', '服用抗病毒药物,继续休息,多喝水'), -- 就诊记录3 +(4, 4, '2024-12-19', '手术前准备', '手术前身体检查,确认适合手术'), -- 就诊记录4 +(5, 5, '2024-12-18', '腰部疼痛', '开具止痛药,建议理疗治疗'), -- 就诊记录5 +(6, 6, '2024-12-17', '肺部疾病', '胸部CT检查显示轻度肺炎,开始治疗'), -- 就诊记录6 +(7, 7, '2024-12-16', '发热', '开具退烧药,建议卧床休息'), -- 就诊记录7 +(8, 8, '2024-12-15', '胃部不适', '做胃镜检查,排除胃病,服用胃药'), -- 就诊记录8 +(9, 9, '2024-12-14', '急性咽炎', '抗生素治疗,注意嗓子保护'), -- 就诊记录9 +(10, 10, '2024-12-13', '过敏反应', '开具抗过敏药物,避免接触过敏源'); -- 就诊记录10 \ No newline at end of file diff --git a/数据库脚本/插入病人表.sql b/数据库脚本/插入病人表.sql new file mode 100644 index 0000000..9e34878 --- /dev/null +++ b/数据库脚本/插入病人表.sql @@ -0,0 +1,12 @@ +INSERT INTO Patient (name, gender, birthdate, contact_number, address, insurance_id, attending_doctor_id, visit_type) +VALUES +('徐圣源', 'M', '2004-03-25', '202211070123', '山东科技大学', 'INS1234567890', 1, 'Inpatient'), +('张世鹏', 'M', '2004-07-15', '202211070123', '山东科技大学', 'INS1234567891', 2, 'Outpatient'), +('王五', 'M', '1982-10-22', '13900000003', '北京市昌平区', 'INS1234567892', 1, 'Inpatient'), +('赵六', 'F', '1979-12-30', '13900000004', '北京市东城区', 'INS1234567893', 3, 'Outpatient'), +('孙七', 'M', '1995-02-18', '13900000005', '北京市丰台区', 'INS1234567894', 4, 'Inpatient'), +('周八', 'F', '1988-05-10', '13900000006', '北京市西城区', 'INS1234567895', 1, 'Outpatient'), +('钱九', 'M', '1975-08-25', '13900000007', '北京市石景山区', 'INS1234567896',2, 'Inpatient'), +('吴十', 'F', '1998-01-01', '13900000008', '北京市通州区', 'INS1234567897', 3, 'Outpatient'), +('郑十一', 'M', '1984-06-15', '13900000009', '北京市朝阳区', 'INS1234567898', 1, 'Inpatient'), +('冯十二', 'F', '1993-11-20', '13900000010', '北京市怀柔区', 'INS1234567899', 4, 'Outpatient'); \ No newline at end of file diff --git a/数据库脚本/插入病房表.sql b/数据库脚本/插入病房表.sql new file mode 100644 index 0000000..dcb1b10 --- /dev/null +++ b/数据库脚本/插入病房表.sql @@ -0,0 +1,12 @@ +INSERT INTO Ward (total_beds, available_beds, department_id) +VALUES +( 20, 5, 1), +( 100, 50, 2), +( 15, 3, 3), +( 50, 25, 4), +( 30, 10, 5), +( 10, 5, 6), +( 20, 12, 7), +( 40, 20, 8), +( 35, 15, 9), +( 60, 30, 10); diff --git a/数据库脚本/插入科室表.sql b/数据库脚本/插入科室表.sql new file mode 100644 index 0000000..7883708 --- /dev/null +++ b/数据库脚本/插入科室表.sql @@ -0,0 +1,13 @@ +-- 插入科室数据 +INSERT INTO department (department_name, location) +VALUES +('内科', '北京市朝阳区医院1楼'), +('外科', '北京市海淀区医院2楼'), +('儿科', '北京市昌平区医院3楼'), +('妇产科', '北京市丰台区医院4楼'), +('眼科', '北京市西城区医院5楼'), +('牙科', '北京市石景山区医院6楼'), +('精神科', '北京市通州区医院7楼'), +('皮肤科', '北京市朝阳区医院8楼'), +('耳鼻喉科', '北京市怀柔区医院9楼'), +('肿瘤科', '北京市大兴区医院10楼'); diff --git a/数据库脚本/插入药品表.sql b/数据库脚本/插入药品表.sql new file mode 100644 index 0000000..1260392 --- /dev/null +++ b/数据库脚本/插入药品表.sql @@ -0,0 +1,12 @@ +INSERT INTO Medication (name, dosage, unit_price, stock_quantity, expiration_date) +VALUES +('阿莫西林', '250mg', 30.50, 200, '2024-12-31'), +('布洛芬', '200mg', 25.00, 150, '2024-09-30'), +('洛伐他汀', '20mg', 80.00, 100, '2025-05-20'), +('氯氮平', '50mg', 55.75, 50, '2024-06-15'), +('阿司匹林', '100mg', 15.00, 300, '2025-03-10'), +('钙片', '500mg', 10.00, 400, '2025-08-15'), +('甘草片', '100mg', 20.00, 180, '2024-11-01'), +('维生素C', '500mg', 12.00, 500, '2025-07-25'), +('卡托普利', '25mg', 40.00, 250, '2024-12-10'), +('盐酸二氢吡啶', '20mg', 18.50, 120, '2025-02-05'); \ No newline at end of file diff --git a/数据库脚本/更新及删除脚本.sql b/数据库脚本/更新及删除脚本.sql new file mode 100644 index 0000000..53eebdf --- /dev/null +++ b/数据库脚本/更新及删除脚本.sql @@ -0,0 +1,47 @@ +-- 3.5.3 数据更新及脚本 + +-- 更新病人“李四”的联系电话。 +UPDATE Patient +SET contact_number = '1234567890' +WHERE name = '李四'; + +-- 更新药品“抗生素”的库存数量。 +UPDATE Medication +SET stock_quantity = 200 +WHERE name = '抗生素'; + +-- 更新病房“心脏科病房”的可用床位数。 +UPDATE Ward +SET available_beds = available_beds - 1 +WHERE ward_id = (SELECT ward_id FROM Department WHERE department_name = '心脏科'); + +-- 删除病人“王五”的就诊记录。 +DELETE FROM Medical_Record +WHERE patient_id = (SELECT patient_id FROM Patient WHERE name = '王五'); + +-- 删除医生“张三”的所有处方。 +DELETE FROM Prescription +WHERE doctor_id = (SELECT doctor_id FROM Doctor WHERE name = '张三'); + +-- 更新病人“张三”的就诊类型为“住院”。 +UPDATE Patient +SET visit_type = 'Inpatient' +WHERE name = '张三'; + +-- 删除病房“内科病房”中的所有空闲床位信息。 +DELETE FROM Ward +WHERE ward_id = (SELECT ward_id FROM Department WHERE department_name = '内科') AND available_beds = total_beds; + +-- 删除指定药品的所有库存记录。 +DELETE FROM Medication +WHERE medication_id = 101; + +-- 更新药品的单价。 +UPDATE Medication +SET unit_price = 20.50 +WHERE name = '阿莫西林'; + +-- 更新医嘱的状态为“已完成”。 +UPDATE "Order" +SET status = 'Completed' +WHERE order_id = 123; \ No newline at end of file diff --git a/数据库脚本/权限脚本.sql b/数据库脚本/权限脚本.sql new file mode 100644 index 0000000..41da7e7 --- /dev/null +++ b/数据库脚本/权限脚本.sql @@ -0,0 +1,24 @@ +-- 用户角色及授权需求: + +-- 创建管理员角色并授权。 +CREATE ROLE admin; +GRANT ALL PRIVILEGES ON DATABASE hospital_db TO admin; + +-- 创建医生角色并授权。 +CREATE ROLE doctor; +GRANT SELECT, INSERT, UPDATE ON Patient TO doctor; +GRANT SELECT, INSERT, UPDATE ON Medical_Record TO doctor; + +-- 创建病人角色并授权。 +CREATE ROLE patient; +GRANT SELECT ON Patient TO patient; +GRANT SELECT, INSERT ON Medical_Record TO patient; + +-- 给管理员分配所有权限。 +GRANT admin TO user1; + +-- 给医生分配权限。 +GRANT doctor TO user2; + +-- 给病人分配权限。 +GRANT patient TO user3; \ No newline at end of file diff --git a/数据库脚本/查询脚本.sql b/数据库脚本/查询脚本.sql new file mode 100644 index 0000000..862ba3c --- /dev/null +++ b/数据库脚本/查询脚本.sql @@ -0,0 +1,136 @@ +-- 1. 比较条件查询(使用 =、<、>、!= 等比较操作符) +-- 查询1:查询所有年龄大于60岁的病人。 + +SELECT * FROM Patient +WHERE age > 60; +-- 查询2:查询所有姓名为“李四”的病人信息。 + +SELECT * FROM Patient +WHERE name = '李四'; +-- 查询3:查询所有药品库存低于100的药品。 + +SELECT * FROM Medication +WHERE stock_quantity < 100; +-- 查询4:查询所有病人在“2024-01-01”之前出生的病人信息。 + +SELECT * FROM Patient +WHERE birthdate < '2024-01-01'; +-- 2. 集合比较查询(使用 IN 和 NOT IN) +-- 查询1:查询所有在“心脏科”和“内科”就诊的病人信息。 + +SELECT * FROM Patient +WHERE department_id IN (SELECT department_id FROM Department WHERE department_name IN ('心脏科', '内科')); +-- 查询2:查询所有没有“住院”就诊类型的病人。 + +SELECT * FROM Patient +WHERE visit_type NOT IN ('Inpatient'); +-- 查询3:查询所有药品名称为“头孢”或“阿莫西林”的药品。 + +SELECT * FROM Medication +WHERE name IN ('头孢', '阿莫西林'); +-- 查询4:查询所有病人不是由“张医生”和“李医生”主治的病人。 + +SELECT * FROM Patient +WHERE attending_doctor_id NOT IN (SELECT doctor_id FROM Doctor WHERE name IN ('张医生', '李医生')); +-- 3. 范围比较查询(使用 BETWEEN) +-- 查询1:查询2024年1月1日至2024年12月31日内所有就诊记录。 + +SELECT * FROM Medical_Record +WHERE visit_date BETWEEN '2024-01-01' AND '2024-12-31'; +-- 查询2:查询库存量在50到200之间的药品信息。 + +SELECT * FROM Medication +WHERE stock_quantity BETWEEN 50 AND 200; +-- 查询3:查询2023年1月1日到2023年12月31日之间的所有病人。 + +SELECT * FROM Patient +WHERE birthdate BETWEEN '2023-01-01' AND '2023-12-31'; +-- 查询4:查询药品有效期在未来两个月内的药品。 + +SELECT * FROM Medication +WHERE expiration_date BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL '2 months'; +-- 4. 字符串相似比较查询(使用 LIKE 和 ILIKE) +-- 查询1:查询所有病人名字中包含“张”字的病人信息。 + +SELECT * FROM Patient +WHERE name LIKE '%张%'; +-- 查询2:查询所有药品名称中包含“抗生素”的药品。 + +SELECT * FROM Medication +WHERE name LIKE '%抗生素%'; +-- 查询3:查询所有病人地址中包含“北京市”的病人。 + +SELECT * FROM Patient +WHERE address LIKE '%北京市%'; +-- 查询4:查询所有药品名称以“盐酸”开头的药品。 + +SELECT * FROM Medication +WHERE name LIKE '盐酸%'; +-- 5. 多表连接查询(使用 JOIN) +-- 查询1:查询所有病人的姓名、就诊类型和主治医生的姓名。 + +SELECT p.name AS patient_name, p.visit_type, d.name AS doctor_name +FROM Patient p +JOIN Doctor d ON p.attending_doctor_id = d.doctor_id; +-- 查询2:查询所有病房的床位数量及科室名称。 + +SELECT w.ward_name, w.total_beds, dep.department_name +FROM Ward w +JOIN Department dep ON w.department_id = dep.department_id; +-- 查询3:查询每个病人及其所有处方的药品名称和总费用。 + +SELECT p.name AS patient_name, m.name AS medication_name, pr.total_amount +FROM Patient p +JOIN Prescription pr ON p.patient_id = pr.patient_id +JOIN Medication m ON pr.medication_id = m.medication_id; +-- 查询4:查询病人及其最近的就诊记录和医生姓名。 + +SELECT p.name AS patient_name, m.visit_date, d.name AS doctor_name +FROM Medical_Record m +JOIN Patient p ON m.patient_id = p.patient_id +JOIN Doctor d ON m.doctor_id = d.doctor_id +WHERE m.visit_date = (SELECT MAX(visit_date) FROM Medical_Record WHERE patient_id = p.patient_id); +-- 6. 嵌套查询(使用子查询) +-- 查询1:查询所有病人的姓名和主治医生的姓名,医生为“张三”。 + +SELECT name, (SELECT name FROM Doctor WHERE doctor_id = p.attending_doctor_id) AS doctor_name +FROM Patient p +WHERE p.attending_doctor_id = (SELECT doctor_id FROM Doctor WHERE name = '张三'); +-- 查询2:查询病人“王五”的最新就诊记录。 + +SELECT * FROM Medical_Record +WHERE patient_id = (SELECT patient_id FROM Patient WHERE name = '王五') +AND record_id = (SELECT MAX(record_id) FROM Medical_Record WHERE patient_id = (SELECT patient_id FROM Patient WHERE name = '王五')); +-- 查询3:查询所有库存量低于平均库存量的药品。 + +SELECT * FROM Medication +WHERE stock_quantity < (SELECT AVG(stock_quantity) FROM Medication); +-- 查询4:查询所有没有开具处方的病人信息。 + +SELECT * FROM Patient +WHERE patient_id NOT IN (SELECT patient_id FROM Prescription); +-- 7. EXISTS查询(使用 EXISTS) +-- 查询1:查询是否有病人在“张三”医生的主治下。 + +SELECT EXISTS( + SELECT 1 FROM Patient + WHERE attending_doctor_id = (SELECT doctor_id FROM Doctor WHERE name = '张三') +); +-- 查询2:查询是否有药品库存低于50的记录。 + +SELECT EXISTS( + SELECT 1 FROM Medication + WHERE stock_quantity < 50 +); +-- 查询3:查询是否有任何病人未支付的费用。 + +SELECT EXISTS( + SELECT 1 FROM Prescription + WHERE total_amount > 0 AND patient_id NOT IN (SELECT patient_id FROM Payment WHERE status = 'Paid') +); +-- 查询4:查询是否有任何药品即将过期(在未来30天内过期)。 + +SELECT EXISTS( + SELECT 1 FROM Medication + WHERE expiration_date BETWEEN CURRENT_DATE AND CURRENT_DATE + INTERVAL '30 days' +); \ No newline at end of file diff --git a/数据库脚本/索引脚本.sql b/数据库脚本/索引脚本.sql new file mode 100644 index 0000000..0821fad --- /dev/null +++ b/数据库脚本/索引脚本.sql @@ -0,0 +1,30 @@ +-- 创建索引用于加速对病人姓名的查询。 +CREATE INDEX idx_patient_name ON Patient(name); + +-- 创建索引用于加速按就诊类型查询病人。 + +CREATE INDEX idx_visit_type ON Patient(visit_type); + +-- 创建索引用于加速查询药品名称。 +CREATE INDEX idx_medication_name ON Medication(name); + +-- 创建索引用于加速查询医生的科室。 +CREATE INDEX idx_doctor_department ON Doctor(department_id); + +-- 创建索引用于加速根据日期查询处方。 +CREATE INDEX idx_prescription_date ON Prescription(prescription_date); + +-- 创建索引用于加速根据医生ID查询病人信息。 +CREATE INDEX idx_attending_doctor_id ON Patient(attending_doctor_id); + +-- 创建索引用于加速查询病人的就诊记录。 +CREATE INDEX idx_medical_record_patient_id ON Medical_Record(patient_id); + +-- 创建索引用于加速查询科室的医生。 +CREATE INDEX idx_department_doctor ON Doctor(department_id); + +-- 创建索引用于加速查询药品库存。 +CREATE INDEX idx_medication_stock_quantity ON Medication(stock_quantity); + +-- 创建索引用于加速查询医嘱的状态。 +CREATE INDEX idx_order_status ON "Order"(status); \ No newline at end of file diff --git a/数据库脚本/视图脚本.sql b/数据库脚本/视图脚本.sql new file mode 100644 index 0000000..b1f0c2a --- /dev/null +++ b/数据库脚本/视图脚本.sql @@ -0,0 +1,29 @@ +-- 创建一个视图显示病人的基本信息和主治医生。 +CREATE VIEW PatientDoctorInfo AS +SELECT p.name AS patient_name, p.gender, p.birthdate, d.name AS doctor_name +FROM Patient p +JOIN Doctor d ON p.attending_doctor_id = d.doctor_id; + +-- 创建一个视图显示病人的处方信息。 +CREATE VIEW PrescriptionInfo AS +SELECT p.name AS patient_name, m.name AS medication_name, pr.total_amount, pr.prescription_date +FROM Prescription pr +JOIN Patient p ON pr.patient_id = p.patient_id +JOIN Medication m ON pr.medication_id = m.medication_id; + +-- 创建一个视图显示所有药品的库存情况。 +CREATE VIEW MedicationStockInfo AS +SELECT name, stock_quantity, unit_price, expiration_date +FROM Medication; + +-- 创建一个视图显示所有科室的医生信息。 +CREATE VIEW DepartmentDoctorInfo AS +SELECT d.name AS doctor_name, dep.department_name, d.years_of_experience +FROM Doctor d +JOIN Department dep ON d.department_id = dep.department_id; + +-- 创建一个视图显示医生的诊断记录和治疗方案。 +CREATE VIEW DoctorMedicalRecord AS +SELECT d.name AS doctor_name, m.visit_date, m.diagnosis, m.treatment_plan +FROM Medical_Record m +JOIN Doctor d ON m.doctor_id = d.doctor_id; \ No newline at end of file diff --git a/数据库脚本/触发器脚本.sql b/数据库脚本/触发器脚本.sql new file mode 100644 index 0000000..c7691c1 --- /dev/null +++ b/数据库脚本/触发器脚本.sql @@ -0,0 +1,29 @@ +-- 触发器:在插入病人记录时,自动更新病房的可用床位。 +CREATE TRIGGER update_available_beds_after_patient_insert +AFTER INSERT ON Patient +FOR EACH ROW +EXECUTE FUNCTION update_available_beds(); + +-- 触发器:在删除药品时,删除与药品相关的处方。 +CREATE TRIGGER delete_prescriptions_after_medication_delete +AFTER DELETE ON Medication +FOR EACH ROW +EXECUTE FUNCTION delete_related_prescriptions(); + +-- 触发器:在更新药品库存时,记录日志。 +CREATE TRIGGER log_medication_stock_update +AFTER UPDATE ON Medication +FOR EACH ROW +EXECUTE FUNCTION log_stock_update(); + +-- 触发器:插入就诊记录时,更新病人的就诊次数。 +CREATE TRIGGER update_visit_count_after_insert +AFTER INSERT ON Medical_Record +FOR EACH ROW +EXECUTE FUNCTION update_visit_count(); + +-- 触发器:删除病人记录时,删除相关的医嘱。 +CREATE TRIGGER delete_orders_after_patient_delete +AFTER DELETE ON Patient +FOR EACH ROW +EXECUTE FUNCTION delete_related_orders(); \ No newline at end of file