INSERT Batch, and if duplicate key Update in codeigniter - mysql

Is there any way of performing in batch Insert query and if the key already exists, UPDATE that row in codeigniter?
I have gone through the documentation and found only insert_batch and update_batch. But how to update the row with duplicate key in active records? And what happens if one row fails to be inserted or updated in batch_insert? All insertion fails or only that row?

You will have to go with little custom query by adding "ON DUPLICATE" statement
$sql = $this->db->insert_string('YourTable', $data) . ' ON DUPLICATE KEY UPDATE duplicate=duplicate+1';
$this->db->query($sql);
$id = $this->db->insert_id();
Also please check this out, it will give you better solution

For Codeigniter 3, you can use this library. This allows you to be able to supply an array of key-value pairs to be inserted into separate rows and will auto-update data if a duplicate key occurs in DB.
https://github.com/amir-ranglerz/CodeIgniter-Insert-Batch-on-DUPLICATE-KEY-Update

Related

A simple MERGE MySQL query. what is the error in line 1? need assistance [duplicate]

I have to use INSERT and UPDATE in single query. For that SQL having MERGE statement.
Is MERGE statement supported in MySQL. If supported, please provide sample.
MERGE is not supported by MySQL, However, there is other possible way of doing the same:
INSERT...ON DUPLICATE KEY UPDATE
If you specify the ON DUPLICATE KEY UPDATE option in the INSERT
statement and the new row causes a duplicate value in the UNIQUE or
PRIMARY KEY index, MySQL performs an update to the old row based on
the new values.

ON DUPLICATE KEY MYSQL Not Work

I have read for dozens thread but I dunno I could not figure it. I hope this thread won't duplicate.
So, I tried to insert data but in the end if the data has been on the table. It would update the value if it is not, then the data would be inserted into the table. I have read that I have to use
INSERT INTO table() VALUES() ON DUPLICATE KEY data1='$data1', data2='$data2'
Then, here is my code
$sql = "INSERT INTO niche_new(gamename,domain,url,type,date) VALUES('$gamename','$websiteurl','$url','download','$waktu') ON DUPLICATE KEY UPDATE domain='$websiteurl',url='$url'";
$process = $conn->query($sql) or die("Error: ".$conn->error);
In the end of the query.
The data won't be updated, but it is inserted. It makes my table has
lots of duplicate data.
I really appreciate any answers. Thank you :)
Note: It was a great mistake. I forgot to set unique key on my table.
To whom who want to use ON DUPLICATE KEY, make sure you set UNIQUE KEY
by picking which ROW as your parameter. It works now.
If it is inserting duplicate data, most likely, none of the columns you are inserting have the UNIQUEor PRIMARY KEY modifier.
Please specify the KEY column and it should work

Multiple insert in mysql table, some of which may be repeated

I'm developing a php script in which I insert multiple data into a mysql table, but some of this data may already be inserted. I can try to insert each of the individual data and detect the #1062 error (duplicate entry), but it would be very inefficient since it can be more than 100 entries. So, is there any way to do this in one query or must I use a query for each entry to be inserted?
Thanks a lot.
You'll want to use the INSERT ... ON DUPLICATE KEY UPDATE command to accomplish this. In the "ON DUPLICATE KEY UPDATE" section, you can just do some kind of update that changes nothing. For example, "SET fieldName=fieldName", just so nothing is actually changed.
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

Which technique is more efficient for replacing records

I have an app that has to import TONS of data from a remote source. From 500 to 1500 entries per call.
Sometimes some of the data coming in will need to replace data already stored in the dB. If I had to guess, I would say once in 300 or 400 entries would one need to be replaced.
Each incoming entry has a unique ID. So I am trying to figure out if it is more efficient to always issue a delete command based on this ID or to check if there is already an entry THEN delete.
I found this SO post where it talks about the heavy work a dB has to do to delete something. But it is discussing a different issue so I'm not sure if it applies here.
Each incoming entry has a unique ID. So I am trying to figure out if it is more efficient to always issue a delete command based on this ID or to check if there is already an entry THEN delete.
Neither. Use INSERT ... ON DUPLICATE KEY UPDATE ....
Since you are using MySQL and you have a unique key then let MySQL do the work.
You can use
INSERT INTO..... ON DUPLICATE KEY UPDATE......
MySQL will try to insert a new record in the table, is the unique value exists in the table then MySQL will update all the field that you have set after the update
You can read more about the INSERT INTO..... ON DUPLICATE KEY UPDATE...... syntax on
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

Is it possible to declare to mysql queries?

I'm trying to create a code for a single button where it will perform either of two actions where it will add to the database if the user currently don't have the record while it will update the user's record if the user has records already. I've done it like this:
if() {
mysql_query("INSERT INTO table...");
}
else {
mysql_query("UPDATE table SET...");
}
Is it possible?
Yes, what you've written will work. If you have a way to know if there already exists a row or not without making an additional query just for this bit of code, then do exactly as you wrote.
If, however, you planned to first SELECT from the table to see if a row exists, then conditionally INSERT or UPDATE, you will perform more queries than necessary.
It would be better to either:
Have a PRIMARY KEY or other constraint on the table prevent duplicate INSERTs. Then issue an INSERT ... ON DUPLICATE KEY UPDATE query. This will attempt to INSERT the row, and if it is a duplicate, automatically perform the specified UPDATE to that row instead.
Issue the UPDATE query and check mysql_affected_rows to see if it updated an existing row. If not, then issue the INSERT query to create the new row.
Which one is more appropriate depends on your application.
you can use INSERT ... ON DUPLICATE KEY UPDATE Syntax like:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If you have properly set unique keys, you should use REPLACE so you could remove the if.
REPLACE INTO table VALUE (...);
Pay attention that this is a MySQL extension, thus not portable to other DBs.
Yes, you could try the insert then if it fails try the update.
But you could use the MYSQL sql "REPLACE" keyword, which will insert a new record if it doesn't exist or delete the existing record and insert your new one if it does.
You could also use the INSERT ... ON DUPLICATE KEY UPDATE syntax
(explained here - Link to MYSQL ref which seems to be the closest fit to your requirement.
yes it is possible
first write a query for check that record is already exist or not.
Yes it is possible , it will work