I want to copy all the rows from one table to another and change the ID if there is a duplicate.
I'm using phpmyadmin and tried the operations tab.
Copy table to (database.table):
Data Only
Add Auto Increment
This is the SQL it gives me:
INSERT INTO `wsuca2_dbwsuca2`.`cxtb4_menu` SELECT * FROM `wsuca2_dbwsuca2`.`j25_menu`
This is the error I'm getting:
#1062 - Duplicate entry '0-0-root-*' for key 'idx_client_id_parent_id_alias_language'
what you need is
INSERT ...... ON DUPLICATE KEY UPDATE
also, "change the ID if there is a duplicate" is not enough it seems. Your table have a complex unique key of client_id,parent_id,alias,language.
both your table had an entry of with the above field set to 0-0-root-* so it throws a error because MySQL doesn't know how to handle it.
either
update those entry manually before copying over
use INSERT ...... ON DUPLICATE KEY UPDATE to specify how to update those entries when found.
use INSERT IGNORE to ignore all duplicate entries (probably not what you want)
Having Duplicate key error is clear notification that you did not use the same structure and keys in the both tables. So first recreate the structure:
DROP TABLE `wsuca2_dbwsuca2`.`cxtb4_menu`;
SHOW CREATE TABLE `wsuca2_dbwsuca2`.`j25_menu`; //old table structure
and paste the structure of j25_menu as structure od cxtb4_menu (change the name of the table). After that insert the data with your INSERT clause.
Related
I have a big table already in MySQL and I'm trying to create a another smaller table that consists of data from the big one. I'm using this code:
CREATE TABLE IF NOT EXISTS borrower (
idBorrower VARCHAR(255),
borrower_address VARCHAR(255),
PRIMARY KEY (borrower_address)
);
INSERT INTO borrower
(idBorrower,
borrower_address)
SELECT idBorrower, borrower_address
FROM final_data;
I'm using the borrower_address as the PK because it is made up of email addresses and all of them are unique/ no duplicates. When I run the code to insert the data into the new borrower table I get the below error:
Error Code: 1062. Duplicate entry 'rmeigh8g#51.la' for key 'borrower.PRIMARY'
The email address the error message is mentioning is in the first row of the big table(final_data) that I'm inserting the data from. But there is no duplicate value, it only appears once in the big table.
Any idea what might be causing the issue or how to fix it?
If the data does not exist in the original table, then it would appear to exist multiple times in final_data. You can easily check this:
SELECT borrower_address, COUNT(*)
FROM final_data
GROUP BY borrower_address
HAVING COUNT(*) >= 2;
You can fix this problem then by using GROUP BY or some other construct to only fetch the data once -- or use ON DUPLICATE KEY UPDATE.
i am trying to create a trigger for inserting data.and I want to get the current id for that author or user who is inserting or change in table.but I am getting this error:
unknown column id in new
here is my query
INSERT INTO logs VALUES(null,NEW.id,'inserted',NOW());
I have two table in my database comments table and logs table...
can anybody help me how can I get rid of this error...
It sounds like your comments table has columns like this:
CREATE TABLE comments (
comment_id ...
author ...
message ...
);
But your INSERT statement in the trigger references NEW.id. There is no id column in your comments table. The NEW.* syntax is a way to get access to the row that made the trigger execute. You can only reference columns that exist in that table.
So since I guess your primary key column is comment_id, you should drop the trigger and recreate it to run the INSERT like this:
INSERT INTO logs VALUES(null,NEW.comment_id,'inserted',NOW());
The point is that the NEW.comment_id must reference a column in the comments table, for which you defined the trigger.
I have a table containing some duplicate values for 1 column, ie with table emails
id email
1 test#test.com
2 test#test.com
3 more#most.many
4 cook#sheep.com
I'd like to remove row with id '2'. And I'd like to do this by creating a unique index of email thus forcing the table to drop redundancies.
I have seen this method referenced here(http://www.it-iss.com/mysql/sql-removing-duplicate-records/) and https://stackoverflow.com/questions/19000050/how-to-delete-first-of-a-double-record-with-alter-ignore-command-in-mysql
But when I attempt the statement
alter ignore table emails_test add unique index(email)
I get a duplicate entry error for test#test.com, as if I never included the ignore keyword
Is there something I'm missing here? If this is not possible, what are alternative methods of deleting duplicates that are simpler than, say, using temporary tables MySQL Error 1093 - Can't specify target table for update in FROM clause
You could try doing this as a few separate steps:
CREATE TABLE _emails LIKE emails
ALTER TABLE _emails ADD UNIQUE INDEX(email)
INSERT IGNORE INTO _emails SELECT * FROM emails
RENAME TABLE emails TO emails_old, _emails TO emails
Sorry for my poor english.
I would like to use the "on duplicate key update" but I do not know how to.
My MySQL database is alike :
id (primary key | autoincrement), id_hostel, date, allotement
My MySQL query :
insert into table (id_hostel, datebvj, allotement) VALUES
('1','09/05/2012','7'), ('1','10/05/2012','5'),
('1','11/05/2012','6')
ON DUPLICATE KEY UPDATE allotement=VALUES(allotement)
allotement means rooms
The problem : This query makes an insert query even if there is already a data in the database.
I would like the query to run good.
Any suggestion ?
Thank you very much.
An 'on duplicate' will only convert into an update if the insert would result in a duplicate record being created, where duplicate means a unique/primary key index would be violated.
Given your table structure, you'd have to insert a duplicate id field to trigger the conversion. None of your other fields have unique keys, and your insert statement is not inserting an id value, so there is no way to trigger the insert->update switch.
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