Database Principle and Application Experiment Report - Experiment 10 - Trigger

Database Principle and Application Experiment Report

 Experiment Topics   Experiment 10 Triggers  

10.1 Purpose of experiment

Through experiments, students can deepen their understanding of data integrity and learn to understand, create and use triggers.

10.2 Experiment content   (using the Teacher table of Experiment 9)

(1) Create a trigger T1 for the Teacher table. When inserting or updating data in the table, ensure that the Tage value of the operated record is greater than 0.

(2) Create a trigger T2 for the Teacher table, prohibiting the deletion of the CEO whose number is 00001.

(3) The number of the person in the Teacher table is unique and unchangeable. Create trigger T3 to realize the immutability of the number in the update.

(4) Demonstrate an insert operation that violates the constraints of the T1 flip-flop.

(5) Demonstrate an update operation that violates the constraints of the T1 trigger.

(6) Demonstrate an insert operation that violates the constraints of the T2 flip-flop.

(7) Demonstrate an update operation that violates the constraints of the T2 trigger.

10.3 Experimental procedure

(1) Still use the Teacher table in the custom integrity experiment . Create a trigger T1 for this table. When inserting or updating data in the table, ensure that the Tage value of the operated record is greater than 0.

Enter the following SQL statement in the new query window

USE University_Mis
GO
CREATE TRIGGER T1 ON Teacher
FOR INSERT,UPDATE
AS
IF(SELECT Tage FROM INSERTED)<1
BEGIN
PRINT ‘职工年龄必须是大于0的整数! 操作失败!’
ROLLBACK TRANSACTION
END

The command is successfully executed, as shown in Figure 1 below

 

figure 1

 (2) Create a trigger T2 for the Teacher table, prohibiting deletion of the CEO whose number is S01.

Enter the following SQL statement in the new query window

USE University_Mis
GO
CREATE TRIGGER T2 ON Teacher
FOR DELETE
AS
IF(SELECT Tno FROM DELETED)=’T01’
BEGIN
PRINT ‘此人是CEO! 删除操作失败!’
ROLLBACK TRANSACTION
END

The command is successful, as shown in Figure 2

 

figure 2

(3) The number of the person in the Teacher table is unique and unchangeable. Create trigger T3 to realize the immutability of the number in the update.

Enter the following SQL statement in the new query window

USE University_Mis
GO
CREATE TRIGGER T3 ON Teacher
FOR UPDATE
AS
IF UPDATE(Tno)
BEGIN
PRINT ‘职工编号不能修改!’
ROLLBACK TRANSACTION
END

The command is successful, as shown in Figure 3

 

image 3

(4) Enter the following SQL statement in the new query window

USE University_Mis
INSERT INTO Teacher VALUES(‘T03’,’ 李宏’,’F’,-10,’开发部’)

Inserting a tuple failed because the age limit must be greater than 0, and here is -10. As shown in Figure 4

 

Figure 4

wrong information:

Employee age must be an integer greater than 0! Operation failed!
Msg 3609, Level 16, State 1, Line 1 Transaction
ended in trigger. batch aborted

(5) Enter the following SQL statement in the new query window

USE University_Mis
UPDATE Teacher SET Tage=-7 WHERE Tno=’T01’

Failed to update data, because the age limit must be greater than 0, and here is -7, as shown in Figure 5 below

 

Figure 5

     wrong information:

     Employee age must be an integer greater than 0! Operation failed!

Msg 3609, Level 16, State 1, Line 1 Transaction
ended in trigger. Batch processing has been aborted.

(6) Enter the following SQL statement in the new query window

USE University_Mis
DELETE FROM Teacher WHERE Tname=’李用’

The deletion failed, because the deletion of the CEO is restricted, and Li Yong is the CEO, and the error is reported as shown in Figure 6

 

Figure 6

wrong information:

This person is the CEO! Delete operation failed!
Msg 3609, Level 16, State 1, Line 1 Transaction
ended in trigger. Batch processing has been aborted.

(7) Enter the following SQL statement in the new query window

USE University_Mis
UPDATE Teacher SET Tno=’T07’ WHERE Tsex=’F’

Failed to update the tuple, because the Tno attribute cannot be modified due to restrictions, an error is reported, as shown in Figure 7

 

Figure 7

wrong information:

The employee number cannot be modified!
Msg 3609, Level 16, State 1, Line 1 Transaction
ended in trigger. Batch processing has been aborted.

10.4 Experimental Requirements

(1) Before the experiment, please read the general requirements and instruction manual of the experiment carefully

(2) In the SSMS environment of SQL Server 2005 or 2008 or 2014, complete all SQL data definition operations in steps (1)-(7) of the above experiment, and paste the operation window of the steps in bold into the experiment report.

10.5 Experiment experience _

1) Experiment reflection

  none

2) Experimental Harvest

The trigger will give the user the necessary prompt information when the system detects that there is a violation of the integrity constraint in the data, and it will also cause certain operations within the system to automatically eliminate the negative impact caused by the violation of the integrity constraint, which is safe. protection function.

appendix:

none

Guess you like

Origin blog.csdn.net/cangzhexingxing/article/details/125563847