Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use trigger in MariaDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use triggers in MariaDB". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use triggers in MariaDB.

A Trigger is an action triggered by events, including INSERT statements' UPDATE statements and DELETE statements. When the database executes these events, the trigger is activated to perform the response operation.

1. The basic form of creating a trigger with only one execution statement in MariaDB is:

CREATE TRIGGER trigger name BEFORE | AFTER trigger event ON table name FOR EACH ROW execute statement

The trigger name parameter refers to the name of the trigger to be created: the BEFORE and AFTER parameters specify the trigger execution time, BEFORE refers to the execution of the trigger statement before the trigger event, and AFTER indicates the execution of the trigger statement after the trigger event. Trigger events include: INSERT, UPDATE, and DELETE. The table name parameter refers to the name of the table in which the trigger event occurred. FOR EACH ROW means that any action on a record satisfies the trigger event will trigger the trigger, and the execution statement parameter refers to the program executed after the trigger is triggered.

For example, the following code:

CREATE TRIGGER login_trigger AFTER INSERT ON login FOR EACH ROW INSERT INTO trigger_psd VALUES (NOW ())

2. Create a trigger with multiple execution statements

Triggers may execute multiple statements, and the basic form of creating a trigger with multiple execution statements is:

CREATE TRIGGER trigger name BEFORE | AFTER trigger event ON table name FOR EACH ROW BEGIN execution statement list END

Among them: the "execution statement list" parameter between BEGIN and END indicates the contents of multiple execution statements that need to be executed, and different execution statements are separated by semicolons.

For example:

DELIMITER & & CREATE TRIGGER dept_trig2 AFTRE DELETE ON department FOR EACH ROW BEGIN INSERT INTO trigger_time values ("21:01:01"); INSERT INTO trigger_time values ("22:01:01"); END & & DELIMITER

Note: in MySQL (MariaDB), only one trigger can be created for the same trigger event of a table at the same trigger time. For example, in the department table, when the event INSERT is triggered, there can only be one trigger with a trigger time of AFTEER. However, you can define a trigger whose trigger event is BEFORE. If the table China executes the INSERT statement, then the trigger executes automatically.

3. View triggers

You can use the SHOW TRIGGERS statement in MariaDB to view the basic information of a trigger, which is in the following form:

SHOW TRRIGERS

4. View the trigger information in the triggers table

All triggers in MariaDB are defined in the triggers table under the information_shema database. Query the triggers table to see all the trigger information in the database. The code is as follows:

SELECT * FROM information_schema.triggers

The trigger code for querying a specific name is:

SELECT * FROM information_schema.triggers where TRIGGER_NAME=' trigger name'

5. Delete trigger

The code to delete a trigger that already exists in the database is as follows:

DROP TRIGGER trigger name

For example:

DROP TRIGGER dept_trig1; so far, I believe you have a deeper understanding of "the use of triggers in MariaDB". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report