MySQL, Synchronise two tables - mysql

I have two data base A & B, in each one I have a table called answer, I want to use the 2nd one as an archive table, I want to create a trigger that copies the last inserted row in A.answer to B.answer.
Here what I did
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW INSERT INTO `B`.`answer` SELECT * FROM `answer`
This trigger works, but copy all the answers inserted in A.answer to B.answer.
The problem is : I dont want to copy all answers, but only the last one.
(remark : I dont know the id of the inserted answer, so dont tell me to add a ' WHERE answer.id = xx ').
Thanks for your help

You could write your trigger this way:
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW
INSERT INTO `B`.`answer` VALUES (NEW.col1, NEW.col2, ..., NEW.colN)
where you have to specify all column names.
Please see fiddle here.

Related

Trigger after Update based on condition

I have 4 columns in a table and this are.
when a new item is inserted it will look like this
I have a seperate table that look like this (based on the above image) it will appear like this
It looks like more of a logs. Now I have a code for that.
CREATE TRIGGER insert_on_tbl1
AFTER INSERT ON table1
FOR EACH ROW
INSERT INTO table2 (Person1,Item,tag) VALUES (New.Person1,New.Item,'Inserted');
now here is my question lets there is a modification happen something like this
and the responsilble for updating is on the Person1 Column (AAA) how can I insert that in my logs table? actually i have a code here for that and here it is
CREATE TRIGGER update_basedon_tbl1
AFTER UPDATE ON table1
FOR EACH ROW
IF IFNULL(NEW.Person2,"") = "" THEN
INSERT INTO table2 (Person1,Item,tag) VALUES (New.Person1,New.Item,'Modified');
END IF;
The problem here now is if the person who updates comes from Person2 still it saves the logs how can I fix that? I mean I want that all logs will be saved if changes are made from Person1 column only.

How to copy multiple row in table while inserting additional value

How can i copy table and insert new value at the same time
I want to copy a table with multiple rows and insert a same value "Borrowed" for one column Transaction. I already know how to copy but i dont know how to copy and insert new/another value at the same time.
here is what i got:
INSERT INTO TRANSACTION(UserID,TRANSACTION,First_Name,Last_Name,ISBN,Title,DATE)
VALUES (1,"Borrowed",(SELECT First_Name,Last_Name,ISBN,Title),NOW());
Add your fixed values into the SELECT list:
INSERT INTO TRANSACTION(UserID,TRANSACTION,First_Name,Last_Name,ISBN,Title,DATE)
SELECT 1, "Borrowed", First_Name,Last_Name,ISBN,Title,NOW()
FROM <your table goes here>

INSERT INTO statement in a specific column

For example i have table with a different field names(column), lets say 5 columns and all of them are empty. And i wanted to insert data in one specific column. Is it possible? I'm looking for example of this, but unlucky to find one. Most of insert into statements examples required all columns to be filled. If possible, can you give me the correct syntax? I'm sorry if i'm lacking research or it's already been asked, it's ok if you will redirect me to the link.
If you want insert on column3, leaving empty the other:
INSERT INTO table_name (column1,column2,column3,column4,column5)
VALUES ("","","VALUE","","");
The other part of program would UPDATE the other columns:
UPDATE table_name
SET column1=value1,column2=value2,column4=value4,column5=value5
WHERE some_column=some_value;
The documentation on how to construct an INSERT INTO statement is here: INSERT INTO Statement (Microsoft Access SQL).
But basically, you just need to be explicit about which columns you want to insert values for, and omit the other ones, like this:
INSERT INTO table (colname) VALUES ('colvalue')
What happens to the fields you omit? The documentation says:
When you do not specify each field, the default value or Null is inserted for missing columns.

mysql trigger on insert many to many

I have two tables with many to many relationship.
The tables are
table user
idUser
name
table right
idRight
type
table user_has_right
idUser
idRight
When I insert a user with the rights I want him to have,
obviously the middle table (user_has_right) must have some inserts
too. Let's say I want to insert the user with id = 1 and name = 'test'
and right = 'boss' , where boss is already a row in table right with id = 1 .
The middle table must have an insert of (1,1).
I know how to do this programmatically, but can this be done with trigger on insert?
Thank you.
//The problem I'm facing is how to find the idRight for type 'boss'. Maybe a nested select inside the trigger?
//Is this task even possible?
I've come across this old question. If you're still interested in an answer then here it is
CREATE TRIGGER tg_user_insert
AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_has_right (idUser, idRight)
SELECT NEW.idUser, idRight
FROM `right`
WHERE type = 'boss';
Here is SQLFiddle demo

T-SQL how to modify the value before insert

I find that there are only after and instead of triggers in sql server. And it is illegal to modify the values in the inserted pesudo table. Then my problem occurs: If I want to check the data which is going to be inserted into my table, and when the data violates my constraints I should modify these values to default values, how to do it ? How about updateing the values after inserted ? However, if there's no primary key or colum which is unique in my table, how can I locate the row just inserted and then update it ?
Basically, with an INSTEAD OF INSERT trigger, you can achieve what you're looking for - just read out the data from the INSERTED pseudo table, modify it, and insert it into the table
So your trigger would look something like this:
CREATE TRIGGER YourTrigger ON dbo.YourTable
INSTEAD OF INSERT
AS
SET NOCOUNT ON
-- do the INSERT based on the INSERTED pseudo table, modify data as needed
INSERT INTO dbo.YourTable(Col1, Col2, ....., ColN)
SELECT
Col1, 2 * Col2, ....., N * ColN
FROM
INSERTED
Of course, you could also add e.g. checks in the form of WHERE clause to that SELECT .... FROM INSERTED statement to e.g. ignore certain rows - the possibilities are endless!