Trigger to update value mysql - php

I am quite new to SQL and am working on a small project, but I got stuck when wanting to implement triggers to my model.
I am designing a database for a sports league. I have a table "GamePlayed" with a column called "GoalsScored" and another one "Team" with a column called "GoalsFor". Those two tables are connected by the "TeamID" key.
I would like to have a trigger that updates the value of "GoalsFor" by adding to the previous value the value of "GoalsScored". In this way, with every game, the total quantity of goals scored will be constantly updated.
I tried the following:
CREATE TRIGGER "Goalsscored" AFTER UPDATE
ON `GamePlayed`
FOR EACH ROW
BEGIN
UPDATE `Team` SET `Team`.`GoalsFor`=`Team`.`GoalsFor`+`GamePlayed`.`GoalsScored`
WHERE `GamePlayed`.`Team_TeamID`=`Team`.`TeamID`
END
Unfortunately, it says there is a problem in my SQL syntax...
How can I do it?

You need to change your delimiter. Something like this
delimiter $$
CREATE TRIGGER "Goalsscored" AFTER UPDATE
ON `GamePlayed`
FOR EACH ROW
BEGIN
UPDATE `Team` SET `Team`.`GoalsFor`=`Team`.`GoalsFor`+`GamePlayed`.`GoalsScored`
WHERE `GamePlayed`.`Team_TeamID`=`Team`.`TeamID`;
END;
$$
See eg http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

Related

Syncing two rows from different tables in the same MySQL database

I have a table named clients, in that table there's two columns of importance; id and client. I have a secondary table in the same database named calendar. I really want the two columns of id and client in the calendar table to sync with the ones in client table.
Right now I am using this PHP to execute this in MySQL:
INSERT IGNORE INTO calendar (id, client) SELECT id, client FROM clients;
Is there a better way of accomplish this task? Maybe a built in function in MySQL that I have overlooked or something like that?
Use Triggers : The MySQL trigger is a database object that is associated with a table. It will be activated when a defined action is executed for the table.
The trigger can be executed when you run one of the following MySQL statements on the table: INSERT, UPDATE and DELETE and it can be invoked before or after the event.
You can make trigger when you insert or update a row in main table and make the changes in another table
Example:
DELIMITER $$
CREATE TRIGGER my_sync_trigger
AFTER INSERT ON `clients` for each row
begin
INSERT INTO calender (id,client)
Values (new.id, new.client);
END$$
DELIMITER ;
"new" stands for the new value inserted into clients table. The same value will be inserted into id and client column in calender.
Note: single quotes are removed from table name because quotes effectively make it a string literal instead of a proper identifier.
DELIMITER command will change the ending of each statement from ";" to "$$" so that MySQL is not confused with ";" inside and outside the trigger
Make similar triggers for update and delete also
Simple guide for examples and syntax:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx

How do I get the randomly generated primary key of an inserted row in MariaDB/MySQL?

Okay, I am currently developing a website that is supposed to have a searchable database of pool pumps. As part of this system, to prevent people from reading hidden data, I had the primary key of the pool pump stock randomly generated. Here's the code I wrote for the MariaDB backend:
DELIMITER $$
CREATE TRIGGER random_pump_id BEFORE INSERT ON tbl_stock FOR EACH ROW
BEGIN
DECLARE temp_id MEDIUMINT;
REPEAT
SET temp_id = FLOOR(RAND() * 16777216);
UNTIL (SELECT COUNT(*) FROM tbl_stock WHERE pump_id = temp_id) <= 0 END REPEAT;
SET NEW.pump_id = temp_id;
END
$$
But now I've run into a dilemma. Every time I want to insert a row, I need a way to retrieve the primary key I just generated. I know if I used AUTO_INCREMENT I could use the LAST_INSERT_ID function, or lastInsertId in PDO. But since I am not using AUTO_INCREMENT, and instead am using a separate trigger, these functions will only return a 0. I know I can do it in PostgreSQL by using the RETURNING clause, but I can't find a way to accomplish this in MariaDB.
Does anyone know of any solution? Perhaps some obscure trigger I don't know about? Please?
Presumably you have some other way to locate the record? Probably via a UNIQUE key? If so, the fetch the row after adding the random id.
Don't use a trigger, instead write application code. (Triggers can't solve all problems.)

PHP MyAdmin After Update Trigger not working

I am using PHP MyAdmin Version 4.1.12.
I am trying to create a simple trigger that, after an update, sets 'dateModified' in table 'person' to CURRENT_TIMESTAMP. dateModified is of type TIMESTAMP. The way in which the update occurs to person is the setting of a single attribute in a single record through a X-Editable enabled grid view on a web page. After performing validation against the model with the updated attribute, a new database command is created with the relevant update SQL and executed. So each update only ever modifies a single row within 'person'.
Here is the SQL I wrote to create the trigger:
DELIMITER |
CREATE TRIGGER PERSON_AUPD AFTER UPDATE ON person
FOR EACH ROW BEGIN
SET #dateModified = CURRENT_TIMESTAMP ;
END;
|
DELIMITER ;
After performing updates, I see that the trigger hasn't fired, and the timestamp remains unchanged from the one they were created with (the default for dateModified, and dateCreated, are both CURRENT_TIMESTAMP, so they get set automatically on insert).
I have looked around for answers, and even looked into alternate methods to getting the update (the alternate method was calling a model's afterupdate method and performing separate SQL there on dateModified). I would prefer to exhaust every opportunity to use the triggers, before I go putting more code into my model.
Any help would be greatly appreciated.
Thanks to #juergend, the solution was the following:
Set trigger type to before update because after update cannot update attributes, and are best used to insert new records in related audit tables, etc.
Add NEW. to the front of the attribute you wish to modify.

php mysql - update multiple rows in a table

Ok, i have a checklist system that i am working on. There is a page that pulls data from the database and displays it as a checklist. I am trying to create a button that when pushed will reset the database to a certain state that i want. Basically, i have a button that sends an ajax callout to a php page that executes an UPDATE query. This query is as follows:
UPDATE $table SET value='$value', comments='$comments', editedBy='$editedBy', editedDate='$editedDate' WHERE projectId='$projectId';
I set the variables first of course, that's not my question. Just pretend they have data. My question is how can i repeat this query so that every row table x that has a projectId of n is updated? I'm guessing this involves a for loop?
SIDE NOTE: Since this query is just setting the value to false and making the comments, editedBy, and editedDate fields blank for every row in table x that has a projectId of n, is there a better way of doing this other than the UPDATE query?
Thanks for any help!
As long as you don't sepcify a LIMIT in your UPDATE query, it will update every row it finds that satisfies your where clause.
Now, if you're updating the projects table and projectID is your primary key, you'll need to run a loop to update other projectIDs. If you're not updating the project table, then your update query will update any record that has a foreign key match to the project ID you specified.
Does that help?

incrementing a field in a table when insertion in another table occurs

Let's say I have two tables as shown:
user
id plan course_limit username
10 0 ahmad
note: plan is enum field containing '','a','b','c'.
course
id user_id username
1 10 ahmad
Now I want when a user insert into course as shown I want the course_limit to increment by 1 for that user so that I can apply a limit.
You can create a trigger with the following code.
CREATE
DEFINER = 'root'#'localhost'
TRIGGER databasename.AI_course_each
AFTER INSERT
ON databasename.course
FOR EACH ROW
BEGIN
UPDATE user SET user.course_limit = user.course_limit + 1
WHERE user.user_id = new.user_id;
END;
Some explanation
You create a trigger that fires once for every row FOR EACH ROW that is inserted AFTER INSERT into table course.
When you insert into the table you can trigger BEFORE and AFTER the insert is done.
If you want to be able to prevent the insert you fire before, if you just want to do useful work, you fire after.
The inserted fields can be accessed via a dummy table 'new'.
So here after each insert the UPDATE statement gets executed.
More trigger options
You can also use triggers BEFORE UPDATE, AFTER UPDATE,BEFORE DELETE and AFTER DELETE.
In the update and delete cases you get an extra dummy table old that you can use to refer to the data in the table before the update or delete happened.
Lots more is possible, but I'll keep it simple for now.
See http://forge.mysql.com/wiki/Triggers for more info.
Using a trigger should solve your problem : with that, you'll be able to register SQL code, on your MySQL server, that runs when certain events occur (like insertion).

Categories