Composite key insertion in codeginiter - mysql

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!

Related

Getting a conflict when adding a UNIQUE KEY on columns that are a superset of another UNIQUE KEY?

I'm getting a duplicate entry error when trying to create a new UNIQUE KEY in a situation that should be impossible. I currently have a UNIQUE KEY mykey (column_a, column_b) and I'm now trying to run ALTER TABLE mytable ADD UNIQUE KEY mykey2 (column_a, column_b, column_c) and getting a duplicate entry error. How could this possibly be?
It's a very active table, so my first instinct was a race condition, but the original key should still prevent any duplicates.
Discovered this issue which seems to explain what I was seeing, locking the table resolves the problem :)

Ignore values on insert with ON DUPLICATE UPDATE

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.

Any chance of failure in REPLACE over INSERT?

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.

is there `on duplicate key insert` in MYSQL?

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.

Upsert without using on duplicate key MYSQL

I'm trying to write a mysql statement that upserts into a table without having to use the primary key.
I know of the on duplicate key command but I can't use it here since I'm checking for the uniqueness of two columns that aren't primary keys. I know it would be better to just make these two primary keys, but I can't since this was the schema that was given.
The schema looks like this:
tbl_order_detail
key_order_detail
key_order
key_product
some_other_keys
If the key_order,key_product pair is unique then I do a regular insert.
If they aren't unique then I update the row.
Any suggestions?
ON DUPLICATE KEY UPDATE works not only with primary keys, but with any unique constraints.
So just create a composite unique index for (key_order, key_product) columns and use it.