MySQL merge data on duplicate key - mysql

I'm adopting dashboard now and I created two tables for selecting from frontend;
DATA_SELECTED_HISTORY
DATA_SELECTED_NOW
My frontend page get data from DATA_SELECTED_NOW and my backend algorithm put new data to this database.
I want to put my new data to DATA_SELECTED_NOW,
and the former data to be pushed to DATA_SELECTED_HISTORY when being faced with duplicate key.
I think I could use a swap table solution or insert(select subquery) + insert on duplicate key solution, but I don't get an idea anymore.
How can I use this solution in SQL?

you can use trigger in this case, to check duplication before insert to DATA_SELECTED_NOW and insert in DATA_SELECTED_HISTORY if it duplicates, check the below code
CREATE TRIGGER TRIGGER_Name
BEFORE INSERT ON DATA_SELECTED_NOW
FOR EACH ROW
BEGIN
IF (EXISTS(SELECT 1 FROM User WHERE key = NEW.Key)) THEN
-- you can replace "key = NEW.Key " with your logic to check
-- inset into DATA_SELECTED_HISTORY
END IF;
END$$

Related

What is proper way to set and compare variable inside an sql trigger

Am populating a table using a trigger after an insert event occurs on another table and that worked fine. However i then noticed that the trigger would still insert a new row for existing records. To fix this, I want to create the trigger again but this time it would only fire if a condition is met...but not having previously used triggers in the past am getting a syntax error and not able to identify what am doing wrong. Kindly have a look and help me fix this
CREATE TRIGGER `students_gen_insert`
AFTER INSERT ON `students` FOR EACH ROW
BEGIN
INSERT INTO records (student_id, subject_id)
SELECT new.student_id, subjects.subject_id
FROM subjects
WHERE category = new.class;
END;
Am currently using MySql 5.6.17 version.
It is generally not a good idea to SELECT from the table the trigger is on, and forbidden to UPDATE or INSERT (not that you are doing those). Assuming you are trying to get the values for the row just inserted, the first SET ... SELECT you have is needless; just use NEW.fieldname to get the fields of the inserted row.
The second SET ... SELECT and following condition are a bit confusing. If referential integrity is being maintained, I would think it would be impossible for the records table to refer to that particular student_id of the students table at the point the trigger is executed. Perhaps this was to avoid the duplicate inserts from the trigger's previous code? If so, it might help for you to post that so we can pinpoint the actual source of redundant inserts.

Can you modify a foreign key on insert with a trigger?

I have an issue where a query is inserting into table A, which references table B, but it's using the wrong foreign key for table B. Is it possible to create a trigger in oracle where if the input foreign key is 'ASDF', we modify it to 'FDSA' before the insert so that we can fix this issue?
In either MySQL or Oracle, you can do this using a before insert trigger.
I don't recommend using a trigger for this purpose. Fixing the input data or adding the new value to the reference table seem to me to be more sensible approaches.
Following up on #GordonLinoff's post - I agree with (what appears to be) his point that a trigger is inappropriate here. However, recognizing that sometimes you have to work with what you're given, if you were to use a trigger you'd use a BEFORE INSERT trigger, as in:
CREATE TRIGGER TABLE_A_BI
BEFORE INSERT ON TABLE_A
FOR EACH ROW
BEGIN
IF :NEW.FK_FIELD = 'ASDF' THEN
:NEW.FK_FIELD = 'FDSA';
END IF;
END YOUR_TABLE_BI;
Best of luck.

whenever i update my table i want to store my old data(old-row) in a new table

CREATE TRIGGER items_
ON test
after update
AS
begin
INSERT INTO test2(id,namecan)
SELECT id,namecan from test
end
I Have tried with trigger but i didnt get any results so please help me how to deal with it
methods involving two stored procedures are also welcome
You could do it with a stored procedure, but I would use a trigger.
With a stored procedure, I'd perceive you using a cursor on the otherDatabase table to read through the records and compare each with the values in Table1 to determine whether Table1's data needed to be written to Table2, and if so, to do it.
With a trigger, I would simply update the data in Table1 by whatever means, without concerning myself with what the overwriting data is, and in the trigger,
use the old and new values using the ##Inserted & ##Deleted (system) tables to determine if the old values (##Deleted) needed to be written to Table2.
If you don want to use triggers, you can go for this concept..
Let there is a form from which you are inserting the values on a table say register table. And the action of the form in going to servlet MyServlet. In Myservlet
you can first fetch the data from the Register table and store it into the Rsultset object let rs. And after that insert the rs into a new table.
Please let me know if m not clear to u..
Your trigger syntax is wrong
delimiter //
CREATE TRIGGER items_ after update
ON test
for each row
begin
INSERT INTO test2(id,namecan) values (old.id,old.namecan);
end; //
delimiter ;
So this trigger will make an entry to the test2 table every time you update row in the test table with the old row values from test table.
You should call old.id and old.namecan (i assume that table 'test' have 'id' and 'namecan' field)to get the older data. your trigger body should look like this
begin
INSERT INTO test2(id,namecan) value(old.id, old.namecan)
end
'old' will reference to record which you update.

SQL server 2008 trigger before insert

I am very new to SQL and sorry for asking this is very primary knowledge. My issue as follow.
I have a table called Group and minimumVal and maximumVal are two columns. minimumVal always should be less than maximumVal. I want to check this before inserting a new record to the table. If this condition failed data should not inserted into the DB.
So as per my understanding I though of having a trigger but have no idea how to write this.
This is what I write so far;
CREATE TRIGGER tr_Group
ON Table_Group
for INSERT
AS
????
Please advise me.
I recommend you use check constraints for this:
ALTER TABLE dbo.Table_Group ADD CONSTRAINT CK_Group
CHECK (minimumVal < maximumVal)
If you still want to use triggers, then you need an INSTEAD OF trigger:
CREATE TRIGGER tr_Group ON Table_Group
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Table_Group
SELECT *
FROM INSERTED
WHERE minimumVal < maximumVal
END

MySQL Triggers (Before Insert)

I have two tables inspect, with columns inspect_id (pk), inspect_loc and duplicate_inspect with columns inspect_id, inspect_loc. I need a MySQL trigger that inserts Data into duplicate_inspect only if the data being inserted into inspect is already available.
For example ..
CREATE TRIGGER duplicate
BEFORE INSERT ON duplicate_inspect
FOR EACH ROW
BEGIN
IF new.inspect_id = inspect_id THEN
INSERT INTO duplicate_inspect (inspect_id,inspect_loc) values('','');
END IF;
END
I can't test now, but you need the values. (i think all code it's ok).