From 49eb7a5285154b8632976bdd3447f92c7bbc3b8e Mon Sep 17 00:00:00 2001 From: std Date: Sun, 29 Dec 2024 16:52:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E9=83=A8=E7=9A=84=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=8C=E5=8C=85=E6=8B=AC=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E8=84=9A=E6=9C=AC=EF=BC=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=88=9D=E5=A7=8B=E5=8C=96=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E7=B4=A2=E5=BC=95=E5=88=9B=E5=BB=BA=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E8=A7=86=20=E5=9B=BE=E5=88=9B=E5=BB=BA=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=EF=BC=8C=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E6=9D=83=E9=99=90=E8=84=9A=E6=9C=AC=EF=BC=8C=E8=A7=A6=E5=8F=91?= =?UTF-8?q?=E5=99=A8=E8=84=9A=E6=9C=AC=EF=BC=8C=E5=AD=98=E5=82=A8=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E5=88=9B=E5=BB=BA=E8=84=9A=E6=9C=AC=EF=BC=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9F=A5=E8=AF=A2=E8=84=9A=E6=9C=AC=EF=BC=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=9B=B4=E6=96=B0=E5=92=8C=E5=88=A0=E9=99=A4=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql_scripts/attendance_system_create_db.sql | 123 ++++++++++++ ...attendance_system_security_permissions.sql | 61 ++++++ sql_scripts/business_queries.sql | 143 ++++++++++++++ sql_scripts/data_update_scripts.sql | 79 ++++++++ sql_scripts/index_creation_scripts.sql | 42 ++++ sql_scripts/initialize_data.sql | 91 +++++++++ sql_scripts/stored_procedures.sql | 179 ++++++++++++++++++ sql_scripts/triggers.sql | 84 ++++++++ sql_scripts/view_creation_scripts.sql | 78 ++++++++ 9 files changed, 880 insertions(+) create mode 100644 sql_scripts/attendance_system_create_db.sql create mode 100644 sql_scripts/attendance_system_security_permissions.sql create mode 100644 sql_scripts/business_queries.sql create mode 100644 sql_scripts/data_update_scripts.sql create mode 100644 sql_scripts/index_creation_scripts.sql create mode 100644 sql_scripts/initialize_data.sql create mode 100644 sql_scripts/stored_procedures.sql create mode 100644 sql_scripts/triggers.sql create mode 100644 sql_scripts/view_creation_scripts.sql diff --git a/sql_scripts/attendance_system_create_db.sql b/sql_scripts/attendance_system_create_db.sql new file mode 100644 index 0000000..436c88a --- /dev/null +++ b/sql_scripts/attendance_system_create_db.sql @@ -0,0 +1,123 @@ +-- 创建数据库,如果不存在 +CREATE DATABASE IF NOT EXISTS attendance_system CHARACTER SET utf8 COLLATE utf8_general_ci; + +-- 使用创建的数据库 +USE attendance_system; + +-- 1. 创建员工表(Employee),该表需要先创建 +CREATE TABLE IF NOT EXISTS Employee ( + EmployeeID VARCHAR(50) PRIMARY KEY, -- 员工ID,主键 + Name VARCHAR(100) NOT NULL, -- 姓名 + Gender VARCHAR(10) NOT NULL, -- 性别(男/女) + DateOfBirth DATE NOT NULL DEFAULT '2000-01-01', -- 出生日期,默认值 + Position VARCHAR(100) NOT NULL, -- 职位 + DepartmentID VARCHAR(50), -- 部门ID + ContactInfo VARCHAR(50), -- 联系方式 + HireDate DATE NOT NULL, -- 入职日期 + EmploymentStatus VARCHAR(50) NOT NULL -- 状态(在职/离职/休假) +); + +-- 2. 创建部门表(Department),需要在Employee表后创建,因为Department表需要引用Employee表中的ManagerID +CREATE TABLE IF NOT EXISTS Department ( + DepartmentID VARCHAR(50) PRIMARY KEY, -- 部门ID,主键 + DepartmentName VARCHAR(100) NOT NULL, -- 部门名称 + ManagerID VARCHAR(50), -- 主管ID,外键,关联到员工表 + FOREIGN KEY (ManagerID) REFERENCES Employee(EmployeeID) -- 外键,关联到员工表 +); + +-- 3. 创建考勤记录表(Attendance),该表依赖Employee表 +CREATE TABLE IF NOT EXISTS Attendance ( + AttendanceID VARCHAR(50) PRIMARY KEY, -- 考勤ID,主键 + EmployeeID VARCHAR(50), -- 员工ID,外键,关联到员工表 + AttendanceDate DATE NOT NULL, -- 考勤日期 + CheckInTime DATETIME, -- 上班时间 + CheckOutTime DATETIME, -- 下班时间 + WorkingHours INT, -- 工作时长(小时) + OvertimeHours INT, -- 加班时长(小时) + AttendanceStatus VARCHAR(20) NOT NULL, -- 考勤状态(正常/迟到/早退/缺勤) + FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID) -- 外键,关联到员工表 +); + +-- 4. 创建假期申请表(LeaveRequest),该表依赖Employee表 +CREATE TABLE IF NOT EXISTS LeaveRequest ( + RequestID VARCHAR(50) PRIMARY KEY, -- 申请ID,主键 + EmployeeID VARCHAR(50), -- 员工ID,外键,关联到员工表 + LeaveType VARCHAR(50) NOT NULL, -- 假期类型 + StartDate DATE NOT NULL, -- 开始日期 + EndDate DATE NOT NULL, -- 结束日期 + LeaveDays INT NOT NULL, -- 假期天数 + RequestStatus VARCHAR(20) NOT NULL, -- 申请状态(待审批/已批准/已拒绝) + ApproverID VARCHAR(50), -- 审批人ID,外键,关联到员工表 + Remarks TEXT, -- 备注 + FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID), -- 外键,关联到员工表 + FOREIGN KEY (ApproverID) REFERENCES Employee(EmployeeID) -- 外键,关联到员工表 +); + +-- 5. 创建用户角色表(UserRoles) +CREATE TABLE IF NOT EXISTS UserRoles ( + RoleID VARCHAR(50) PRIMARY KEY, -- 角色ID,主键 + RoleName VARCHAR(100) NOT NULL, -- 角色名称 + RoleDescription TEXT NOT NULL -- 角色描述 +); + +-- 6. 创建用户账号表(UserLogin),该表依赖Employee表和UserRoles表 +CREATE TABLE IF NOT EXISTS UserLogin ( + UserID VARCHAR(50) PRIMARY KEY, -- 用户ID,主键 + EmployeeID VARCHAR(50), -- 员工ID,外键,关联到员工表 + RoleID VARCHAR(50), -- 角色ID,外键,关联到用户角色表 + Username VARCHAR(50) NOT NULL, -- 用户名 + Password VARCHAR(255) NOT NULL, -- 密码(加密存储) + LastLoginTime DATETIME, -- 最后登录时间 + AccountStatus VARCHAR(20) NOT NULL, -- 账号状态(有效/禁用) + FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID), -- 外键,关联到员工表 + FOREIGN KEY (RoleID) REFERENCES UserRoles(RoleID) -- 外键,关联到用户角色表 +); + +-- 7. 创建考勤统计表(AttendanceSummary),该表依赖Employee表 +CREATE TABLE IF NOT EXISTS AttendanceSummary ( + SummaryID VARCHAR(50) PRIMARY KEY, -- 统计ID,主键 + EmployeeID VARCHAR(50), -- 员工ID,外键,关联到员工表 + Month DATE NOT NULL, -- 统计月份 + PresentDays INT, -- 出勤天数 + LateCount INT, -- 迟到次数 + EarlyLeaveCount INT, -- 早退次数 + AbsentDays INT, -- 缺勤天数 + TotalOvertimeHours INT, -- 加班总时长(小时) + FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID) -- 外键,关联到员工表 +); + +-- 8. 创建系统日志表(SystemLogs),该表依赖UserLogin表 +CREATE TABLE IF NOT EXISTS SystemLogs ( + LogID VARCHAR(50) PRIMARY KEY, -- 日志ID,主键 + UserID VARCHAR(50), -- 用户ID,外键,关联到用户账号表 + ActionType VARCHAR(50) NOT NULL, -- 操作类型 + ActionTime DATETIME NOT NULL, -- 操作时间 + Description TEXT, -- 操作描述 + FOREIGN KEY (UserID) REFERENCES UserLogin(UserID) -- 外键,关联到用户账号表 +); + +-- 9. 创建培训记录表(TrainingRecords),该表依赖Employee表 +CREATE TABLE IF NOT EXISTS TrainingRecords ( + TrainingID VARCHAR(50) PRIMARY KEY, -- 培训ID,主键 + EmployeeID VARCHAR(50), -- 员工ID,外键,关联到员工表 + TrainingName VARCHAR(100) NOT NULL, -- 培训名称 + TrainingDate DATE NOT NULL, -- 培训日期 + TrainingHours INT, -- 培训时长(小时) + TrainingStatus VARCHAR(50) NOT NULL, -- 培训状态(已完成/进行中/未完成) + FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID) -- 外键,关联到员工表 +); + +-- 10. 创建绩效考核表(PerformanceAppraisal),该表依赖Employee表 +CREATE TABLE IF NOT EXISTS PerformanceAppraisal ( + ReviewID VARCHAR(50) PRIMARY KEY, -- 考核ID,主键 + EmployeeID VARCHAR(50), -- 员工ID,外键,标识被考核的员工 + ReviewDate DATE NOT NULL, -- 考核日期 + ReviewPeriod VARCHAR(50) NOT NULL, -- 考核周期(季度/年度) + ReviewScore INT, -- 考核评分 + ReviewGrade VARCHAR(50), -- 考核等级(A/B/C等) + ReviewerID VARCHAR(50), -- 考核人ID,外键,标识考核主管 + FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID), -- 外键,关联到员工表 + FOREIGN KEY (ReviewerID) REFERENCES Employee(EmployeeID) -- 外键,关联到员工表 +); + +-- 提示:所有的表创建完成后,可以执行数据插入脚本(如 `initialize_data.sql`)。 diff --git a/sql_scripts/attendance_system_security_permissions.sql b/sql_scripts/attendance_system_security_permissions.sql new file mode 100644 index 0000000..b9e2160 --- /dev/null +++ b/sql_scripts/attendance_system_security_permissions.sql @@ -0,0 +1,61 @@ +-- 3.5.8 安全方案设计及实现脚本 +-- 该脚本用于实现员工考勤管理系统中的安全方案,包括用户角色设计和权限管理。 + +-- ======================================== +-- 1. 创建角色表 (UserRoles) +-- ======================================== +CREATE TABLE IF NOT EXISTS UserRoles ( + RoleID VARCHAR(50) PRIMARY KEY, -- 角色ID + RoleName VARCHAR(50) NOT NULL, -- 角色名称 (管理员、经理、员工等) + RoleDescription VARCHAR(255) NOT NULL -- 角色描述 +); + +-- ======================================== +-- 2. 创建用户表 (UserLogin) +-- ======================================== +CREATE TABLE IF NOT EXISTS UserLogin ( + UserID VARCHAR(50) PRIMARY KEY, -- 用户ID + EmployeeID VARCHAR(50) NOT NULL, -- 员工ID,关联到 Employee 表 + RoleID VARCHAR(50) NOT NULL, -- 角色ID,关联到 UserRoles 表 + Username VARCHAR(50) NOT NULL, -- 用户名 + Password VARCHAR(255) NOT NULL, -- 密码(应加密存储) + LastLoginTime DATETIME, -- 上次登录时间 + AccountStatus VARCHAR(20) NOT NULL, -- 账户状态 (有效、无效) + FOREIGN KEY (RoleID) REFERENCES UserRoles(RoleID), -- 外键,关联到 UserRoles 表 + FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID) -- 外键,关联到 Employee 表 +); + +-- ======================================== +-- 3. 插入角色数据 +-- ======================================== +INSERT INTO UserRoles (RoleID, RoleName, RoleDescription) VALUES +('admin', '管理员', '具有完全访问权限,包括管理用户和所有数据操作'), +('manager', '经理', '可以管理本部门员工的考勤、假期等信息,不能管理系统用户'), +('employee', '员工', '只能查看和管理自己的信息,提交假期申请'); + +-- ======================================== +-- 4. 插入用户数据 +-- ======================================== +-- 假设已有一些员工数据,插入一些示例用户数据 +INSERT INTO UserLogin (UserID, EmployeeID, RoleID, Username, Password, AccountStatus) VALUES +('user1', 'E001', 'admin', 'admin_user', 'hashed_admin_password', '有效'), +('user2', 'E002', 'manager', 'manager_user', 'hashed_manager_password', '有效'), +('user3', 'E003', 'employee', 'employee_user', 'hashed_employee_password', '有效'); + +-- ======================================== +-- 7. 权限控制实现(应用级) +-- ======================================== +-- 权限控制可以通过应用程序实现,根据角色在业务逻辑中进行权限判断。比如: +-- 根据用户角色判断是否有权限执行某个操作 +-- 如果是管理员,允许所有操作 +-- 如果是经理,只允许本部门的员工操作 +-- 如果是员工,只允许自己提交假期申请、查看个人信息等 + +-- ======================================== +-- 8. 总结 +-- ======================================== +-- 通过上述的角色设计和权限管理,我们可以确保系统中的每个用户仅能够访问和操作他们有权限的部分数据。 +-- 管理员具有完全权限,经理可以管理本部门的员工信息,员工只能管理和查看自己的信息。 + +-- 注:上述 SQL 脚本仅为示例,实际开发中可能会根据具体需求调整和优化。 +-- 为了提高安全性,密码应进行加密存储,不应使用明文密码。 diff --git a/sql_scripts/business_queries.sql b/sql_scripts/business_queries.sql new file mode 100644 index 0000000..0d93c1f --- /dev/null +++ b/sql_scripts/business_queries.sql @@ -0,0 +1,143 @@ +-- 1. 查询所有在职员工的姓名和职位 +-- 查询类型:比较条件 +SELECT Name, Position +FROM Employee +WHERE EmploymentStatus = '在职'; + +-- 2. 查询所有已批准的年假记录 +-- 查询类型:比较条件 +SELECT e.Name, l.LeaveType, l.StartDate, l.EndDate +FROM LeaveRequest l +JOIN Employee e ON l.EmployeeID = e.EmployeeID +WHERE l.LeaveType = '年假' AND l.RequestStatus = '已批准'; + +-- 3. 查询1985年6月21日后入职的员工姓名和职位 +-- 查询类型:范围比较 +SELECT Name, Position +FROM Employee +WHERE HireDate > '1985-06-21'; + +-- 4. 查询所有在2024年1月到2024年3月间请病假的员工及其假期天数 +-- 查询类型:范围比较 +SELECT e.Name, l.LeaveDays, l.StartDate, l.EndDate +FROM LeaveRequest l +JOIN Employee e ON l.EmployeeID = e.EmployeeID +WHERE l.LeaveType = '病假' AND l.StartDate BETWEEN '2024-01-01' AND '2024-03-31'; + +-- 5. 查询所有名字中包含“王”字的员工 +-- 查询类型:字符串相似比较 +SELECT Name +FROM Employee +WHERE Name LIKE '%王%'; + +-- 6. 查询所有职位为“技术总监”或者“产品经理”的员工 +-- 查询类型:集合比较 +SELECT Name, Position +FROM Employee +WHERE Position IN ('技术总监', '产品经理'); + +-- 7. 查询所有缺勤记录的员工姓名和考勤日期 +-- 查询类型:集合比较 +SELECT e.Name, a.AttendanceDate +FROM Attendance a +JOIN Employee e ON a.EmployeeID = e.EmployeeID +WHERE a.AttendanceStatus = '缺勤'; + +-- 8. 查询所有2024年1月缺勤超过3天的员工姓名 +-- 查询类型:集合比较 +SELECT e.Name +FROM AttendanceSummary a +JOIN Employee e ON a.EmployeeID = e.EmployeeID +WHERE a.AbsentDays > 3 AND a.Month LIKE '2024-01%'; + +-- 9. 查询所有迟到超过5分钟的员工 +-- 查询类型:比较条件 +SELECT e.Name, a.AttendanceDate +FROM Attendance a +JOIN Employee e ON a.EmployeeID = e.EmployeeID +WHERE TIMESTAMPDIFF(MINUTE, a.CheckInTime, '09:00:00') > 5; + +-- 10. 查询所有2024年5月1日至5月5日请假超过2天的员工 +-- 查询类型:范围比较 +SELECT e.Name, l.LeaveDays +FROM LeaveRequest l +JOIN Employee e ON l.EmployeeID = e.EmployeeID +WHERE l.StartDate BETWEEN '2024-05-01' AND '2024-05-05' + AND l.LeaveDays > 2; + +-- 11. 查询所有职位为“经理”或“主管”的员工及其入职日期 +-- 查询类型:集合比较 +SELECT Name, Position, HireDate +FROM Employee +WHERE Position IN ('经理', '主管'); + +-- 12. 查询所有2024年12月1日的考勤记录 +-- 查询类型:范围比较 +SELECT e.Name, a.AttendanceDate, a.AttendanceStatus +FROM Attendance a +JOIN Employee e ON a.EmployeeID = e.EmployeeID +WHERE a.AttendanceDate = '2024-12-01'; + +-- 13. 查询所有名字以“张”开头的员工 +-- 查询类型:字符串相似比较 +SELECT Name +FROM Employee +WHERE Name LIKE '张%'; + +-- 14. 查询所有部门是“研发部”且在2024年12月有请假记录的员工 +-- 查询类型:多表连接 +SELECT e.Name, l.LeaveType, l.StartDate, l.EndDate +FROM Employee e +JOIN Department d ON e.DepartmentID = d.DepartmentID +JOIN LeaveRequest l ON e.EmployeeID = l.EmployeeID +WHERE d.DepartmentName = '研发部' AND l.StartDate BETWEEN '2024-12-01' AND '2024-12-31'; + +-- 15. 查询所有部门为“人事部”的员工,并返回其主管姓名 +-- 查询类型:多表连接 +SELECT e.Name AS EmployeeName, m.Name AS ManagerName +FROM Employee e +JOIN Department d ON e.DepartmentID = d.DepartmentID +JOIN Employee m ON d.ManagerID = m.EmployeeID +WHERE d.DepartmentName = '人事部'; + +-- 16. 查询所有2024年7月未请假员工的姓名 +-- 查询类型:多表连接 +SELECT e.Name +FROM Employee e +LEFT JOIN LeaveRequest l ON e.EmployeeID = l.EmployeeID +WHERE l.LeaveType IS NULL OR l.StartDate NOT LIKE '2024-07%'; + +-- 17. 查询所有员工及其部门名称 +-- 查询类型:多表连接 +SELECT e.Name, d.DepartmentName +FROM Employee e +JOIN Department d ON e.DepartmentID = d.DepartmentID; + +-- 18. 查询所有职位为“销售经理”的员工及其所在的部门名称 +-- 查询类型:多表连接 +SELECT e.Name, d.DepartmentName +FROM Employee e +JOIN Department d ON e.DepartmentID = d.DepartmentID +WHERE e.Position = '销售经理'; + +-- 19. 查询2024年上半年未请过假的员工姓名 +-- 查询类型:嵌套查询 +SELECT e.Name +FROM Employee e +WHERE e.EmployeeID NOT IN ( + SELECT DISTINCT EmployeeID + FROM LeaveRequest + WHERE LeaveStartDate BETWEEN '2024-01-01' AND '2024-06-30' + OR LeaveEndDate BETWEEN '2024-01-01' AND '2024-06-30' +); + +-- 20. 查询所有已经提交申请且状态为“已批准”的年假记录 +-- 查询类型:EXISTS查询 +SELECT e.Name, l.LeaveType, l.StartDate, l.EndDate +FROM Employee e +JOIN LeaveRequest l ON e.EmployeeID = l.EmployeeID +WHERE EXISTS ( + SELECT 1 + FROM LeaveRequest lr + WHERE lr.EmployeeID = e.EmployeeID AND lr.RequestStatus = '已批准' +); diff --git a/sql_scripts/data_update_scripts.sql b/sql_scripts/data_update_scripts.sql new file mode 100644 index 0000000..440e9ab --- /dev/null +++ b/sql_scripts/data_update_scripts.sql @@ -0,0 +1,79 @@ +-- 文件名:data_update_scripts.sql +-- 这是为员工考勤管理系统编写的更新操作(UPDATE)和删除操作(DELETE)SQL脚本 + +-- 1. 更新操作(UPDATE) + +-- 更新员工“孙晓”的职位为“高级软件工程师” +UPDATE Employee +SET Position = '高级软件工程师' +WHERE Name = '孙晓'; + +-- 将“杨洋”的入职日期更新为“2024-06-01” +UPDATE Employee +SET HireDate = '2024-06-01' +WHERE Name = '杨洋'; + +-- 更新部门ID为‘D003’的部门主管为‘刘晨’ +UPDATE Department +SET ManagerID = 'E008' +WHERE DepartmentID = 'D003'; + +-- 更新员工“赵丽”的联系方式为“12345678911” +UPDATE Employee +SET ContactInfo = '12345678911' +WHERE Name = '赵丽'; + +-- 修改“2024-12-02”至“2024-12-06”期间的所有请假记录状态为“已批准” +UPDATE LeaveRequest +SET RequestStatus = '已批准' +WHERE StartDate BETWEEN '2024-12-02' AND '2024-12-06'; + +-- 更新2024年12月1日员工“王强”的考勤状态为“迟到” +UPDATE Attendance +SET AttendanceStatus = '迟到' +WHERE EmployeeID = 'E003' AND AttendanceDate = '2024-12-01'; + +-- 修改“张伟”在“2024-06-01”到“2024-06-30”期间的所有假期天数为3天 +UPDATE LeaveRequest +SET LeaveDays = 3 +WHERE EmployeeID = 'E001' AND StartDate BETWEEN '2024-06-01' AND '2024-06-30'; + +-- 将“刘晨”所有加班时长更新为0 +UPDATE Attendance +SET OvertimeHours = 0 +WHERE EmployeeID = 'E008'; + +-- 为员工“赵丽”设置新的密码 +UPDATE UserLogin +SET Password = 'new_password123' +WHERE EmployeeID = 'E004'; + +-- 2. 删除操作(DELETE) + +-- 删除员工“李娜”的所有考勤记录 +DELETE FROM Attendance +WHERE EmployeeID = 'E002'; + +-- 删除所有状态为“已拒绝”的假期申请记录 +DELETE FROM LeaveRequest +WHERE RequestStatus = '已拒绝'; + +-- 删除所有职位为“技术总监”的员工记录 +DELETE FROM Employee +WHERE Position = '技术总监'; + +-- 删除部门为“市场部”的所有员工 +DELETE FROM Employee +WHERE DepartmentID = 'D004'; + +-- 删除2024年1月1日到2024年1月31日期间所有请假记录 +DELETE FROM LeaveRequest +WHERE StartDate BETWEEN '2024-12-01' AND '2024-12-05'; + +-- 删除所有考勤记录为“早退”的记录 +DELETE FROM Attendance +WHERE AttendanceStatus = '早退'; + +-- 删除所有2024年12月1日到2024年12月15日已批准的年假记录 +DELETE FROM LeaveRequest +WHERE LeaveType = '年假' AND RequestStatus = '已批准' AND StartDate BETWEEN '2024-12-01' AND '2024-12-15'; diff --git a/sql_scripts/index_creation_scripts.sql b/sql_scripts/index_creation_scripts.sql new file mode 100644 index 0000000..29f6ad2 --- /dev/null +++ b/sql_scripts/index_creation_scripts.sql @@ -0,0 +1,42 @@ +-- 文件名:index_creation_scripts.sql +-- 这是为员工考勤管理系统编写的索引创建脚本 + +-- 1. 为员工姓名创建索引 +CREATE INDEX idx_employee_name +ON Employee (Name); + +-- 2. 为员工部门ID创建索引 +CREATE INDEX idx_employee_department +ON Employee (DepartmentID); + +-- 3. 为考勤表中的员工ID创建索引 +CREATE INDEX idx_attendance_employeeid +ON Attendance (EmployeeID); + +-- 4. 为考勤表中的考勤日期创建索引 +CREATE INDEX idx_attendance_date +ON Attendance (AttendanceDate); + +-- 5. 为假期申请表中的员工ID创建索引 +CREATE INDEX idx_leave_request_employeeid +ON LeaveRequest (EmployeeID); + +-- 6. 为假期申请表中的申请状态创建索引 +CREATE INDEX idx_leave_request_status +ON LeaveRequest (RequestStatus); + +-- 7. 为绩效考核表中的员工ID创建索引 +CREATE INDEX idx_performance_employeeid +ON PerformanceAppraisal (EmployeeID); + +-- 8. 为考勤总结表中的统计月份创建索引 +CREATE INDEX idx_attendance_summary_month +ON AttendanceSummary (Month); + +-- 9. 为用户登录表中的员工ID创建索引 +CREATE INDEX idx_userlogin_employeeid +ON UserLogin (EmployeeID); + +-- 10. 为系统日志表中的用户ID创建索引 +CREATE INDEX idx_system_logs_userid +ON SystemLogs (UserID); diff --git a/sql_scripts/initialize_data.sql b/sql_scripts/initialize_data.sql new file mode 100644 index 0000000..0d0c0c4 --- /dev/null +++ b/sql_scripts/initialize_data.sql @@ -0,0 +1,91 @@ +-- ==================================== +-- 数据初始化脚本 +-- ==================================== + +-- 创建员工表 +-- 表结构请根据实际需求定义 +-- 示例员工表的插入数据 +-- ==================================== +-- 员工表 (Employee) +-- ==================================== +INSERT INTO Employee (EmployeeID, Name, Gender, DateOfBirth, Position, DepartmentID, ContactInfo, HireDate, EmploymentStatus) +VALUES +('E001', '张伟', '男', '1990-01-15', '软件工程师', 'D001', '13812345678', '2020-03-01', '在职'), +('E002', '李娜', '女', '1985-05-20', '人事经理', 'D002', '13923456789', '2018-06-15', '在职'), +('E003', '王强', '男', '1992-09-10', '测试工程师', 'D001', '13734567890', '2021-02-20', '在职'), +('E004', '赵丽', '女', '1988-03-25', '财务主管', 'D003', '13645678901', '2017-08-10', '在职'), +('E005', '孙晓', '男', '1995-12-05', '产品经理', 'D001', '13556789012', '2022-04-05', '在职'), +('E006', '陈刚', '男', '1990-07-13', '前端开发', 'D001', '13467890123', '2019-09-15', '休假'), +('E007', '杨洋', '女', '1983-06-18', 'HR专员', 'D002', '13378901234', '2020-01-01', '离职'), +('E008', '刘晨', '男', '1994-11-11', 'Java开发', 'D001', '13289012345', '2021-11-20', '在职'), +('E009', '吴霞', '女', '1991-02-23', '市场经理', 'D004', '13190123456', '2018-07-10', '在职'), +('E010', '郑涛', '男', '1989-05-17', '技术总监', 'D001', '13001234567', '2015-03-23', '在职'); + +-- ==================================== +-- 部门表 (Department) +-- ==================================== +INSERT INTO Department (DepartmentID, ManagerID, DepartmentName) +VALUES +('D001', 'E010', '研发部'), +('D002', 'E002', '人事部'), +('D003', 'E004', '财务部'), +('D004', 'E009', '市场部'); + +-- ==================================== +-- 考勤记录表 (Attendance) +-- ==================================== +INSERT INTO Attendance (AttendanceID, EmployeeID, AttendanceDate, CheckInTime, CheckOutTime, WorkingHours, OvertimeHours, AttendanceStatus) +VALUES +('A001', 'E001', '2024-12-01', '2024-12-25 09:00:00', '2024-12-25 18:00:00', 8, 1, '正常'), +('A002', 'E002', '2024-12-01', '2024-12-25 09:15:00', '2024-12-25 18:00:00', 7.75, 0, '迟到'), +('A003', 'E003', '2024-12-01', '2024-12-25 09:00:00', '2024-12-25 18:00:00', 8, 0, '正常'), +('A004', 'E004', '2024-12-01', '2024-12-25 09:00:00', '2024-12-25 18:00:00', 8, 2, '正常'), +('A005', 'E005', '2024-12-01', '2024-12-25 09:10:00', '2024-12-25 18:00:00', 7.83, 0, '迟到'), +('A006', 'E006', '2024-12-01', '2024-12-25 09:00:00', '2024-12-25 18:00:00', 8, 0, '正常'), +('A007', 'E007', '2024-12-01', '2024-12-25 09:00:00', '2024-12-25 18:00:00', 8, 0, '正常'), +('A008', 'E008', '2024-12-01', '2024-12-25 09:05:00', '2024-12-25 18:00:00', 7.92, 0, '迟到'), +('A009', 'E009', '2024-12-01', '2024-12-25 09:00:00', '2024-12-25 18:00:00', 8, 0, '正常'), +('A010', 'E010', '2024-12-01', '2024-12-25 09:00:00', '2024-12-25 18:00:00', 8, 0, '正常'); + +-- ==================================== +-- 假期申请表 (LeaveRequest) +-- ==================================== +INSERT INTO LeaveRequest (RequestID, EmployeeID, LeaveType, StartDate, EndDate, LeaveDays, RequestStatus, ApproverID, Remarks) +VALUES +('L001', 'E001', '年假', '2024-12-10', '2024-12-12', 3, '待审批', 'E002', '年终假期'), +('L002', 'E002', '病假', '2024-12-03', '2024-12-04', 2, '已批准', 'E004', '因病请假'), +('L003', 'E003', '年假', '2024-12-05', '2024-12-06', 2, '已批准', 'E010', '年假'), +('L004', 'E004', '事假', '2024-12-02', '2024-12-02', 1, '已批准', 'E002', '处理家务事'), +('L005', 'E005', '年假', '2024-12-08', '2024-12-10', 3, '已批准', 'E010', '年终休假'), +('L006', 'E006', '病假', '2024-12-01', '2024-12-02', 2, '已拒绝', 'E003', '生病未能批准'), +('L007', 'E007', '事假', '2024-12-15', '2024-12-16', 2, '待审批', 'E004', '家里有事'), +('L008', 'E008', '年假', '2024-12-07', '2024-12-09', 3, '已批准', 'E010', '休假'), +('L009', 'E009', '病假', '2024-12-06', '2024-12-07', 2, '已批准', 'E003', '生病'), +('L010', 'E010', '年假', '2024-12-12', '2024-12-14', 3, '已批准', 'E002', '年假'); + +-- ==================================== +-- 用户角色表 (UserRoles) +-- ==================================== +INSERT INTO UserRoles (RoleID, RoleName, RoleDescription) +VALUES +('R001', '管理员', '系统管理员,拥有最高权限'), +('R002', '普通员工', '普通员工,拥有基本的操作权限'), +('R003', 'HR', '人事部员工,负责员工假期审批'), +('R004', '财务', '财务部员工,负责财务相关操作'), +('R005', '经理', '部门经理,负责审批部门内员工的假期'); + +-- ==================================== +-- 考勤统计表 (AttendanceSummary) +-- ==================================== +INSERT INTO AttendanceSummary (SummaryID, EmployeeID, Month, PresentDays, LateCount, EarlyLeaveCount, AbsentDays, TotalOvertimeHours) +VALUES +('S001', 'E001', '2024-12-01', 22, 1, 0, 0, 10), +('S002', 'E002', '2024-12-01', 20, 2, 1, 0, 5), +('S003', 'E003', '2024-12-01', 23, 0, 0, 0, 8), +('S004', 'E004', '2024-12-01', 21, 1, 0, 0, 12), +('S005', 'E005', '2024-12-01', 20, 1, 1, 1, 15), +('S006', 'E006', '2024-12-01', 19, 3, 2, 0, 20), +('S007', 'E007', '2024-12-01', 18, 2, 1, 0, 3), +('S008', 'E008', '2024-12-01', 21, 0, 0, 0, 6), +('S009', 'E009', '2024-12-01', 22, 1, 0, 0, 7), +('S010', 'E010', '2024-12-01', 23, 0, 0, 0, 10); diff --git a/sql_scripts/stored_procedures.sql b/sql_scripts/stored_procedures.sql new file mode 100644 index 0000000..be47c00 --- /dev/null +++ b/sql_scripts/stored_procedures.sql @@ -0,0 +1,179 @@ +-- 文件名:stored_procedures.sql +-- 这是为员工考勤管理系统编写的存储过程创建脚本 + +DELIMITER // + +-- 1. 存储过程:sp_add_attendance_record +-- 需求:添加员工考勤记录 +CREATE PROCEDURE sp_add_attendance_record( + IN p_employee_id VARCHAR(50), + IN p_attendance_date DATE, + IN p_check_in_time DATETIME, + IN p_check_out_time DATETIME, + IN p_working_hours INT, + IN p_overtime_hours INT, + IN p_attendance_status VARCHAR(20) +) +BEGIN + -- 开始事务 + START TRANSACTION; + BEGIN + -- 插入考勤记录 + INSERT INTO Attendance (EmployeeID, AttendanceDate, CheckInTime, CheckOutTime, WorkingHours, OvertimeHours, AttendanceStatus) + VALUES (p_employee_id, p_attendance_date, p_check_in_time, p_check_out_time, p_working_hours, p_overtime_hours, p_attendance_status); + + -- 提交事务 + COMMIT; + EXCEPTION + WHEN OTHERS THEN + -- 回滚事务 + ROLLBACK; + -- 可以记录日志或返回错误信息 + END; +END // + +-- 2. 存储过程:sp_approve_leave_request +-- 需求:批准假期申请,修改状态为“已批准” +CREATE PROCEDURE sp_approve_leave_request( + IN p_request_id VARCHAR(50), + IN p_approver_id VARCHAR(50) +) +BEGIN + -- 开始事务 + START TRANSACTION; + BEGIN + -- 更新假期申请状态为“已批准” + UPDATE LeaveRequest + SET RequestStatus = '已批准', ApproverID = p_approver_id + WHERE RequestID = p_request_id; + + -- 提交事务 + COMMIT; + EXCEPTION + WHEN OTHERS THEN + -- 回滚事务 + ROLLBACK; + -- 可以记录日志或返回错误信息 + END; +END // + +-- 3. 存储过程:sp_reject_leave_request +-- 需求:拒绝假期申请,修改状态为“已拒绝” +CREATE PROCEDURE sp_reject_leave_request( + IN p_request_id VARCHAR(50), + IN p_approver_id VARCHAR(50) +) +BEGIN + -- 开始事务 + START TRANSACTION; + BEGIN + -- 更新假期申请状态为“已拒绝” + UPDATE LeaveRequest + SET RequestStatus = '已拒绝', ApproverID = p_approver_id + WHERE RequestID = p_request_id; + + -- 提交事务 + COMMIT; + EXCEPTION + WHEN OTHERS THEN + -- 回滚事务 + ROLLBACK; + -- 可以记录日志或返回错误信息 + END; +END // + +-- 4. 存储过程:sp_update_performance_score +-- 需求:更新员工的绩效考核评分和等级 +CREATE PROCEDURE sp_update_performance_score( + IN p_review_id VARCHAR(50), + IN p_review_score INT, + IN p_review_grade VARCHAR(10) +) +BEGIN + -- 开始事务 + START TRANSACTION; + BEGIN + -- 更新绩效评分和等级 + UPDATE PerformanceAppraisal + SET ReviewScore = p_review_score, ReviewGrade = p_review_grade + WHERE ReviewID = p_review_id; + + -- 提交事务 + COMMIT; + EXCEPTION + WHEN OTHERS THEN + -- 回滚事务 + ROLLBACK; + -- 可以记录日志或返回错误信息 + END; +END // + +-- 5. 存储过程:sp_generate_monthly_attendance_summary +-- 需求:生成每月考勤总结 +CREATE PROCEDURE sp_generate_monthly_attendance_summary( + IN p_month DATE +) +BEGIN + -- 开始事务 + START TRANSACTION; + BEGIN + -- 将每月的考勤数据汇总插入考勤总结表 + INSERT INTO AttendanceSummary (EmployeeID, Month, PresentDays, LateCount, EarlyLeaveCount, AbsentDays, TotalOvertimeHours) + SELECT + E.EmployeeID, + p_month AS StatisticMonth, + SUM(CASE WHEN A.AttendanceStatus = '正常' THEN 1 ELSE 0 END) AS PresentDays, + SUM(CASE WHEN A.AttendanceStatus = '迟到' THEN 1 ELSE 0 END) AS LateCount, + SUM(CASE WHEN A.AttendanceStatus = '早退' THEN 1 ELSE 0 END) AS EarlyLeaveCount, + SUM(CASE WHEN A.AttendanceStatus = '缺勤' THEN 1 ELSE 0 END) AS AbsentDays, + SUM(A.OvertimeHours) AS TotalOvertimeHours + FROM + Employee E + LEFT JOIN + Attendance A ON E.EmployeeID = A.EmployeeID AND A.AttendanceDate >= DATE_FORMAT(p_month, '%Y-%m-01') + AND A.AttendanceDate < DATE_ADD(DATE_FORMAT(p_month, '%Y-%m-01'), INTERVAL 1 MONTH) + GROUP BY + E.EmployeeID; + + -- 提交事务 + COMMIT; + EXCEPTION + WHEN OTHERS THEN + -- 回滚事务 + ROLLBACK; + -- 可以记录日志或返回错误信息 + END; +END // + +-- 6. 存储过程:sp_add_employee +-- 需求:新增员工信息 +CREATE PROCEDURE sp_add_employee( + IN p_employee_id VARCHAR(50), + IN p_name VARCHAR(100), + IN p_gender VARCHAR(10), + IN p_date_of_birth DATE, + IN p_position VARCHAR(50), + IN p_department_id VARCHAR(50), + IN p_contact_info VARCHAR(20), + IN p_hire_date DATE, + IN p_employment_status VARCHAR(20) +) +BEGIN + -- 开始事务 + START TRANSACTION; + BEGIN + -- 向员工表插入新员工记录 + INSERT INTO Employee (EmployeeID, Name, Gender, DateOfBirth, Position, DepartmentID, ContactInfo, HireDate, EmploymentStatus) + VALUES (p_employee_id, p_name, p_gender, p_date_of_birth, p_position, p_department_id, p_contact_info, p_hire_date, p_employment_status); + + -- 提交事务 + COMMIT; + EXCEPTION + WHEN OTHERS THEN + -- 回滚事务 + ROLLBACK; + -- 可以记录日志或返回错误信息 + END; +END // + +DELIMITER ; diff --git a/sql_scripts/triggers.sql b/sql_scripts/triggers.sql new file mode 100644 index 0000000..1dc2f8a --- /dev/null +++ b/sql_scripts/triggers.sql @@ -0,0 +1,84 @@ +-- 文件名:triggers.sql +-- 这是为员工考勤管理系统编写的触发器创建脚本 + +DELIMITER // + +-- 1. 触发器:trg_before_insert_attendance +-- 需求:在插入考勤记录前,自动计算工作时长和加班时长 +CREATE TRIGGER trg_before_insert_attendance +BEFORE INSERT ON Attendance +FOR EACH ROW +BEGIN + DECLARE work_duration INT; + DECLARE overtime_duration INT; + + -- NULL 检查 + IF NEW.CheckInTime IS NULL OR NEW.CheckOutTime IS NULL THEN + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '上班时间或下班时间不能为空'; + END IF; + + -- 计算工作时长 (下班时间 - 上班时间),单位为小时 + SET work_duration = TIMESTAMPDIFF(MINUTE, NEW.CheckInTime, NEW.CheckOutTime) / 60; + + -- 如果工作时长大于8小时,计算加班时长 + IF work_duration > 8 THEN + SET overtime_duration = work_duration - 8; + SET NEW.OvertimeHours = overtime_duration; + ELSE + SET NEW.OvertimeHours = 0; + END IF; + + SET NEW.WorkingHours = work_duration; +END // + +-- 2. 触发器:trg_after_update_leave_status +-- 需求:当假期申请的状态被更新为“已批准”时,自动更新员工的状态为“休假” +CREATE TRIGGER trg_after_update_leave_status +AFTER UPDATE ON LeaveRequest +FOR EACH ROW +BEGIN + -- 如果请假申请已批准,且员工当前状态不是“休假” + IF NEW.RequestStatus = '已批准' AND (SELECT EmploymentStatus FROM Employee WHERE EmployeeID = NEW.EmployeeID) != '休假' THEN + UPDATE Employee + SET EmploymentStatus = '休假' + WHERE EmployeeID = NEW.EmployeeID; + END IF; +END // + +-- 3. 触发器:trg_before_insert_performance_appraisal +-- 需求:在插入绩效考核记录前,确保绩效评分为合法值(例如:0到100之间) +CREATE TRIGGER trg_before_insert_performance_appraisal +BEFORE INSERT ON PerformanceAppraisal +FOR EACH ROW +BEGIN + -- 如果绩效评分不在0到100之间,抛出错误 + IF NEW.ReviewScore < 0 OR NEW.ReviewScore > 100 THEN + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '绩效评分必须在0到100之间'; + END IF; +END // + +-- 4. 触发器:trg_after_insert_employee +-- 需求:当插入新员工记录时,自动创建一个默认的用户登录记录 +CREATE TRIGGER trg_after_insert_employee +AFTER INSERT ON Employee +FOR EACH ROW +BEGIN + -- 插入新的用户登录记录 + INSERT INTO UserLogin (EmployeeID, Username, Password, AccountStatus) + VALUES (NEW.EmployeeID, CONCAT('user_', NEW.EmployeeID), 'default_password', '有效'); +END // + +-- 5. 触发器:trg_before_delete_attendance +-- 需求:在删除考勤记录前,检查是否存在未处理的加班记录。如果有,则不允许删除该记录 +CREATE TRIGGER trg_before_delete_attendance +BEFORE DELETE ON Attendance +FOR EACH ROW +BEGIN + -- 检查该记录是否存在加班时长 + IF OLD.OvertimeHours > 0 THEN + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '存在未处理的加班时长,不能删除考勤记录'; + END IF; +END // + + +DELIMITER ; diff --git a/sql_scripts/view_creation_scripts.sql b/sql_scripts/view_creation_scripts.sql new file mode 100644 index 0000000..243259f --- /dev/null +++ b/sql_scripts/view_creation_scripts.sql @@ -0,0 +1,78 @@ +-- 文件名:view_creation_scripts.sql +-- 这是为员工考勤管理系统编写的视图创建脚本 + +-- 1. 员工考勤视图 +CREATE VIEW EmployeeAttendanceView AS +SELECT + E.Name AS EmployeeName, + A.AttendanceDate, + A.CheckInTime, + A.CheckOutTime, + A.WorkingHours, + A.OvertimeHours, + A.AttendanceStatus +FROM + Employee E +JOIN + Attendance A ON E.EmployeeID = A.EmployeeID; + +-- 2. 部门员工视图 +CREATE VIEW DepartmentEmployeeView AS +SELECT + D.DepartmentName, + E.Name AS EmployeeName, + E.Position, + E.HireDate, + E.EmploymentStatus +FROM + Department D +JOIN + Employee E ON D.DepartmentID = E.DepartmentID; + +-- 3. 假期申请状态视图 +CREATE VIEW LeaveRequestStatusView AS +SELECT + E.Name AS EmployeeName, + L.LeaveType, + L.LeaveDays, + L.RequestStatus, + A.Name AS ApproverName +FROM + LeaveRequest L +JOIN + Employee E ON L.EmployeeID = E.EmployeeID +LEFT JOIN + Employee A ON L.ApproverID = A.EmployeeID; + +-- 4. 绩效考核视图 +CREATE VIEW PerformanceReviewView AS +SELECT + E.Name AS EmployeeName, + P.ReviewDate, + P.ReviewPeriod, + P.ReviewScore, + P.ReviewGrade, + A.Name AS ReviewerName +FROM + PerformanceAppraisal P +JOIN + Employee E ON P.EmployeeID = E.EmployeeID +LEFT JOIN + Employee A ON P.ReviewerID = A.EmployeeID; + +-- 5.部门详细信息视图 +CREATE VIEW DepartmentDetailsView AS +SELECT + D.DepartmentID, + D.DepartmentName, + D.ManagerID, + M.Name AS ManagerName, + COUNT(E.EmployeeID) AS TotalEmployees +FROM + Department D +LEFT JOIN + Employee M ON D.ManagerID = M.EmployeeID +LEFT JOIN + Employee E ON D.DepartmentID = E.DepartmentID +GROUP BY + D.DepartmentID, D.DepartmentName, D.ManagerID, M.Name;