Experiment 5 stored procedures and triggers

Experiment 5 stored procedures and triggers

  • Purpose
  1. Deepen your understanding of stored procedures and triggers
  2. Master the creation and use of stored procedures and triggers, and understand the differences in how they execute
  3. Understand and appreciate the difference and connection between stored procedures and triggers

2. Experimental content

Based on the established tables, create related stored procedures or triggers as needed to complete certain functions.

  1. Create an InsertS stored procedure to insert a record into S , and the value of the new record is provided by the parameter. And verify the execution of the stored procedure.

experiment procedure:

  1. Create a stored procedure:

DROP PROCEDURE IF EXISTS InsertS;

DELIMITER $$

CREATE PROCEDURE InsertS(in S_SNO CHAR(2),in S_SNAME CHAR(3),in S_STATUS CHAR(2),in C_CITY CHAR(2))

BEGIN

   insert into s VALUES(S_SNO,S_SNAME,S_STATUS,C_CITY);

END

$$

DELIMITER;

  1. callInsertS _

call InsertS('S8','Lean',50,'Beijing');

Experimental results:

  1. Create a stored procedure:

  1. callInsertS _

Result analysis:

The stored procedure is successfully created and can be directly called by the user to insert data

  1. Create a QuerySPJ stored procedure to query the used parts of the engineering project according to the engineering project code provided by the user, and return the total quantity of the used parts.

experiment procedure:

  1. Create a stored procedure:

DROP PROCEDURE IF EXISTS QuerySPJ;

DELIMITER $$

CREATE PROCEDURE QuerySPJ(in J_JNO CHAR(2),OUT QTYcount INT)

BEGIN

   SELECT jno,pno,QTY FROM spj WHERE JNO=J_JNO;

   SELECT sum(QTY) into QTYcount

   FROM spj

   GROUP BY JNO

   HAVING JNO=J_JNO;

END

$$

DELIMITER;

  1. Call QuerySPJ to query J4 :

SET @count=0;

call QuerySPJ('J4',@count);

SELECT @count;

Experimental results:

  1. Create a stored procedure:

  1. Call QuerySPJ to query J4 :

 

Result analysis:

      

It can be seen that the parts usage of J4 is indeed as shown in the results. See result 1 for the parts usage of J4, and see result 2 for the total number of parts. The total number of parts used in J4 is 800.

  1. Create a trigger so that when a record is deleted in the S table, the data in the SPJ table is also deleted accordingly. (Remove the foreign key relationship of the table before creating the trigger. This trigger only simulates the cascading delete operation)

experiment procedure:

  1. Remove foreign keys:

Sql statement method:

Alter table spj drop foreign key spj_ibfk_1;

Alter table spj drop foreign key spj_ibfk_2;

Alter table spj drop foreign key spj_ibfk_3;

Visual interface:

  1. Create a trigger:

CREATE TRIGGER delete_spj

AFTER

DELETE on s

for each ROW

BEGIN

DELETE FROM spj WHERE SNO=OLD.SNO;

END;

  1. Then run the delete statement:

DELETE from s where SNO='S3';

Experimental results:

  1. Remove foreign keys:

  1. Create a trigger:

  1. Then run the delete statement:

Result analysis:

In the absence of foreign keys, the s table is not associated with the spj table, but cascading deletion can be achieved through triggers. It should be noted that when the data in the s table is deleted, the data in the spj table will also be deleted accordingly.

 

 

  1. Add a column attribute in the S table, the attribute name is AvgQty , which indicates the average supply quantity of the supplier, and create a trigger so that when data is inserted into the SPJ table, AvgQty is also modified accordingly.

experiment procedure:

  1. Add a new attribute column:

alter table s add avgQty DOUBLE (10,0);

  1. Create a trigger:

CREATE TRIGGER spj_AvgQty

AFTER

INSERT on spj

for EACH ROW

BEGIN

DECLARE qtycount DOUBLE(10,0);

DECLARE count INT;

SET qtycount=0;

set count=0;

SELECT SUM(QTY) INTO qtycount FROM spj GROUP BY sno HAVING sno=new.sno;

SELECT count(*) INTO count FROM spj GROUP BY sno HAVING sno=new.sno;

set qtycount=qtycount/count;

UPDATE s SET AvgQty=qtycount WHERE sno=new.sno;

END;

  1. test:

INSERT INTO spj VALUES('S3','P1','J6',200);

Experimental results:

  1. Add new property column:

  1. Create a trigger:

  1. test :

Result analysis:

Insert a ('S3', 'P1', 'J6', 200); the data in the AvgQty data column in the s table changes accordingly.

  1. Create a supply history table hspj(sno,pno,jno,qty,username,modifydate) to record supply changes. Realize such constraint control: if a supply record in the spj table changes, insert a row of historical records into the hspj table, where username is the user name of the operation, and modifydate is the time of the operation.

experiment procedure:

  1. Create a hspj table:

DROP TABLE  if EXISTS hspj;

CREATE TABLE hspj(

sno char(2),

pno char(2),

jno char(7),

qty INT,

username CHAR(20),

modifydate TIMESTAMP,

PRIMARY KEY(sno,pno,jno)

);

  1. Create a trigger:

CREATE TRIGGER spj_hspj

AFTER

INSERT on spj

FOR EACH ROW

BEGIN

INSERT into hspj() VALUES(new.sno,new.pno,new.jno,new.qty,CURRENT_USER,CURRENT_TIMESTAMP);

end;

  1. test:

INSERT INTO spj() VALUES('S7','P1','J2',200);

Experimental results:

  1. Create a hspj table:

 

 (2) Create a trigger:

 (3) Test:

Result analysis:

        When inserting a piece of data ('S7', 'P1', 'J2', 200);, the hspj table will insert a piece of operation information.

Guess you like

Origin blog.csdn.net/weixin_41957626/article/details/129740157