Toggle navigation
TechnoWikis.com
Email or Username
Password
Remember
Login
Register
|
I forgot my password
All Activity
Questions
Hot!
Pending
Ask a Question
Privacy Policy
Contact
Advanced Mysql - Triggers Programming
Home
Tutorials
Databases
Advanced Mysql - Triggers Programming
(adsbygoogle = window.adsbygoogle || []).push({});
+5
votes
262
views
Advanced Mysql - Triggers Programming
asked
Jun 24, 2019
in
Databases
by
backtothefuture
(
552k
points)
reopened
Jun 24, 2019
|
262
views
answer
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Add answer
Cancel
1
Answer
+3
votes
Best answer
Advanced Mysql - Triggers Programming
A trigger (Triggers) is an object within the database that executes an action when an operation or event has occurred in the database.
Example when an entry ticket is sold, we discount an available locality.
The generic code is
CREATE TRIGGER name
trigger
{BEFORE |
DESPUES DE }
// Will be executed before or after the event
{INSERT |
UPDATE |
DELETE}
/
/ action or event triggered by the trigger
ON tablename
// name of the table that affected the event
FOR EACH ROW
sql sentence that will be executed
We created the example of ticket sales for an event or product in stock.
To test this you can use phpmyadminn or any software that supports stored procedures and triggers.
We create the database
CREATE DATABASE `sales`
We create 2 tables
The table to store the tickets sold
CREATE TABLE `tickets` (
`id` int (10) NOT NULL AUTO_INCREMENT,
`idevento` int (11) NOT NULL,
`nroticket` int (11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;
The table to store the availability of tickets for a specific event
CREATE TABLE IF NOT EXISTS `stock` (
`idevento` int (10) NOT NULL,
`stockactual` int (10) NOT NULL
) ENGINE = MyISAM DEFAULT CHARSET = latin1;
CREATE TABLE `events` (
`idevento` int (10) NOT NULL AUTO_INCREMENT,
`event` int (11) varchar (200),
`date` date NOT NULL,
PRIMARY KEY (`idevento`)
) ENGINE = MyISAM DEFAULT CHARSET = latin1 AUTO_INCREMENT = 1;
We create the trigger as an sql query, in this case we use the free software HEIDISQL, the sentence NEW.nombre_column
a indicates which field of the trigger operation we are going to use in the execution in this case idevento, if there were more I always use them with NEW in front,
CREATE TRIGGER `update_stock` AFTER INSERT ON` tickets`
FOR EACH
ROW
UPDATE stock SET stockactual = stockactual -1 WHERE idevento = NEW.idevento
:
We execute the SQL statement and we see that the trigger has indeed been created
:
As an example we insert in the stock table of 500 tickets available for an event, here there will be no response from the trigger since we create it to run if an insertion occurs in the tickets table.
INSERT INTO `sales`.`stock` (` idevento`, `stockactual`) VALUES ('1', '500');
Now let's try the magic of the triggers
Suppose we sell ticket number 100 for event 1 and insert the sale in the database tickets
INSERT INTO tickets (idevento, nroticket) VALUES ('1', '100');
Observe what happened in the stock table and we will see that the stock for this event now has 499 tickets available, because the insertion in the
ticket
table triggered and I executed the tigger updates_stock
.
The use of this methodology allows transparency of execution both to the user and the programmer, knowing that an action will be executed if another triggers it and thus avoid routine tasks such as updating a stock in case of sales, imagine this if the sale is a market with thousands of product, we see how with a few lines of code we solve a big problem.
Another example could be to remove a product from the warehouse and remove the stock
CREATE TRIGGER `Low_products` AFTER DELETE ON` products`
FOR EACH
DELETE FROM stock WHERE idproduct = NEW.idproduct
ROW
The possibilities are endless and you can go deep into more complex situations, triggers or triggers are a great complement to stored procedures
answered
Jun 24, 2019
by
stackoverflow
(
3.5m
points)
edited
Jun 24, 2019
ask related question
comment
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Add comment
Cancel
Related questions
+4
votes
1
answer
Mysql triggers or trigger - Defining events and moments
asked
Jun 23, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
234
views
+4
votes
1
answer
Disable MySQL Installer is running in Community mode Windows 10 CMD window | ManifestUpdate
asked
Feb 18, 2021
in
Databases
by
backtothefuture
(
552k
points)
|
397
views
+3
votes
1
answer
Uninstall MySQL on Ubuntu 21.04 | Completely
asked
Nov 13, 2020
in
Databases
by
backtothefuture
(
552k
points)
|
447
views
+5
votes
1
answer
How to install or uninstall MySQL in CentOS 8
asked
Sep 30, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
1.7k
views
+5
votes
1
answer
Start, stop or restart MySQL service on Linux, Windows or mac from console
asked
Aug 24, 2019
in
Databases
by
backtothefuture
(
552k
points)
|
1.3k
views
Sponsored articles cost $40 per post. You can contact us via
Feedback
All categories
Tutorials
7.5k
Microsoft
1.9k
Android
1.4k
Security
120
Linux / Unix
543
Internet
757
Virtualization
104
Apple
611
Networks
64
Other Devices
321
Other Applications
184
Hardware
19
Development
53
Digital Marketing
47
Databases
14
Graphic Design
30
Guides
794
GraphicDesign
54
Networking
4
PlayStation
186
Gaming
55
Linux/Unix
85
Manzana
33
Otherdevices
38
Otherapps
49
Digitalmarketing
39
Safety
1
Developing
2
Help
685
Social Networks
34
Android Tutorial
549
iPhone Tutorial
267
News
17
Social
6
Phone
11
Telephone
9
Applications
167
Smartphones
3
Cell Phones
11
Applications
25
Travels
6
Photo
21
Education
18
Games
25
Internet
14
Music
8
Technique
10
Video
6
Windows
5
Apple
2
Cell Phones
3
TikTok
216
Trips
2
House
1
Operating System
5
Tips & Tricks
892
Solutions
6
Tutorials
3
FAQ
1
Applications
5
Cell Phones
2
Tutorials
4
Computers
6
Tutoriales
2
8
Technology
2
In Computers
1
In Applications
2
Tutorials Tutorials
9
Applications Applications
25
Aplicaciones Applications
4
Devices Devices
5
Tutoriales Tutorials
1
Tutorials u00a0 Tutorials
2
Applications u00a0 Applications
2
Devices u00a0 Devices
1
OS OS
1
Etc Etc
2
Most popular questions within the last 30 days
Please leave a comment about whether the solution works or not (with device model)
[X]Close
10,659
questions
10,791
answers
510
comments
3
users