parent
b73441e6b2
commit
3782d13932
@ -0,0 +1,9 @@
|
|||||||
|
-- 创建新数据库
|
||||||
|
CREATE DATABASE hms;
|
||||||
|
|
||||||
|
-- 创建用户
|
||||||
|
CREATE USER gaussdb WITH CREATEDB password "Gauss@123";
|
||||||
|
|
||||||
|
-- 给用户赋予数据库的权限
|
||||||
|
GRANT ALL PRIVILEGES TO gaussdb;
|
||||||
|
|
@ -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);
|
@ -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');
|
@ -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);
|
@ -0,0 +1,13 @@
|
|||||||
|
-- 插入科室数据
|
||||||
|
INSERT INTO department (department_name, location)
|
||||||
|
VALUES
|
||||||
|
('内科', '北京市朝阳区医院1楼'),
|
||||||
|
('外科', '北京市海淀区医院2楼'),
|
||||||
|
('儿科', '北京市昌平区医院3楼'),
|
||||||
|
('妇产科', '北京市丰台区医院4楼'),
|
||||||
|
('眼科', '北京市西城区医院5楼'),
|
||||||
|
('牙科', '北京市石景山区医院6楼'),
|
||||||
|
('精神科', '北京市通州区医院7楼'),
|
||||||
|
('皮肤科', '北京市朝阳区医院8楼'),
|
||||||
|
('耳鼻喉科', '北京市怀柔区医院9楼'),
|
||||||
|
('肿瘤科', '北京市大兴区医院10楼');
|
@ -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');
|
@ -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;
|
@ -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;
|
@ -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);
|
@ -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;
|
@ -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();
|
Loading…
Reference in new issue