This question already has an answer here:
INSERT deleted values into a table before DELETE with a DELETE TRIGGER
(1 answer)
Closed 6 years ago.
How can I achieve to Delete a row before Insert a new one in the same Table. I tried it with a Trigger but I read that it is not possible because it could cause a deadlock.
I also wanted to save the row which should be deleted to another table (example Table B) before delete it and then Insert a new one (into Table A).
Is there any other ways to do it ?
PS: They will have the same key
You could use UPDATE...
UPDATE tbl
SET col1 = newCol1,
col2 = newCol2
WHERE etc = etc
And If you want to insert updated row to another table you could use TRIGGER AFTER UPDATE for that.
CREATE TRIGGER TriggerName ON Tbl
AFTER UPDATE
AS
INSERT INTO Log (Col1, Col2)
SELECT Col1, Col2
FROM deleted
Related
I'm new to SQL, but trying to build a simple trigger that will execute on each insertion. I want it to update some table values if the key already exists, or insert it with some new values if it doesn't exist.
Below is what I have attempted, but I would ideally want tester to be the thing that is being inserted. Something like NEW.reference_key?
CREATE TRIGGER key_access_monitor
BEFORE INSERT ON individual_key_log
FOR EACH ROW
BEGIN
IF EXISTS (SELECT reference_key FROM individual_key_log WHERE key = 'tester')
SELECT 'Found it!'
ELSE
SELECT 'Cannot find it!'
END IF
END
You want to insert a new row if a row with the same key does not already exist, and update the existing row if it does exist, is that correct?
You'd probably find it easier to use INSERT ... ON DUPLICATE KEY UPDATE, as follows:
INSERT INTO individual_key_log
(key, col1, col2, col3, ...)
VALUES
('tester', 'val1', 'val2', 'val3', ...)
ON DUPLICATE KEY UPDATE
SET col1 = 'val1', col2 = 'val2', col3 = 'val3', ...;
One of the problems with your trigger approach is that it is bad practice to attempt to query a table from within a row trigger on that same table, as it violates the ACID principle; many DBMSes do not permit it at all, raising a "Mutating Table" error. Updating a row from within an insert row trigger has the same problem magnified. A row trigger should never try to access its target table directly, instead accessing only the triggering row indirectly through OLD and NEW.
Hope that helps.
This question already has answers here:
In MySQL, can I copy one row to insert into the same table?
(26 answers)
Closed 7 years ago.
A great way to duplicate a row in MySQL is to use INSERT INTO ... SELECT FROM syntax.
For example:
INSERT INTO tblExample (col1, col2, col3)
SELECT col1, col2, col3 FROM tblExample WHERE pkey = 1234;
This is easy and straightforward, but from a code maintenance standpoint this is one more statement to keep track of if there are any schema changes. Suppose I add an additional column to the tblExample table, col4; now I have to remember to go back and update this SQL statement in my code. If I fail to do so, then I've just introduced a bug.
With this in mind, is there an easy to way to copy the whole row, whatever the schema may be, except for the primary key?
Very much against best practices you can do the following:
INSERT INTO myTable
SELECT * FROM myTable WHERE thisField = "abcd"
A somewhat inelegant way:
CREATE TEMPORARY TABLE tmpTable ENGINE=MEMORY SELECT * FROM realTable WHERE pk = 'something';
UPDATE tmpTable SET pk = 'something else' ;
INSERT INTO realTable SELECT * FROM tmpTable;
This question already has answers here:
mysql insert if value not exist in another table
(5 answers)
Closed 8 years ago.
INSERT INTO entity (name,surname,adr_id) values('test','test',< pseudocode see bellow >)
I want to check in table addresses by street,town and return that ID as adr_id or insert a new row in addresses.
So initially INSERT to have existing adr_id or a new, just created one.
This is possible?
You can use Insert ... On Duplicate... Update...
Example :
INSERT INTO table (a,b,c) VALUES (4,5,6)
ON DUPLICATE KEY UPDATE c=9;
In a PHP script I'm using the MySQL INSERT ... ON DUPLICATE KEY UPDATE command to insert/update a number of records in my DB.
Is there anything that MySQL returns to say which records were inserted or updated?
An 'ugly' way would be to do a SELECT before each INSERT and see if the key exists before each INSERT but I'd like to know if MySQL has this function built in.
In case anyone need further info on what I'm trying to do is, to save the record id's to a log.
You will need another SELECT regardless, specifically SELECT ROW_COUNT().
From http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count
For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value is 1 if the row is inserted as a new row and 2 if an existing row is updated.
No, there is no way to tell which records were inserted and which were updated purely with the means provided by MySQL without additional queries. However, you can add a column to the table where you can keep an indicator that you can use to mark the record as updated instead of inserted, e.g.
INSERT INTO YourTable (Col1, Col2, Updated)
VALUES ("value1", "value2", 0)
ON DUPLICATE KEY UPDATE
Col1 = values(Col1),
Col2 = values(Col2),
Updated = 1
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to use if not exists while inserting a row mysql
I have problem:
I have table named "table" with 4 columns: id(int, PK, AI, Unique), col1(varchar), col2(varchar), col3(datetime)
Many users can connect to mysql server and insert rows into "table". The problem is that col2 and col3 can't exists in other row in "table".
I would write something like that:
IF NOT EXISTS (select * from table where col2='2' and col3='2012-12-12 12:12:12') INSERT INTO table(col1, col2, col3) values(1,2,'2012-12-12 12:12:12')
I assume you know what I'm trying to do.
I'm trying to do this in one statement (to avoid situation in which 2 users inserts the same row at the same time) or in transaction. If I should do it in transaction, then what type of isolation should it be? Serializable isolation causes lock tables so it can slow down inserting process much more.
Help me, please.
If you don't want duplicates for the pair of col2 and col3 (i.e. col 2 can have duplicates but any pair of col2 and col3 only appears once) you can specify this:
UNIQUE(col2,col3)
Any query which now attempts to insert a col2,col3 pair will fail. You can deal with this either using:
INSERT IGNORE INTO table(col1, col2, col3) values(1,2,'2012-12-12 12:12:12');
or if the insert should change the values you can use:
INSERT INTO table(col1, col2, col3) values(1,2,'2012-12-12 12:12:12')
ON DUPLICATE KEY UPDATE someCol=someVal