1. Verification experiment
A classmate defines the product table of product information, the main information includes: product number, product name, main function, manufacturer, c manufacturer address, the SQL code to generate the product table is as follows: CREATE TABLE product ( id INT(
10
) NOT NULL UNIQUE PRIMARY KEY ,
name VARCHAR(20) NOT NULL ,
function VARCHAR(50) ,
company VARCHAR(20) NOT NULL,
address VARCHAR(50)
);
When performing data operations on the product table, the contents of the operation and time to record. So the operate table is defined, and the SQL statement generated by the table is:
CREATE TABLE operate (
op_id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT ,
op_name VARCHAR(20) NOT NULL ,
op_time TIME NOT NULL
);
Please complete the following tasks:
1. Create three triggers BEFORE INSERT, AFTER UPDATE and AFTER DELETE on the product table respectively. The names of the triggers are Tproduct_bf_insert, Tproduct_af_update and Tproduct_af_del respectively. The part of executing the statement is to insert the operation method and operation time into the operate table.
(1) The SQL code for creating the Tproduct_bf_insert trigger is as follows:
CREATE TRIGGER Tproduct_bf_insert BEFORE INSERT
ON product FOR EACH ROW
INSERT INTO operate VALUES(null, 'Insert product', now());
(2) The SQL code for creating the Tproduct_af_update trigger is as follows:
CREATE TRIGGER Tproduct_af_update AFTER UPDATE
ON product FOR EACH ROW
INSERT INTO operate VALUES(null, 'Update product', now());
(3) The SQL code for creating the Tproduct_af_del trigger is as follows:
CREATE TRIGGER Tproduct_af_del AFTER DELETE
ON product FOR EA CH ROW
INSERT INTO operate VALUES(null, 'delete product', now());
2.Perform INSERT, UPDATE, and DELETE operations on the product table, and view the operate table separately.
(1) Insert a record into the product table: 1, 'abc', 'Cure a cold', 'Beijing abc Pharmaceutical Factory', 'Changping District, Beijing'
SQL code: INSERT INTO product VALUES(1, 'abc', 'Cure for colds', 'Beijing abc Pharmaceutical Factory', 'Beijing Changping District'); (2) Update the record and change the
address of the manufacturer whose product number is 1: Change It is "Beijing Haidian District".
SQL code: UPDATE product SET address='Beijing Haidian District' WHERE id=1;
(3) Delete the record whose product number is 1.
SQL code: DELETE FROM product WHERE id=1;
3. Delete Tproduct_bf_update trigger
DROP TRIGGER Tproduct_bf_insert;
2. Design experiment
1. Create three triggers, AFTER INSERT, BEFORE UPDATE, and BEFORE DELETE, on the product table. The names of the triggers are product_af_insert, product_af_update, and Tproduct_bf_del respectively. The part of executing the statement is to insert the operation method and operation time into the operate table.
create trigger product_af_insert after insert
on product for each row
insert into operate values(null,'insert product',now());
create trigger product_af_update before insert
on product for each row
insert into operate values(null,'update product',now());
create trigger product_bf_del before insert
on product for each row
insert into operate values(null,'delete product',now())
; See the basic structure of the product_bf_del trigger.
show triggers;
3.Perform the following INSERT, UPDATE, and DELETE operations on the product table, and view the operate table respectively.
INSERT INTO product VALUES(2, 'Zhixueling','Zhixueling', 'Beijing Zhixueling Pharmaceutical Factory','Beijing Changping District'); UPDATE product SET address
='Tianjin Development Zone' WHERE id=2;
DELETE FROM product WHERE id=2;
4.Remove the product_bf_update trigger.
drop trigger product_bf_update;
Trigger creation and management experiments
Guess you like
Origin blog.csdn.net/m0_55726741/article/details/129248831
Recommended
Ranking