Introduction to MySQL triggers
This article mainly introduces MySQL triggers, including using triggers to add, update, and delete users. For more information, see
In many cases, it is better to understand the benefits. The solution should be designed based on the scenario, rather than relying on experience blindly. Of course, this is a new experience!
The requirement is to make several public systems of the company pass the mailbox user name and password authentication. You only need to remember a pair of user name and password. Simply put, you can only change the password on the ExtMail web page! During Forum authentication, due to the complexity of the forum, I thought of the following solutions:
I have only heard of the authentication through OAuth2.0 or write the interface myself. I have never played it!
When the mailbox is modified, it is also submitted to the Forum database. Unfortunately, it's not ExtMail's Perl code, and it's too much work to modify the code!
Previously, the online mailbox User table has been synchronized to the Intranet and can be used for database replication. The database fields in the Forum and mailbox are greatly different. You still need to change a lot of code!
Use a trigger to update the user table of the Forum database!
After careful consideration of the preceding three methods, the Forum authentication method compiled by PHP has been changed to the salt authentication method adapted to the mailbox within the capability scope, the user name and password of the imported mailbox are verified. The field type of the Forum database has been modified. Some back-end codes are available! The last step is to automatically synchronize the changes to the user table of the mailbox database to the user table of the Forum database.
When discussing database replication, we found that the last trigger method was the most feasible. By setting the default value for most fields in the forum user table, we can only add and modify the account, the password and email fields can be used to complete operations on Forum users. The following is a MySQL trigger compiled based on the actual situation!
Trigger Add User
?
1 2 3 4 5 6 7 8 9 10 |
Use extmail; DELIMITER // Create trigger add_bbsuser After insert on extmail. mailbox For each row Begin Insert into xiuno_bbs.bbs_user (username, password, email )\ Values (new. name, new. password, new. username ); End // DELIMITER; |
Trigger update user
?
1 2 3 4 5 6 7 8 9 10 |
Use extmail; DELIMITER // Create trigger update_bbsuser After update on extmail. mailbox For each row Begin Update xiuno_bbs.bbs_user set username = new. name ,\ Password = new. password where email = new. username; End // DELIMITER; |
Trigger delete user
?
1 2 3 4 5 6 7 8 9 |
Use extmail; DELIMITER // Create trigger delete_bbsuser After delete on extmail. mailbox For each row Begin Delete from xiuno_bbs.bbs_user where email = old. username; End // DELIMITER; |
Pitfall I step on
When I first wrote a trigger, I couldn't use any commands I found on the Internet. I realized that the trigger is bound to the database and must use extmail TO THE extmail database to be triggered, in order to trigger operations on this database, there will be no problem later! I am a little white MySQL!