I have an Insert Statement like:
f"INSERT INTO `system_measurements`(`Global_irradiance_tilted_in_Wh_per_m2`, `a_id`, `subDate`) VALUES ('{temp}', '{temp_id}', '{i.date()}')"
And want it to ignore existing entries without checking the date everytime. So i thouhgt I could use
ON DUPLICATE KEY UPDATE a_id=a_id
But it still adds all values to the table.
I interpret your question as saying that a new row is inserted despite the on duplicate key.
In order for on duplicate key to work, you need a unique constraint or index. The update takes place when the query violates the unique constraint.
I am guessing that you want this on a_id, so be use you have something like:
alter table system_measurements add constraint unq_ system_measurements_a_id
unique (a_id);
INSERT IGNORE will do nothing other than discovering that it is a duplicate. "Duplicate" is checked via the PRIMARY KEY and any UNIQUE keys.
Simply stick IGNORE after INSERT in the SQL you have.
Related
Hello I am using the "INSERT ON DUPLICATE KEY UPDATE" sql statement to update my database.
All was working fine since I always inserted an unique id like this:
INSERT INTO devices(uniqueId,name)
VALUES (4,'Printer')
ON DUPLICATE KEY UPDATE name = 'Central Printer';
But for now, I need to insert elements but I don't insert a unique id, I only insert or update the values like this:
INSERT INTO table (a,b,c,d,e,f,g)
VALUES (2,3,4,5,6,7,8)
ON DUPLICATE KEY
UPDATE a=a, b=b, c=c, d=d, e=e, f=f, g=g;
Have to say that an autoincrement primary key is generated always that I insert a row.
My problem is that now the inserted rows are duplicated since I don't insert the primary key or unique id explicitly within the sql statement.
What I am supposed to do?
For example, maybe I need to insert the primary key explicitly? I would like to work with this primary autoincremented key.
For recommendation from Gordon I am adding a sample case the you can see in the next image
Rows Output
In this case I add the first three rows, and then I try to update the three first rows again with different information.... Ok I am seeing the error... There is no key to compare to...... :$
Thanks for your answers,
If you want to prevent columns from being duplicated, then create a unique index or constraint on them. For instance:
create unique index unq_table_7 on table(a, b, c, d, e, f, g);
This will guarantee that the 7 columns -- in combination -- are unique.
I am learning MySQL and used the ON DUPLICATE KEY UPDATE, what this does, if I am not mistaken, when it found a duplicate; it will update the row. So, to my question, is there something like ON DUPLICATE KEY INSERT? like when it found a duplicate, it will still insert the data into the table?
No, there is no way to insert a row that has duplicate values in columns that are constrained against duplicate values. If you could, the result would be that the database would be in a state that violates its own constraints.
You would have to drop any unique key or primary key constraints on the table to allow duplicates in the respective columns.
My problem is that I have multiple unique keys on a table.
Insert ignore is not an option because it suppresses the errors.
MySQL has no support for any type of conditionals outside a statement (ex. if (cond) then insert else don't insert)
Stored procedures are not an option (the only place I can use the if/else statements)
On duplicate key might update a key with a new value, but I want the unique keys not to change in case one fails the unique constraint.
So the only option would be on duplicate just don't update anything. Is there any way I can achieve this? Or are there any other options?
If you want ON DUPLICATE KEY UPDATE to not actually do anything, just set a column value to the existing value. Other conflicts such as foreign key constraints will bubble up, unlike using the IGNORE keyword, but no values will change on conflict.
INSERT INTO table (value1, value2) VALUES ('1', '2')
ON DUPLICATE KEY UPDATE value1 = value1;
If you want to ensure that no valid data changes in the event of a conflict, you can add a column with arbitrary data in it to the table, and use that for the UPDATE statement.
A third option if you wish to keep all logic in your application and not in the database is to run a SELECT statement first to inspect potential conflicts before running your INSERT/UDPATE statement.
Although ruled out for your scenario, a stored procedure would also be able to provide this logic in a single database call.
Found another option in case someone stumbles across this issue.
If your table has an autoincremented primary key , you can update the pk like this :
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
I have my index set to the field: testsuite_id and I'm executing the following query:
INSERT INTO testsuite_dates (testsuite_id,start_date,end_date)
VALUES ('27798','2012:02:27 00:00:00','2012:02:28 00:00:00')
ON DUPLICATE KEY UPDATE testsuite_id='27798';
I would expect this to update the entry if an entry for testsuite_id exists or insert it if it doesn't but if I execute this query a second time it inserts a second entry with the same testsuite_id.
What am I missing?
Then you don't have a PRIMARY or UNIQUE key on testsuite_id.
The "on duplicate key update" only changes the fields YOU specify in the 'update' portion. You're updating only the key field, which triggered the constraint violation in the first place. The other fields are silently dropped, so you're effective doing update yourtable set id=id where id=xxx.
You have to list each field you want to be updated:
INSERT INTO testsuite_dates (testsuite_id,start_date,end_date)
VALUES ('27798','2012:02:27 00:00:00','2012:02:28 00:00:00')
ON DUPLICATE KEY UPDATE
start_date=values(start_date), end_date=values(end_date)
Note the use of the values() function in the update portion. This allows you to refer to the NEW value that would've been inserted, and use it for the update portion. This saves you having to embed the same data twice in the same query. Not a big deal for a few dates or numbers, but if you're inserting large blobs, it makes for a HUGE query string size savings.
did you set your testsuite_id to be a primary key, or have unique constraint
INSERT INTO testsuite_dates (testsuite_id,start_date,end_date)
VALUES ('27798','2012:02:27 00:00:00','2012:02:28 00:00:00')
ON DUPLICATE KEY UPDATE testsuite_id=testsuite_id+1;
What are peoples' thoughts on the most performance efficient way to do the following query:
3 column table
if the combination of col_1 and col_2 values already exist UPDATE col_3
else INSERT new row
I assume i need some kind if UPDATE ON DUPLICATE KEY (which i've never used before), however I do not have a 'KEY' but instead a pair of two values (columns) to make a key...
You can create a PRIMARY or UNIQUE key out of multiple columns (called a composite key) in MySQL, which'll allow ON DUPLICATE KEY to work just fine.
// create a composite index
CREATE INDEX my_composite_index ON my_table (column1, column2);
// insert or update
INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2') ON DUPLICATE KEY UPDATE column3=column3+1;
Most efficient way is to create UNIQUE KEY and use ON DUPLICATE KEY UPDATE.
Slower way is to:
LOCK TABLE
SELECT TABLE (you need an index anyway for the best performance)
if exists, UPDATE
else INSERT
UNLOCK TABLES
Edit: Ignore my suggestions
You can use a composite key as ceejayoz said, however I think you need REPLACE INTO instead of UPDATE ON DUPLICATE KEY because REPLACE also inserts if no duplicate is found.
Note: I don't know the workings of UPDATE ON DUPLICATE KEY but it sounds like it doesn't perform inserts.