1、创建一个数据库 staff,并在数据库中创建以下四张表:
员工表 employee(员工编号id,姓名userName,出生日期birthDate,身份证号idCard,登录名称loginName,登录密码password,手机号mobile,电子邮件email,部门编号deptId,员工级别level,员工头像avatar,备注remark)
部门表 dept(部门编号id,部门名称deptName,部门经理编号managerId)
工资表payroll(工资编号id,员工编号empId,基本工资baseSalary,应发工资actualSalary,奖金bonus,缺勤扣钱deductMoney,薪资发放日期grantDate)
请假表ask_leave(请假编号id,员工编号empId,请假原因leaveReason,请假开始时间beginDate,请假结束时间endDate,提交时间submitDate,审核人编号auditId(该列有触发器维护),申请状态status,审核意见auditOpinion)
基础数据自己填写。
CREATE DATABASE staff;
USE staff
create table employee(
id int not NULL auto_increment,
userName varchar(255),
birthDate DATE,
idCard VARCHAR(255),
loginName VARCHAR(255),
PASSWORD VARCHAR(255),
mobile VARCHAR(255),
email VARCHAR(255),
deptId INT,
LEVEL INT,
avatar BLOB,
remark TEXT,
PRIMARY KEY(id)
);
CREATE TABLE dept(
id INT NOT NULL AUTO_INCREMENT,
deptName VARCHAR(255),
manageId INT,
remark VARCHAR(255),
PRIMARY KEY(id)
);
CREATE TABLE payroll(
id INT NOT NULL AUTO_INCREMENT,
empId INT,
baseSalary DOUBLE,
actualSalary DOUBLE,
bonus DOUBLE,
deductMoney DOUBLE,
grantDate DATE,
PRIMARY KEY(id)
);
CREATE TABLE ask_leave(
id INT NOT NULL AUTO_INCREMENT,
empId INT,
leaveReason TEXT,
beginDate DATE,
endDate DATE,
submitDate DATE,
auditId INT,
STATUS INT,
auditOpinion TEXT,
PRIMARY KEY(id)
);
2、编写存储过程实现插入员工表:参数为:员工编号id,姓名userName,出生日期birthDate,身份证号idCard,登录名称loginName,登录密码password,手机号mobile,电子邮件email,部门编号deptId,员工级别level,员工头像avatar,备注remark。
存储过程名称为:insert_employee。
DELIMITER $$
CREATE
procedure `staff`.`insert_employee`( IN id INT, IN userName VARCHAR(255),
IN birthDate DATE,
IN idCard VARCHAR(255),
IN loginName VARCHAR(255),
IN PASSWORD VARCHAR(255),
IN mobile VARCHAR(255),
IN email VARCHAR(255),
IN deptId INT,
level INT,
IN avatar BLOB,IN remark TEXT)
BEGIN
insert into `employee` VALUES(NULL,userName,birthDate,idCard,
loginName,PASSWORD,mobile,email,deptId,LEVEL,avatar,remark);
END$$
DELIMITER ;
3、利用存储过程在员工表中插入5条记录。
CALL `insert_employee`(NULL,'aa','2001-1-1','41032222222222222','abc','1234','123445555','123@qq.com',1,1,NULL,NULL);写5条即可
4、创建触发器,当插入或修改工资表payroll时,应发工资自动为“基本工资+奖金-缺勤扣钱”。
DELIMITER $$
CREATE
TRIGGER `staff`.`beforeinsert_payloyee` BEFORE INSERT
ON `staff`.`payroll`
FOR EACH ROW BEGIN
set new.`actualSalary`=new.`baseSalary`+new.`bonus`-new.`deductMoney`;
END$$
DELIMITER ;
DELIMITER $$
create
trigger `staff`.`before_update_payloyee` BEFORE UPDATE
ON `staff`.`payroll`
FOR EACH ROW BEGIN
SET new.`actualSalary`=new.`baseSalary`+new.`bonus`-new.`deductMoney`;
END$$
DELIMITER ;
5、在员工表中依据姓名userName建立索引。索引名为:index_userName。
CREATE INDEX index_userName ON `employee`(`userName`);
6、建立员工部门工资视图(包含员工名称,部门名称,基本工资,应发工资,奖金,缺勤扣钱)视图名称:v_employee_dept_payroll。
CREATE VIEW v_employee_dept_payroll
AS
select `userName`,`deptName`,`baseSalary`,`actualSalary`,`bonus`,`deductMoney`
FROM`dept`,`employee`,`payroll`
WHERE `dept`.`id`=`employee`.`deptId` AND `employee`.`id`=`payroll`.`empId`
7、利用触发器实现插入请假信息时,审核人编号自动填入请假人所在部门的部门经理编号。触发器名称为:insert_ask_leave。
DELIMITER $$
CREATE
trigger `staff`.`insert_ask_leave` BEFORE INSERT
ON `staff`.`ask_leave`
FOR EACH ROW BEGIN
DECLARE manangeID1 INT;
SELECT `manageId` INTO manangeID1 FROM `dept` WHERE `id`=(
SELECT `deptId` FROM `employee` WHERE `id`=new.`empId`);
SET new.`auditId`=manangeID1;
END$$
DELIMITER ;
8、备份数据库的结构和数据,导出SQL文件名为:staff_sjk.sql。
9、将staff_sjk.sql上传至服务器。