insert into 2column table, avoiding duplicate records - mysql

I have a table with 2 columns, userid and messageid. I am updating it automatically through a php script, but I can't get a working insert statement. I don't mind if there are duplicates of userid, or duplicates of messageid (in fact there should be duplicates of both), I just don't want any duplicate of the same combination of userid and messageid. Is there any way to write a query that will do this for me, or do I have to handle it at the php level?
I've probably tried 20 different queries that I found on here and google, but have not gotten it right. This was the last thing I tried:
INSERT INTO interests_join (userid, interestid)
VALUES (1, 4)
WHERE NOT EXISTS
(SELECT userid, interestid FROM interests_join WHERE userid = 1 AND interestid = 4)

You can add a UNIQUE KEY, sql will refuse to insert a new row that is a duplicate of an existing one.
ALTER TABLE `interests_join` ADD UNIQUE `row` (`userid`, `interestid`);
Then you'll have to check from PHP if the query was successful or not (error #1062). You can't apply the key if there are duplicate rows, you have to remove them first .

Related

SQL insert avoiding duplicates from queried table + avoiding to insert duplicates in updated table

I wish to update specific columns of a table from certain columns from a second table avoiding duplicates in a unique column, with the extra twist that the second table itself has duplicates.
INSERT INTO cie (cik,name,sic,fye)
SELECT dumpsubq2.cik,dumpsubq2.name,dumpsubq2.sic,dumpsubq2.fye
FROM dumpsubq2
WHERE NOT EXISTS(SELECT cik
FROM cie
WHERE dumpsubq2.cik = cie.cik)
Now this would work fine if only there were no duplicates in the dumpsubq2 Table. So the exploit is to somehow include DISTINCT while querying the dumpsubq2 table. And i fail to achieve this in one pass :/
Do i have to do this in 2 steps ?
Eliminating duplicates in the dumpsubq2 in a temp table
Play above query that stops with (error #1062 - Duplicate entry '1606069' for key 'cie.cik') when it encounters a duplicate in dumpsubq2)
Thanks for your help :)
This query did it for me :)
INSERT INTO cie(cik,name,sic,fye)
SELECT * from ( select distinct cik,name,sic,fye FROM `dumpsubq2` WHERE form = '10-k') AS tmp
WHERE NOT EXISTS (
SELECT cik FROM cie WHERE tmp.cik = cie.cik
)

SQL "Replace Into" drop some of my rows

I'm trying to write a SQL statement that replace instead of update.
The columns of my table look like that
(id
block
region
login
password
email
business
firstname
name
version
updatable
bodyshop_id
mac
register_date
lastvisite_date
enum_test
address1)
and when I run a statement like this:
REPLACE INTO `users` (`login`, `firstname`, `region`, `address1`, `enum_test`, `block`, `id`) VALUES ('Samira GO', 'Samira', 'all', 'lmklm', '1', '0', '2')
Samira have the id number two. (target of the replace ;) )
The person with the id number one is drop by the request.
(The primary id key of the table is id+login+email)
(When I ask this request to SQL it told me that 3 lines are affect)
If you want to ask, id, login, or email are some primary value, so I don't understand how it would be able to change some value with another id or login
From the MySQL REPLACE doc:
The REPLACE statement returns a count to indicate the number of rows affected. This is the sum of the rows deleted and inserted. If the count is 1 for a single-row REPLACE, a row was inserted and no rows were deleted. If the count is greater than 1, one or more old rows were deleted before the new row was inserted. It is possible for a single row to replace more than one old row if the table contains multiple unique indexes and the new row duplicates values for different old rows in different unique indexes.
So, it sounds like one row was inserted and two rows were deleted.
Examine your table definition and see if there are any UNIQUE indexes other than the PRIMARY KEY. Note also that while you say the primary key is id, login, email, your query doesn't specify email. If two rows existed that matched id and login but had different email, they may have both been deleted.
You may also consider that what you wanted to do is an INSERT ... ON DUPLICATE KEY UPDATE instead of a REPLACE. REPLACE functions more like a combined DELETE then INSERT.

Insert if not exist and Update if exist

I have three columns on my database table
user_id, post_id, and vote
I want to insert a new data if user_id and post_id don't exist. But if both columns user_id and post_id exist i will be able to update 'vote' column value. I set user_id to be unique but it proves to be not working since i want user to insert votes on different post.
The query below only updated the value of vote since user_id already exist. I want to have it updated if and only if user_id and post_id existed
I used this sql query
INSERT INTO polls (user_id,post_id,vote) VALUES (1,2,5)
ON DUPLICATE KEY UPDATE vote= ?;
Here's my problem
You must create unique key combination
Create unique index your_index_name on yourtable (field_one,field_two),
then do the insert into , on duplicate key logic
It is absolutely logical that your code does not work as intended, because your only key is user_id, thus if you want to check the uniqueness of user_id AND post_id, then you should set it as so.
Don't think you can do it purely in MySQL :*( post_id would have to be unique and you said that does not fit your business logic. Furthermore, if multiple keys are detected, they are joined by an OR in the resulting query, which will further complicate things for you. Here's an excerpt from the manual, where a and b are keys involved in the ON DUPLICATE KEY query:
If a=1 OR b=2 matches several rows, only one row is updated. In general, you should try to avoid using an ON DUPLICATE KEY UPDATE clause on tables with multiple unique indexes.

My SQL INSERT INTO based on conditions

I have a table called autosaves where i my web-app saves every 4 second a user autosave in case my web-app crashes.
autoSaves
customerId
designType
autosaveFile
The condition is this:
If a customerId and designtype already exists, update the row with these values(customerId designType autosaveFile)
otherwise if the 2 conditions i mentioned do not exist then create a new row with the new values.
I have come accross the Insert Into statement but i cannot seem to understand how to formulate it so that it updates when the 2 conditions are met.
You need to create a unique index on the customerId and designType columns:
CREATE UNIQUE INDEX ix_cust_design ON autoSaves (customerId, designType);
Then you can use the following INSERT statement:
INSERT INTO autoSaves (customerId, designType, autosaveFile)
VALUES (#id, #type, #file)
ON DUPLICATE KEY UPDATE autosaveFile = VALUES(autosaveFile)

Fix DB duplicate entries (MySQL bug)

I'm using MySQL 4.1. Some tables have duplicates entries that go against the constraints.
When I try to group rows, MySQL doesn't recognise the rows as being similar.
Example:
Table A has a column "Name" with the Unique proprety.
The table contains one row with the name 'Hach?' and one row with the same name but a square at the end instead of the '?' (which I can't reproduce in this textfield)
A "Group by" on these 2 rows return 2 separate rows
This cause several problems including the fact that I can't export and reimport the database. On reimporting an error mentions that a Insert has failed because it violates a constraint.
In theory I could try to import, wait for the first error, fix the import script and the original DB, and repeat. In pratice, that would take forever.
Is there a way to list all the anomalies or force the database to recheck constraints (and list all the values/rows that go against them) ?
I can supply the .MYD file if it can be helpful.
To list all the anomalies:
SELECT name, count(*) FROM TableA GROUP BY name HAVING count(*) > 1;
There are a few ways to tackle deleting the dups and your path will depend heavily on the number of dups you have.
See this SO question for ways of removing those from your table.
Here is the solution I provided there:
-- Setup for example
create table people (fname varchar(10), lname varchar(10));
insert into people values ('Bob', 'Newhart');
insert into people values ('Bob', 'Newhart');
insert into people values ('Bill', 'Cosby');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Adam', 'Sandler');
-- Show table with duplicates
select * from people;
-- Create table with one version of each duplicate record
create table dups as
select distinct fname, lname, count(*)
from people group by fname, lname
having count(*) > 1;
-- Delete all matching duplicate records
delete people from people inner join dups
on people.fname = dups.fname AND
people.lname = dups.lname;
-- Insert single record of each dup back into table
insert into people select fname, lname from dups;
-- Show Fixed table
select * from people;
Create a new table, select all rows and group by the unique key (in the example column name) and insert in the new table.
To find out what is that character, do the following query:
SELECT HEX(Name) FROM TableName WHERE Name LIKE 'Hach%'
You will se the ascii code of that 'square'.
If that character is 'x', you could update like this:(but if that column is Unique you will have some errors)
UPDATE TableName SET Name=TRIM(TRAILING 'x' FROM Name);
I'll assume this is a MySQL 4.1 random bug. Somes values are just changing on their own for no particular reason even if they violates some MySQL constraints. MySQL is simply ignoring those violations.
To solve my problem, I will write a prog that tries to resinsert every line of data in the same table (to be precise : another table with the same caracteristics) and log every instance of failures.
I will leave the incident open for a while in case someone gets the same problem and someone else finds a more practical solution.