Duplicate key update MySQL not updating - mysql

I have two Unique keys in one table. I'm inserting the data from an csv file.
Unique keys are: enrollmentNo and subjectCode
My query is:
Insert into result_stud_det(enrollmentNo,departmentCode,subjectCode,semester,marks,enrSubjCode) values (?,?,?,?,?,?) "
+ "ON DUPLICATE KEY UPDATE previousMarks=marks, marks=?;
The problem appeared when the data is updating. The "marks" updating in the end is remaining same. The first data on the csv file is copied down to every other column with unique enrollmentNo and to any subjectCodes.
It is because it is looking for the unique key "enrollmentNo" only.
What do I need to do so that the latest marks, "marks", doesn't have same value after updating it?

I suspect your problem is related to this question: MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields
From the accepted answer:
UPDATE in ON DUPLICATE KEY UPDATE is performed if one of the UNIQUE field equals the value to be inserted
It sounds like you are expecting your statement to behave as if you have a single, composite unique key, instead of two separate unique keys.

Related

UPDATE/INSERT based on non-unique keys?

I have a table that contains
id | user | date | data1 | data2 ......
where id is the primary unique key.
I'm trying to write a query that can UPDATE if both user and date exist while INSERT if either one of them doesn't exist
I thought about the INSERT INTO...ON DUPLICATE KEY...UPDATE method, but that requires using the unique key, which I do have but not using.
What would be a good way to deal with this issue?
Per discussion in comments, you should make (user, date) a unique key.
This will trigger the INSERT INTO ... ON DUPLICATE KEY UPDATE query as expected, updating rows with matching user and date fields, and inserting new ones where no match is found.
The only valid option is to implement this UPSERT in the programming language that you use with mysql, because MySQL needs a unique key for both INSERT ... INTO and REPLACE.
Or to add a unique index on the user and date columns which seems to be in concordance with your business logic anyway.

How to reset all ID AUTO Increment to sequencial value on each INSERT Or DELETE Query Execution..!

I have a table with name as listings and inside there I have a COLUMN namely as when some rows are deleted so the AUTO Incrmementation columns namely as "ID" goes into soemthing very bad values..Like missing values in between which I don't like and don't suit like a professional way..so therefore I want please if you people can guide me to how reset all ID columns values in rows on each INSERT or DELETE Query Exeution please..!
If you really want to find the lowest unused key value, don't use AUTO_INCREMENT at all, and manage your keys manually. However, this is NOT a recommended practice.
AS explained at Auto Increment after delete in MySQL
Primary autoincrement keys in database are used to uniquely identify a
given row and shouldn't be given any business meaning. So leave the
primary key as is and add another column called for example
courseOrder. Then when you delete a record from the database you may
want to send an additional UPDATE statement in order to decrement the
courseOrder column of all rows that have courseOrder greater than the
one you are currently deleting.
As a side note you should never modify the value of a primary key in a
relational database because there could be other tables that reference
it as a foreign key and modifying it might violate referential
constraints.
Well it is not recommended but you insisted , so use this to re order
By using something like:
ALTER TABLE table_name AUTO_INCREMENT = 1;

MySQL on duplicate key update without updating the recorded row

I read about the MySQL command ON DUPLICATE KEY UPDATE. I have a column Surnames in a Users table. Since there must be no identical surnames, I want to INSERT a new surname when the surname isn't in the database, and leave the row as it was recorded if the surname was previously saved in the database, without updating it. How can I achieve this?
INSERT IGNORE ... will try to insert a new row, if a duplicate key is found the new data will be discarded.
Documentation of INSERT

prevent duplicate entries by matching a subset of fields

I have an insert statement that inserts tags into a table. Each tag has a unique id (for referencing), a vid_id (for matching to a video), and a name (ex. tag1).
A duplicate entry would be considered having an existing entry with vid_id and name as the same as whatever is being inserted.
How do I stop the insert for a duplicate entry that matches 2/3 fields?
$sql="INSERT into tags (id,vid_id,name) VALUES (?,?,?)";
$stmt16 = $conn->prepare($sql);
$result=$stmt16->execute(array($id,$vid_id,$tag));
You just have to create a unique contraint composed by vid_id and name and you are all set!
This is how you do it:
alter table tags add unique (vid_id, name);
Once created, your unique constraint will forbid the insertion of values that would produce duplicate rows given your unique definition (the fields involved).
That's also true for updates (ah, have you though of that?): if you try to update a record and the update will produce a duplicate, the unique constraint will block it.
create a unique index on these 2 columns.

check duplication using mysql select

I have a category say ecommerce.add ebay and amazon.when i update ebay as amazon,it should n't update.How do i d it?
I suggest you check out unique indexes and primary keys. These will cause an insert or update to fail rather that allow duplicate entries to be made.
CREATE UNIQUE INDEX name_unique ON tablename (name(10));
Replace name_unique with the name you want for the index, tablename with the name of your table, and name(10) with the column name and how many characters you want to be unique (the length of the column if you want the entire value to be unique).