Database system (four)-trigger

1. The purpose of the experiment:

  1. Familiar with data integrity control through SQL;

  2. Practice integrity control methods in practical applications;

  3. Practice the creation and use of triggers.

2. Experimental content:

Based on a certain employee management database, use triggers to achieve data integrity control and complete the following functions:

  1. Create a trigger to record the initial department and salary information of the employee in the change history table when the employee enters the job;

  2. When creating triggers to implement employee information changes, record the employee's department or salary changes to the change history table;

  3. Create a trigger to realize that when an employee resigns, the salary before resignation is automatically backed up to the change history table.

3. Topic:

In an employee information management system, when employees enter, leave, transfer departments or salary changes, the change information needs to be recorded in the change history. Please use the trigger to complete this function. The change history includes department change history and salary change history. The department change history table and the salary change history table are used to record the corresponding information.

The structure of the reference table is as follows:

  1. Employee table: employee (eid, ename, dept, salary, uptime, stutus)
    where eid is the employee number, ename is the employee’s name, dept is the department, salary is the salary, uptime is the modification time, and stutus is the status (1 means active, 0 means resignation).

  2. Department change history table: dept_history(dhid, eid, old, new, uptime),
    where dhid is the department change number (automatic growth, no need to assign), eid is the employee number, and old is the department before the transfer (the old value of newly recruited employees is recorded) Is NULL), new is the department after transfer, and uptime is the modification time.

  3. Salary change history table: sal_history(shid, eid, old, new, uptime)
    where shid is the salary change number (automatically increased, no assignment is required), eid is the employee number, old is the salary before the change, and new is the salary after the change. Uptime is the modification time.

The trigger requirements are as follows:

  1. When a new employee enters a job, one piece of data will be inserted into the employee information table. At the same time, the trigger adds 1 record to the department change history, where the old value is null; adds 1 record to the salary change history, where the old value is 0.

  2. When a new employee's department or salary changes, the trigger performs the following actions. If the department changes, one record is added to the change history; if the salary changes, one record is added to the wage change history.

4. Experimental process:

Create a database:

CREATE DATABASE emp;

-- 创建员工表
CREATE TABLE employee (
eid CHAR (6) PRIMARY KEY,
ename VARCHAR (10) UNIQUE,
dept VARCHAR (10) UNIQUE,
salary INT,
uptime datetime,
status char(1)
) 

--插入数据
INSERT INTO employee VALUES( '1001', 'zs', '软开', 3000, CURRENT_TIMESTAMP, '1' );
	
-- 创建部门变动历史表
CREATE TABLE dept_history (
	dhid INT PRIMARY KEY AUTO_INCREMENT,
	eid CHAR (6),
	old VARCHAR (10),
	new VARCHAR (10),
	uptime datetime 
) 

-- 创建工资变动历史表
CREATE TABLE sal_history ( 
shid INT PRIMARY KEY AUTO_INCREMENT, 
eid CHAR (6), 
old INT, 
new INT, 
uptime datetime 
)

Question 1: When a new employee enters a job, one piece of data will be inserted into the employee information table. At the same time, the trigger adds 1 record to the department change history, where the olddept value is null; adds 1 record to the salary change history, where the oldsal value is 0.

Create insert trigger:

DELIMITER $
CREATE TRIGGER emp_insert
AFTER INSERT ON employee
FOR EACH ROW
BEGIN
INSERT INTO dept_history ( eid, old, new, uptime )
	VALUES( new.eid, NULL, new.dept, new.uptime );
	INSERT INTO sal_history ( eid, old, new, uptime )
	VALUES( new.eid, 0, new.salary, new.uptime );
END$
DELIMITER ;

Question 2: When a new employee's department or salary changes, the trigger performs the following operations. If the department changes, one record is added to the change history; if the salary changes, one record is added to the wage change history.

Create update trigger:

DELIMITER $
CREATE TRIGGER emp_update
AFTER UPDATE ON employee
FOR EACH ROW
BEGIN
 	IF
	(old.dept != new.dept) THEN
	INSERT INTO dept_history ( eid, old, new, uptime )
	VALUES(new.eid, old.dept, new.dept, new.uptime );
	END IF;
	IF
	(old.salary != new.salary) THEN
	INSERT INTO sal_history ( eid, old, new, uptime )
	VALUES(new.eid, old.salary, new.salary, new.uptime );	
	END IF;
END$
DELIMITER ;

5. Summary of the experiment:

Trigger trigger time is divided into before and after. As the name implies, before represents the command in the trigger is executed before the data is modified, and after represents the command in the trigger is executed after the data is modified.

Guess you like

Origin blog.csdn.net/qq_43531669/article/details/111647383