Trigger to delete previous data on INSERT - mysql

My table has the following columns: type, how, what, index
I need a trigger so when I insert a new record containing:
--TYPE--HOW--WHAT--INDEX--
INSERT INTO table VALUES(10, 'OP', 'IP', -1);
If there is already another record with WHAT = 'IP' then trigger should delete the previous record and continue inserting. Is this possible to do in MySQL?
I have tried to design my trigger using the Navicat but everytime I have ended with an exception showing up.

Related

Mysql - Create trigger to insert a new row based on a row inserted in another table

Hi I have two tables called users and userrewards. When a new row gets inserted into the users table, I want to take the userid and create a new row in the userrewards table, only if the usertype is "premium". In the userrewards table I just need to insert the userid - the rest of the fields are set to default values until the user makes changes themselves. How can I do this via a trigger?
You need to create an "after instert" trigger on user: that's the table you're watching. Now, if I guessed your column names correctly, the sql for creating the trigger should look like this:
CREATE TRIGGER user_ai AFTER INSERT ON user
FOR EACH ROW
BEGIN
IF new.usertype = 'premium' THEN
INSERT INTO userrewards (user_id) VALUES new.id;
END IF;
END;
Another example of a trigger with if statements and :old / :new values can be found here. MySQL documentatation for create trigger here. MySQL documentation for the if-syntax here.

How can I trigger this SQL query the right way?

This is my problem:
Create a trigger which will add the id and age columns of rows deleted
from the table "metadata" to the "metadata2" table.
The query that will be run to check if the trigger works is:
DELETE FROM metadata WHERE ID=40124197; SELECT * FROM metadata2.
So, what did I do? I did this:
CREATE TRIGGER somerandomtrigger
AFTER INSERT ON metadata2
FOR EACH ROW
BEGIN
INSERT INTO metadata2 (id, age)
END
Yet it keeps saying: Query failed: near "END": syntax error, but I don't get why.
It seems you used the wrong event trigger on the worng table.
When you delete a record from metadata table, you want to insert some data from this record into metadata2.
On delete trigger, there is a a records managed by MySQL named OLD containing the record that has been deleted.
CREATE TRIGGER somerandomtrigger
AFTER DELETE ON metadata
FOR EACH ROW
BEGIN
INSERT INTO metadata2(id, age) VALUES (OLD.id, OLD.age);
END;
Syntax and examples of MySQL trigger

how to use auto-incremented column of one table in another table-mysql workbench 6.2

I tried it with the help of this query "insert into class(faculty_id) select faculty_id from faculty;" but it is inserting already existing values only. Here one column of the table is under auto-increment so I wanted to take this incremented values to another table's column in mysql workbench 6.2. I even tried setting a foreign key between the two columns but it dint work. Please can anyone help out with this issue??
This is what you want i think. an after insert trigger will fire every time something is inserted into faculty, and after the data has been inserted. You can access the values that have just been inserted with the NEW. prefix.
DELIMITER //
create trigger classInsert after insert on faculty
for each row
begin
insert into class(faculty_id) values (NEW.faculty_id);
end//
DELIMITER ;
And it will automatically add an entry into the class table every time you add a new value to faculty.
Fiddle example

Mysql trigger replace row

I want to see if can I use mysql to manage a little stock.
table A contain all the movements:
art_code, qty_load, qty_unload, date
table B with the existence:
art_code, total_load, total_unload, available, date
I've created a trigger: (after update on)
INSERT INTO STOCK VALUES(NEW.ART_CODE, TOTAL_LOAD, TOTAL_UNLOAD, TOTAL_LOAD-TOTAL_UNLOAD, NOW());
but after the first correct run it says a row already exists, how could I replace the old row with the new row?
You can use INSERT ... ON DUPLICATE KEY UPDATE command. It will help you to insert and to update existed records using one statement.
INSERT ... ON DUPLICATE KEY UPDATE Syntax

Insert unqiue record into one table and the results into a second table

Using MySQL and PHP, I have two tables ResponseTable and EntryTable
Response table has
RespID(pk,uq,ai, int), Response(string)
Entry table has
EntryID(pk,uq,ai,int), RespID(int), UserID(int)
I would like to insert a response into the ResponseTable where the Response doesn't exist, and then insert an entry into the EntryTable based on the RespID from the ResponseTable corresponding to the response.
How can this be done with the fewest statements?
EDIT:
Response is unique
The fewest statements from the front-end would be to use a stored procedure. Any way you do it, though, you will still need to have two INSERT statements. You just can't insert two different things with one query.
mysql supports TRIGGERS . you can add one to your ResponseTable that will activate "AFTER" "INSERT" and you can get it to use the values that are being inserted using the NEW key word.
the body of the trigger can be an insert into entry
something like:
CREATE TRIGGER `response_after_insert` AFTER INSERT ON `response`
FOR EACH ROW
BEGIN INSERT INTO entry SET RespID=NEW.RespID ON DUPLICATE KEY UPDATE operation=1;END;
So once you have your trigger set up, any time you do an insert on respnse, the trigger will get activated