After insert trigger can do an update? - mysql

I'm new to the creation of triggers but I need it since I'm using a persistent storage for some IDs.
I have the table RECEIPT with a column RECEIPT_ID
and the table CONFIG_DB with columns and values:
NAME VALUE
--------------- -----
NEXT_RECEIPT_ID 1
and I created this trigger
CREATE TRIGGER UPDATE_RECEPCION_ID
AFTER INSERT ON RECEIPT
FOR EACH ROW
BEGIN
UPDATE CONFIG_DB SET VALUE=VALUE+1;
END
;
I don't know if this is OK... all I want its that after I insert a new RECEIPT_ID the VALUE in CONFIG_DB increases by 1. Thank you very much.
EDIT: I work in Mysql Workbench 5.2.40 with Mysql Server 5.5.25

Use following query to create trigger
delimiter |
CREATE
TRIGGER `UPDATE_RECEPCION_ID`
AFTER INSERT ON `RECEIPT`
FOR EACH ROW BEGIN
UPDATE CONFIG_DB SET VALUE=VALUE+1
WHERE NAME='NEXT_RECEIPT_ID';
END;
|

Related

MySQL Trigger to Update a field in one table after inserting into another

Tables
I have the two tables above. In tblUniformAndMaterials the field AllocatedMaterials is populated from a drop down list which is fed by from tblMaterials. Once selected the field MaterialID in tblUniformAndMaterials is Auto Populated from tblMaterials.
What I want to accomplish by using a Trigger is after the record in tblUniformAndMaterials is inserted I want to update the NiveauDeStock field in tblMaterials to (NiveauDeStock-1). In other words after each material allocation I reduce the stock level by one.
Reference for CREATE TRIGGER https://dev.mysql.com/doc/refman/5.7/en/create-trigger.html
Try this:
DELIMITER $$
CREATE TRIGGER tblUniformAndMaterials_ai
AFTER INSERT
ON tblUniformAndMaterials
FOR EACH ROW
BEGIN
UPDATE tblMaterials
set NiveauDeStock = NiveauDeStock -1
WHERE CodeDeMaterial = NEW.MaterialID;
END;
$$
DELIMITER ;
Note AFTER INSERT and how NEW.MaterialID is the ID inserted into tblUniformAndMaterials.
Good Luck!

Firing a MySQL trigger on existing table upon creation of trigger

I have a table with 2 columns named "Table1":
(Column 1 named "Col1" with the values: A,B,C,D,E,F)
(Column 2 named "Col2" with the values: 12,15,2,5,200,1).
I would like get all the values from column 2 to change to the value 1 if their value is lower than 100, so that column 2 will eventually look like this:
(Column 2 named "Col2" with the values: 1,1,1,1,200,1).
I tried to create a trigger:
delimiter //
CREATE TRIGGER Table1upd BEFORE UPDATE ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;
When creating the trigger in MySQL workbench it says that the trigger was added but that 0 row(s) affected.
I assume my problem is with the BEFORE UPDATE choice, because the trigger does work when I update a value in the table, but I don't know what to change it to so that the trigger will also initially execute automatically when I create it.
Thank you in advance for any help,
D
The answer is simple: a BEFORE UPDATE trigger only triggers on UPDATE, so you need a second trigger BEFORE INSERT to cover both use cases: update and insert:
delimiter //
CREATE TRIGGER Table1insert BEFORE INSERT ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;

Created TRIGGER for two tables but getting error message

I have 2 tables:
ORDERS - Stores all the information from my shopping cart
ITEMS - Stores id | scode | product | price | saleprice | inventory
I have come up with the trigger below, when an order is inserted into the database in 'orders' I want it to update the 'inventory' quantity by -1 in the 'items table'.
CREATE TRIGGER `order_updater` AFTER INSERT ON `orders`
FOR EACH ROW UPDATE items
SET inventory = inventory - 1
WHERE id = new.id
END;
When I try to add it in triggers in phpMyAdmin I keep getting this message:
One or more errors have occured while processing your request:
The following query has failed: "CREATE TRIGGER orer_update BEFORE INSERT ON items FOR EACH ROW CREATE TRIGGER order_updater AFTER INSERT ON orders FOR EACH ROW UPDATE items SET inventory = inventory - 1 WHERE id = new.id END;"
MySQL said: #1303 - Can't create a TRIGGER from within another stored routine.
Can this be done with this trigger?
Will the trigger update a particular item or all items?
Can the trigger be added to the products .htm page or does it need to be in phpMyAdmin?
I'm not sure if the the code is quite right as I'm new to this, any pointers would be appreciated.
*UPDATE:
phpMyAdmin : I copied this from page it says: Version information: 4.0.8, latest stable version: 4.0.9*
Try something like:
DELIMITER $$
CREATE TRIGGER `order_updater` AFTER INSERT ON `orders`
FOR EACH ROW
BEGIN
UPDATE `items`
SET `inventory` = `inventory` - 1
WHERE `id` = new.`id`;
END$$
DELIMITER ;
UPDATE
What version of phpMyAdmin are you using? The following example, using version phpMyAdmin 4.1.0-beta2, works without problem.

History field in MySQL database

Trying to create a field in a table that will get the old value of another field. My table is very similar to this:
------------ -------------------- ------------------------
| id int(10) | price decimal(5,2) | price_old decimal(5,2) |
----------------------------------------------------------
What I am trying to get is the value of price field to get copied in price_old field ON UPDATE of the current record.
Is it possible to achieve this only with mysql?
PS: The data contained in that cell is not critical. The usage is to store the previous price of a item and after to be able to show how it changed. I will only need the last value, not the full history of the table entry. (My MySQL server is 5.0, if this matters)
Yes, you can use a BEFORE UPDATE trigger (documentation).
I think this syntax is right but haven't used MySQL in a while so you may have to toy with it. If it's wrong and you get it working please let me know so I can edit it.
DELIMITER $$
CREATE TRIGGER `price_trigger`
BEFORE UPDATE ON `products`
FOR EACH ROW
BEGIN
IF NEW.price != OLD.price
SET price_old = OLD.price;
END IF;
END$$
DELIMITER ;
Use triggers on update rule. There set value of price to price_old
CREATE TRIGGER `update_price_old`
BEFORE UPDATE ON `table_name` FOR EACH ROW
BEGIN
UPDATE `table_name` SET price_old = OLD.price where OLD.id = id;
END

Trigger syntax error

I have two tables with the following column names.
users
ui | email | pass
user_profiles
_email | qty | lvl
I am attempting to make sure that everytime a new user is added to the users table that the user_profiles table creates a new row for the new user. However it fails. After reading through many sample trigger statements I can't seem to find the error.
CREATE TRIGGER updatUserProfilesTBL AFTER INSERT ON users
BEGIN
INSERT INTO user_profiles (_email)
VALUES (new.email)
END;
Looks like you are missing the FOR EACH ROW. Additionally, you will need to alter the DELIMITER if executing this through the MySQL command line and add a ; after the insert statement.
DELIMITER $$
CREATE TRIGGER updatUserProfilesTBL AFTER INSERT ON users
FOR EACH ROW BEGIN
INSERT INTO user_profiles (_email)
VALUES (new.email);
END$$
DELIMITER ;
According to the CREATE TRIGGER spec, it is not optional.