How to update tables with duplicate keys in sql? - php

How can I update a table that accept duplicate keys in SQL?
I was using Insert Into on Duplicate Key, but for a new requirement I need rows with duplicate keys.
How do I achieve the same behavior that Insert Into on Duplicate Key statement.
Thanks

You should never have duplicate keys. It is against the structure of a relational database. If you need to do something like this, then you need to redesign your database or add a new table with the data you want to duplicate but apply it to many that hold unique data.

Related

How to make MySQL display duplicate data into my table?

I know this has something to do with maybe the Primary Key and Unique Keys, but I'm not sure how to how to make it work. Basically I want MySQL to generate a new row even if data in it is duplicate of last rows. Right now, any duplicate data from previous rows result in the row not being generated. Help is very appreciated.
The table you have defined has all columns as primary key and unique. It's ideal to maintain one column as primary key (perhaps with auto increment) and the rest as non-indexed columns. Check the table definition with the following mysql query if you are not familiar with using phpMyAdmin
desc tablename;

Sql/php - insert data to a table with foreign keys

How do you code in PHP to insert data into a table that has two foreign keys and these foreign keys are the primary key of different tables.
This is a many-to-many relationship so I am making a new table. I want to populate this table.
I searched a lot these past days on how to get the primary key of a row on the left table and right table to combine it in the middle table. But it all doesn't work. Please help.
You should use mysqli_insert_id() after each insert, keep each into its own variable, then use those as your FKs when inserting into the 3rd table.
It will return int. Have a look at the doc: PHP Doc

How does a foreign key get into a table?

thanks in advance for any help.
I have a question about foreign keys. I understand the concept of having the data from one table inserted into another for reference. But my question is, how does it get there?
Currently I have two tables and two forms. One form inserts data into table A, the other form inserts into B. Then I use a function to get the id from the last insert into A and insert it into B. Is this the proper way to do this or am I missing something?
There are two possibilities :
You know the primary key before the insertion in table A => Then your technique isn't the right one, since you're retrieving something you already added.
You don't know it (Example: auto-incremented id's) => Then your technique is the right one, and I don't think there is any other better way to achieve what you are asking for.
Note that what I called the primary key is the primary key of the row in table A, and a foreign key for rows in table B.
Short answer, I don't believe you aren't missing anything. There are many ways to accomplish what you are after, but your explanation is probably the most used and straightforward.
Another way is to use a trigger on table A to populate table B after insert (this only works if you do not need any additional user input, like form input to insert into table B).
As you cannot insert two ids at a time, yes it was an proper way.
First inserting the record on primary table, which we knows it.
Secondly, you that last insert id using the mysqli_insert_id() function
Now insert data on foreign table using this primary key.

"insert on duplicate update" still inserts duplicates

I have a mysql table with descriptionId as a primary key and it is auto incremented. it also has a "content" and a "price" columns and few more.
I also have a form consisting of multiple input boxes with the current database values of my price and content columns in my description table. after submitting the form i'd like to update the table with the new values and if any of the input boxes is deleted, the record must be deleted from the table.
I have also managed to define three arrays to hold the values of all my tables' columns. These Arrays are as followed: $descrId,$content,$price
when i submit my form, the php file loops through theses arrays and executes the following query:(I have validate these arrays so they work just fine)
INSERT INTO
description(descriptionId,content,price,orderNo,salesPerson,dateTime,updated)
VALUES('{$descrId[$k]}','{$content[$k]}','{$price[$k]}','{$orderId}','{$sale}',NOW(),1 )
ON DUPLICATE KEY UPDATE content=VALUES(content),price=VALUES(price),updated=1, dateTime=NOW()
However, this query keeps duplicating the values anytime i press submit.
I appreciate your time....
As I read your question ON DUPLICATE KEY will never occur, because your primary key is an auto incremented value, which will be +1 each time your insert something in the table, so it will never have a duplicated value - that's the idea more or less behind AUTO INCREMENT.
So the answer is to pick another column, i.e. orderNo or dateTime, make it UNIQUE and then try again with the query.
Update
Alternatively you can combine two or more columns and define them as a (unique) key.
If that's also not applicable in your case, then use some hashing function/algorithm when inserting the data and store that hash along the other values in the table.

PHP MySQL INSERT fails due to unique constraint

On insert I am catching the unique constraint mysql_errno() 1062.
This works fine but I want to find the existing row to re-instate or modify it.
Is there are method to obtain the row id on insert fail? I tried mysql_insert_id() but realised that would only return the row I'm inserting (or failed to insert) therefore, I get 0.
Is there no option but to issue another mysql_query and simply perform a select on the duplicate value?
I just want to make sure there is no better, quicker, more economical way to do this.
If you are attempting to insert a row if new or update existing values then REPLACE INTO is what you need. Also consider INSERT ... ON DUPLICATE KEY UPDATE Syntax if there are constraints involved as REPLACE INTO will DELETE and then INSERT.
You'd have to check the table for any "unique" keys (SHOW CREATE TABLE will list them all), and then query the table for the associated values in the insert query you'd attempted. So if you're inserting fields A,B,C,D and B,C have the unique key on them, then:
SELECT id, B, C FROM table WHERE B=$B and C=$C;

Categories