Upsert Only inserts and does not update the existing field - mysql

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/

Related

Insert After Delete Causing Integrity Constraint Violation1062 Duplicate Entry

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!

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

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