Insert After Delete Causing Integrity Constraint Violation1062 Duplicate Entry - mysql

I have an AWS RDS MySQL table which has a unique key constraint. Let us take the table name as user and field name as username for example. Suppose the table has a row username=admin. I am executing the below queries using Laravel.
delete from user where username='admin'
insert into user (username) values ('admin')
Once in a while, I can see Integrity Constraint Violation1062 Duplicate Entry in the logs. It seems like the row is not deleted by the time the code executes the insert query. It works most of the time. I can update the code with other logic but wondering why it is happening! Is there any AWS RDS specific scenarios related to this use case? I have not experienced this using own MySQL installation. Thanks for your help!

Related

Upsert Only inserts and does not update the existing field

I am trying to add attendances to the attendance table and if attendance of the give date and student id is already in the table I need to update the attendance type.
But the upsert is only inserting and it does not ever update.
SmStudentAttendance::upsert($studentRows, ['student_id', 'attendance_date'], ['attendance_type']);
It's too late but it worked for me. Try this in a migration:
$table->unique(['student_id', 'attendance_date']);
Or you can add unique constraints to the columns in phpmyadmin (or a similar tool)
upsert works exactly like
on duplicate key update on MySQL
which needs a unique constraint violation to update instead of an insert.
So check if you have such a violation, directly in the database.
Also you can read more about with an example for example at https://www.amitmerchant.com/insert-or-update-multiple-records-using-upsert-in-laravel8/

How do I avoid foreign key constraint fail error -

I am currently using dreamweaver and I am trying to insert a record insertion wizard connected to a mysql server. However when I try to update the column I get a foreign key constraint fail and I can understand this is due to me creating a non existent foreign key.But how else can I use an input wizard.
database
Here is the data model.
The column I am trying to update is "kommentar" but it is not working, any ideas?
Tough to say with the info provided, but in all likelihood the value you're using in hotellid doesn't have a corresponding entry in the table hotell.
You could run the same wizard for the hotels and add a hotel with your desired hotelid, before inserting into kommentar

How do I get MySQL tables to update each other?

I'm new to programming so please forgive my ignorance. I'm trying to get MySQL tables to update each other. For example: When I insert data into a Primary Key column in one table, it does not appear in it's Foreign Key column in another table.
Does anyone have any advice on this issue?
Thank you!
Additional Information: I used phpMyAdmin to create my tables and then added SQL code to create the Foreign Keys. Example of code is below.
ALTER TABLE CourseSchedule
ADD FOREIGN KEY (CourseId)
REFERENCES Course(CourseId)
I believe what we have here is a slight misunderstanding of the documentation.
For storage engines supporting foreign keys, MySQL rejects any INSERT
or UPDATE operation that attempts to create a foreign key value in a
child table if there is no a matching candidate key value in the
parent table.
When an UPDATE or DELETE operation affects a key value in the parent
table that has matching rows in the child table, the result depends on
the referential action specified using ON UPDATE and ON DELETE
subclauses of the FOREIGN KEY clause. MySQL supports five options
regarding the action to be taken, listed here
What your foreign key actually does is to make sure that you cannot insert values into your CourseSchedule table that do not correspond to an entry in the Courses table.
To give you an example, suppose you were to try to enter schedule a python course for every wednesday at 9:00 but you dont' actually have an entry for python in your Courses table. Then mysql will refuse to create that entry. Mysql cannot do the reverse. It doesn't know details about your python course. So it cannot automatically create a entry in the Courses table for you. Similarly, if oyu created an entr in the courses table. Mysql cannot automatically create a CourseSchedule for you because it doesn't know at what time it should be scheduled.

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

How to restrict a row from getting deleted from a table through a query?

Is that possible?
I know how to do it with a program but is there any query to do it without the support of primary or foreign key?
In MySQL I don't think it is possible. If the MySQL user running query has the DELETE privilege, then they will be able to delete any row.