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.
Related
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.
I have a composite key created on(courseID,lessonID,studID) on a table.
I am trying to insert duplicate value on composite key columns obviously, we will get duplicate entry error.
What I am trying to achieve is to avoid checking it before inserting, if there is a value on composite key or not.
is there a way to avoid the double-checking step!
CodeIgniter is missing something like insert ignore.
is there a way to omit duplicate insert error from CodeIgniter side since it is composite key!
I have a table that I'm inserting records into. It has a primary key made out of two fields. My syntax up until now has been a simple:
INSERT into table (field,field,field) VALUES ('foo', 'bar', foo') type deal, but I've come across the scenario where I may need to overwrite existing values.
I am familiar, and in the past, have used the INSERT INTO .... ON DUPLICATE KEY UPDATE... syntax, but I recently came across the much more simple REPLACE INTO... syntax.
My assumption of this REPLACE INTO is that IF no data exists for the primary key I'm writing to, it will act as an INSERT. IF the primary does exist however, it will delete the record and insert a new one. Is this correct?
If this is correct, are there any downsides to me just forgoing the INSERT INTO... statement and running a REPLACE INTO... for 100% of the lines users are inserting into the table? Are there any potential risks to using the REPLACE INTO... 100% of the time?
REPLACE does a DELETE first, so if you use foreign key constraints with ON DELETE CASCADE, you could unintentionally delete a lot of dependent data. The re-insert step of REPLACE will not recover that data deleted from dependent tables.
I think I've seen cases where REPLACE causes a new auto-increment value to be generated for the primary key. Maybe if the conflict is based on a secondary UNIQUE KEY instead of the primary key, That could throw off other references to the row even if you don't use foreign key constraints.
Basically we have the same problem as this question: ON DUPLICATE KEY update (with multiple where clauses)
But we can't have unique keys for the keys of reference because we need duplicates of both.
Is there a way to do this with one query?
We have a unique identifier, and also need to record date, and increment a value, but also be able to update/insert without making multiple queries.
Please excuse me if I'm understanding you incorrectly, but it seems to me that what you want can in fact be done with the UNIQUE contraint mentioned in the question you're referencing.
Are you aware that you can create a UNIQUE constraint on more than one column? That is, the combination of the 2 columns is unique, but the columns themselves don't have to be.
In your case, you would use ALTER TABLE table ADD CONSTRAINT uq_table_id_date UNIQUE (id, date).
How would you implement the following?
I would like to insert data to mysql tables. Let's imagine there are two tables where a foreign key relation exists. First, i insert a row that has a primary key that should be inserted as a foreign key to one of the rows to the other table. So when i would like to insert the foreign key and it's related data, i have to know the primary key of the related row in the other table. As i am a beginner, my solution would be the following: I would insert a field value with a particular data to the original table so that the inserted value could be used to retrieve the primary key with a SELECT, and then insert the retrieved primary key as the foreign key to the related rows of the other table.
Although I don't know a better solution, I think this would be a very clumsy way to implement this logic. There must be a better way of doing this.
Your solution won't work because if you are inserting not unique data, you may not retrieve the appropriate primary key. MySQL offers LAST_INSERT_ID() function for this. Just insert row into your primary key table and then use SELECT LAST_INSERT_ID(). It returns last primary key value inserted into your original table (last insert query) and now you can use it as foreign key in related table.