Duplicate entry error when inserting into table in MySQL - mysql

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.

Related

Trying to make a foreign key in a table, SQL throws an error

Within my table of studentTakingModule I am trying to set the column of pageID in there a foreign key, which leads back to the primary key in the table of pages. Please can someone guide me as to where I have gone wrong as SQL throws this error at me:
DB Design:
Run
select studentTakingModule.PageID
from studentTakingModule
where not exists (
select 1
from pages
where pages.PageID = studentTakingModule.PageID
);
Presumably (based on the error message you received) the result will be non-empty. The values in the result are PageID values which do not exist in the pages table. If such values exist, you cannot create that foreign key. You will need to make sure that all the not null PageID values in your studentTakingModule exist as pages.PageID. Otherwise the reference your record should point to would not exist. This is what the error message is telling you.

i am getting error while using NEW.id in database triggers

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.

Why everytime I go and execute insert statements that I want to insert into a table, it says field doesn't have a default value?

Here is the screenshot of the Parts table
INSERT INTO Supplies(Cost)
VALUES(10.0);
INSERT INTO Supplies(Cost)
VALUES (20.0);
INSERT INTO Supplies(Cost)
VALUES (30.0);
SELECT * FROM supplies;
Here is the Supplier table
Hello Everyone,
I am working on creating a database (three tables) in MySQL and every time I run an insert statement to input a record into my Supplies table, it says 'Field Supplier_SupplierID' doesn't have a default value'. When I created my model in MySQL, I already auto-incremented 'Parts_PartID' column and that error doesn't exist anymore. So why is it doing it with 'Supplier_SupplierID' column now? Can someone please help me? Here is my model pic and sample of SQL code when I insert records into the Supplies table.
You cannot insert into Supplies without providing the foreign keys for the Parts table and Suppliers table. While those primary keys in those tables may be incrementing automatically, they need to be already known when inserting into Supplies.
Typically you will use the known PartID and SupplierID that have previously been created when inserting into the Supplies table. Those columns cannot be NULL (by definition of your table).
'Field Supplier_SupplierID' doesn't have a default value' means that you are not providing a value, there is no default value and it's not allowed to be NULL.

Issues with MySQL inserting columns below desired location

I have been having some frustration attempting to add data values to this table students. I have all the other data values and have dropped and created the column student_id. However, when trying to add the data with this query:
insert into students(student_id) values('1'),('2'),('3'),('4'),('5');
The data does not insert correctly, as it creates new columns below the first 5 which contain data.
It must be because of my not null values, but I can't not have the not null identifier.
Is there a query command that allows me to change data within already existing value-filled columns? I have been unsuccessful in finding this so far.
Here are some images to explain the problem further.
The query I have made to add my values to the table:
The data was inserted but as it is underneath the columns I need to map with a foreign key, I cannot use the column as the top 5 values are still my not null default, which is required to let me create the foreign key
Looks like you already have your records initially created without the student_id field, you want to UPDATE the current records but you're actually INSERTING new records.
You're meant to update your students with update statements such as "UPDATE students SET student_id = X where condition = Y"
Then it looks like your student_id is your primary key which you should set to AUTO_INCREMENT value.
Regards
INSERT is the wrong command since you want to update existing rows. The problem here lies within the fact that the order of the rows is nondeterministic and I think you cannot update them in one statement. One solution would be as follows:
UPDATE students SET student_id = 1 WHERE first_name = 'Berry';
UPDATE students SET student_id = 2 WHERE first_name = 'Darren';
I hope you really do have only 5 columns to update :-)

Copy entire table into other table and change id

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.