Discuss the trigger for database table changes - mysql

hy,
This trigger will give me whole table record with all changes butt i want to get the last changed row share the code.
Create Or Replace Trigger TRG_check IN
Before insert or update
on temp_checkin
FOR each ROW
BEGIN
INSERT INTO temp_checkin_history
select * from temp_checkin
where temp_checkin.id = temp_checkin.id;
END;
thanks

Writing a query like INSERT INTO SELECT * without specifying the columns is a bad practice. Also, there is no need to select from the trigger owner table like you are doing. Instead, use :NEW keyword and specify all columns. You may write your trigger as
CREATE OR REPLACE TRIGGER trg_check BEFORE
INSERT OR UPDATE ON temp_checkin
FOR EACH ROW
BEGIN
INSERT INTO temp_checkin_history (
id,
col1,
col2,
col3
) --other columns
VALUES (
:new.id,
:new.col1,
:new.col2,
:new.col3
); -- other columns prefixed by :NEW
END;

Related

MySQL-Update inserted auto increment primary key to custom value

When I insert a new row, an auto increment ID will be saved like 1,2,3..
I need to custom it using a Trigger so it will be saved like 20171,20172,20173..
This trigger would do this.
CREATE DEFINER=`root`#`%` TRIGGER `test_before_insert` BEFORE INSERT ON `test` FOR EACH ROW BEGIN
SET NEW.id = (
SELECT CONCAT(YEAR(CURDATE()),IFNULL(MAX(CAST(ids.id AS UNSIGNED))+1,1))
FROM (
SELECT RIGHT(t.id,LENGTH(t.id)-4) AS id
FROM test t
WHERE LEFT(t.id,4) = YEAR(CURDATE())
) ids
);
END
But there are many reason you would not want to. This will get exponentially more expensive as rows are inserted, and provides no viable sorting etc.

How to copy-paste data from one table to another in MySQL?

I have crate a db in MySQL which has a lot of tables. I want the value of one table to be automatically saved on another table too.
For example I write something on: table1.lastname, I want this to be also stored in table2.lastname .
How is this called and how I can do that with PHP My Admin?
CREATE TABLE new_table_name LIKE old_table_name
Create trigger after_insert on new_table
like this
CREATE TRIGGER `AFTER_INSERT` AFTER INSERT ON `new_table_name` FOR EACH ROW BEGIN
insert into new_table_name (column_names) values (column_values) ;
END
For first you must create table for data store.
Then you must create trigger on wanted table for catch event and insert data in early created table.
This will do what you want:
INSERT INTO table2 (lastname)
SELECT lastname
FROM table1
If you want to include all rows from table1. Otherwise you can add a WHERE statement to the end if you want to add only a subset of table1.
I hope this helps.
If the table doesn't exist, you can create one with the same schema like so:
CREATE TABLE table2 LIKE table1;
Then, to copy the data over:
INSERT INTO table2 SELECT * FROM table1
Or If the tables have different structures you can also:
INSERT INTO table2 (`col1`,`col2`) SELECT `col1`,`col2` FROM table1;
EDIT: to constrain this..
INSERT INTO table2 (`col1_`,`col2_`) SELECT `col1`,`col2` FROM
table1 WHERE `foo`=1

Mysql Insert if not exist in two column

I looked into MySQL duplicate key but cant figure it out.
I have a table like below:
id series chapter path(can be unique)
I want only insert data and not update. Lets say I have data like below:
seri:Naruto, klasor:567 ==> If both of these exist in table then do not insert.
seri:Naruto, klasor:568 ==> If Naruto exist but 568 does not exist then do insert.
How can I achieve this?
Easiest way would be to define unique index with two columns on that table:
ALTER TABLE yourtable ADD UNIQUE INDEX (seri,klasor);
You may also define two column primary key, which would work just as well.
Then use INSERT IGNORE to only add rows when they will not be duplicates:
INSERT IGNORE INTO yourtable (seri, klasor) VALUES ('Naruto',567);
INSERT IGNORE INTO yourtable (seri, klasor) VALUES ('Naruto',568);
Edit: As per comments, you can't use UNIQUE INDEX which complicates things.
SET #seri='Naruto';
SET #klasor=567;
INSERT INTO yourtable
SELECT seri,klasor FROM (SELECT #seri AS seri, #klasor AS klasor)
WHERE NOT EXISTS (SELECT seri, klasor FROM yourtable WHERE seri=#seri AND klasor=#klasor);
You may use the above query with two local variables or convert it to single statement by replacing the local variables with actual values.
Better way would be to use stored procedure:
CREATE PROCEDURE yourinsert (vseri VARCHAR(8), vklasor INT)
BEGIN
DECLARE i INT;
SELECT COUNT(*) INTO i FROM yourtable WHERE seri=vseri AND klasor=vklasor;
IF i=0 THEN
INSERT INTO yourtable (seri,klasor) VALUES (vseri, vklasor);
END IF;
END;
This would allow you to perform the INSERT using:
CALL yourinsert('Naruto',567);
INSERT INTO table_name (seri, klasor) VALUES ('Naruto',567)
WHERE NOT EXISTS( SELECT seri,klasor FROM table_name WEHERE seri='Naruto' AND klasor=567
)
Hope this helps..

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!

sql trigger inserting row into two tables

I was looking for a way to create a trigger that would insert the same row into two tables with the same values.
For example, a new row is inserted into pushNotificationQueue as soon as that is inserted, I would like that same exact row to be inserted into messages.
I tried this
CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
insert into mbb_messages select * from mbb_pushNotificationQueue
the only problem with that is that it goes through and adds entries that have already been previously added.
You have to say with which rdbms you are working.
Anyway, you have to use a special table normally named inserted or similar.
This is for Sql Server:
INSERT INTO mbb_messages SELECT * FROM INSERTED
Others like Sybase use a REFERENCES clause to get to the newly inserted record:
create trigger TriggerName after insert on
TableName
referencing new as new_name
And for MySQL (which you are seem to use) you can refer to the newly inserted records by using the NEW table:
CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
insert into mbb_messages select * from NEW;
END;
You need to use the column name with new keyword. Please find the trigger below:
DELIMITER $$
CREATE TRIGGER add_to_message
after insert on mbb_pushNotificationQueue
FOR EACH ROW BEGIN
insert into mbb_oushNotificationQueue(`col1`, `col2`) values(new.col1, new.col2);
END$$
DELIMITER ;
First of all, I say that using select * with an insert-select statement is really, really a bad idea. The reason is that you can never predict the order of the columns that are returned from a selection.
Secondly, assuming SQL Server, I would suggest using the following:
create trigger add_to_message
instead of insert on mbb_pushNotificationQueue
for each row
as
begin transaction
insert into mbb_oushNotificationQueue (col1, col2, col3)
select col1, col2, col3
from inserted
insert into mbb_messages (col1, col2, col3)
select col1, col2, col3
from inserted
if ##ERROR_LEVEL = 0
commit
else
rollback
Disclaimer:
This code has not been tested and may require some minor fixes, but is illustrating the idea very well.
I ended up using
CREATE TRIGGER add_to_messages
after insert on mbb_pushNotificationQueue
FOR EACH ROW
INSERT INTO mbb_messages SET messageID = NEW.messageID,
toUserID = NEW.toUserID,
fromUserID = NEW.fromUserID, message = NEW.message, dateReceived = NEW.dateReceived
Thanks to everyone who posted.
So only add the last record added.
insert into mbb_messages select blah from mbb_pushNotificationQueue where blah meets some criteria....
Having max(id) or something.