1062 - Duplicate entry 'button_buynow' for key 'PRIMARY' - mysql

I know this has been discussed before but when I read the other threads, they don't seem to address my problem.
When I try to run the SQL query in PhpMyAdmin, I get the error :
#1062 - Duplicate entry 'button_buynow' for key 'PRIMARY'
I am sure the table was empty prior to me running the query so I don't know what's going on. Can somebody shed a light?
CREATE TABLE IF NOT EXISTS `buttons` (
`name` varchar(255) NOT NULL default '',
`value` text NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `buttons`
--
INSERT INTO `buttons` (`name`, `value`) VALUES
('button_buynow', 'buynowCC_LG.gif'),
('button_addtocart', 'x-click-but41.gif'),
('button_viewcart', 'viewcart_LG.gif'),
('button_freedownload', 'downloadnow.jpg');

I am sure the table was empty prior to me running the query so I don't know what's going on.
If you're sure that the table was empty you might have a trigger defined on this table that is the cause of this error.
You can check it this way
SELECT *
FROM information_schema.triggers
WHERE trigger_schema = schema()
AND event_object_table = 'buttons'
If you do in fact have a trigger then you either fix it or just drop it.

Related

How to suppress unique key checking while sql insert

I got a MySQL database with some tables.
In one of these tables i want to insert by a SQL script some new rows.
Unfortunately i have to insert in two columns an empty string and the two columns are part of an unique key for that table.
So i tried to set UNIQUE_CHECKS before and after the insert, but i'm getting errors because of duplicate entries.
Here is the definition of the table:
CREATE TABLE `Table_A` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`number` varchar(25) DEFAULT NULL,
`changedBy` varchar(150) DEFAULT NULL,
`changeDate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`,`number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And the INSERT statement which causes error:
SET UNIQUE_CHECKS = 0;
INSERT INTO `Table_A`
(`name`, `number`, `changedBy`, `changeDate`)
SELECT DISTINCT '', 'myUser', CURRENT_TIMESTAMP
FROM Table_A
AND id NOT IN
(
SELECT DISTINCT id
FROM Table_A
);
SET UNIQUE_CHECKS = 1;
As You can see, i'm using UNIQUE_CHECKS.
But as i said this doesn't work properly.
Any help or suggestion would be appreciated.
Patrick
Switching off Unique Keys for the insert operation doesn't indicate that it will check uniqueness only for the operations that happen after you switch it on again. It just means that database will not waste time to check the constraint during the time it is switch off but it will check the constraint when you switch it on again.
What it measn is that you nead to ensure that column has unique value in a columns with Unique Keys before you can turn it on. Which you don't do.
If you want to maintain Uniqueness somehow for new records you insert after some point in time you would need to create trigger and manually check the new records against already existing data. The same possibly goes for updates. But I don't recommend it - you should probably redesign data so either the Unique Key is not there or the data is truly unique for all the records there are and will be.

SQL Query not working on RDS

Using MySQL 5.6.40
This is the table definition:
CREATE TABLE IF NOT EXISTS `updated_tables` (
`table_name` VARCHAR(50) NOT NULL,
`updated_at` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY (`table_name`),
UNIQUE INDEX `table_name_UNIQUE` (`table_name` ASC))
ENGINE = InnoDB;
This is the query (Generated by Sequalize):
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL) ON DUPLICATE KEY UPDATE `table_name`=VALUES(`table_name`), `updated_at`=VALUES(`updated_at`);
Simplified form of the same query:
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL)
And it produces the following entry in table:
table_name: workdamnit
updated_at: 2018-07-05 14:27:17.142494
Now to the question.
Using MySQL 5.6.39-log on AWS RDS
Gives this error:
Error Code: 1048. Column 'updated_at' cannot be null
Is it since the MySQL versions are a bit different, or it has to do something with RDS?
During the creation of your table you have mentioned the following on your column: updated_at:
Do not allow NULL
If nothing is provided, use CURRENT_TIMESTAMP(6) as default.
Hence, It is giving the error when you are inserting NULL in your query.
So, If I understand what you are trying to do currectly, this should be your query:
INSERT INTO `updated_tables` (`table_name`) VALUES ('workdamnit')
instead of
INSERT INTO `updated_tables` (`table_name`,`updated_at`) VALUES ('workdamnit',NULL)
Hope it helps
You are trying to INSERT NULL on a field that does not allow it. Furthermore this field has a default value. so you can simply try:
INSERT INTO `updated_tables` (`table_name`) VALUES ('workdamnit') ...
After some research I found the answer. The RDS instance has a strict mode turned on by default, so it was responding properly. While my local instance, and also an instance of MySQL on EC2, were not using the strict mode.

Truncate table & insert ignore result in a #1062 - Duplicate entry for key 'PRIMARY' error

I have a very strange thing happening, this is my query:
TRUNCATE TABLE `cms_clients`;
INSERT IGNORE INTO `cms_clients` (`id`,`col1`,`col2`,`col3`) VALUES
(4,NULL,NULL,NULL),
(5,NULL,NULL,NULL),
(6,NULL,NULL,NULL),
(7,NULL,NULL,NULL),
(8,NULL,NULL,NULL);
The only primary key I have is id, and even though I have a truncate and insert ignore in the query it still results in the error:
#1062 - Duplicate entry '4' for key 'PRIMARY' error but when it previews that error and I view the table I can see that the rows 4 & 5 are already inserted, so the row 6 triggered the error; when I manually delete the rows 4 and 5 and run the query again I get the same error but on a different row: #1062 - Duplicate entry '6' for key 'PRIMARY' and same the previous one when I view the table I can see that the rows 4,5,6 and 7 are inserted so I believe that the row 8 is triggering the error.
This is the table's schema:
CREATE TABLE `cms_clients` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`col1` varchar(200) NOT NULL,
`col2` varchar(180) NOT NULL,
`col3` varchar(180) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
Am I doing anything wrong or is my mysql starting to act a bit strange?
EDIT
I just tried it on another computer and it's working fine, I think the issue is in my mysql not the query.

ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

i am having an issue while im doing a project in Secure Shell. Im getting the above error even though the table im trying to insert into is empty.
my work includes:
CREATE TABLE LARGE_SLIP(
MARINA_NUM CHAR(4) PRIMARY KEY NOT NULL,
SLIP_NUM CHAR(4) NOT NULL,
RENTAL_FEE DECIMAL(8,2),
BOAT_NAME CHAR(50),
OWNER_NUM CHAR(4));
INSERT INTO LARGE_SLIP (
SELECT MARINA_NUM, SLIP_NUM, RENTAL_FEE, BOAT_NAME, OWNER_NUM
FROM MARINA_SLIP WHERE LENGTH = '40');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
I tried looking up an answer but most results say that something has been assigned in the table that im trying to insert to. Yet the table is empty. Im still learning and this is actually for a class homework. Any help is appreciated
Run the sub query on its own, do you get dupe MARINA_NUM values?
SELECT MARINA_NUM, SLIP_NUM, RENTAL_FEE, BOAT_NAME, OWNER_NUM
FROM MARINA_SLIP WHERE LENGTH = '40';
If the entire row is duplicated you can use UNIQUE. If not, you'll need to find another way to limit the rows with dupe MARINA_NUM values.
I figured out what the problem was
When you create the table for this problem you actually create 2 primary key's
CREATE TABLE LARGE_SLIP(
MARINA_NUM CHAR(4) NOT NULL,
SLIP_NUM CHAR(4) NOT NULL,
RENTAL_FEE DECIMAL(8,2),
BOAT_NAME CHAR(50),
OWNER_NUM CHAR(4), PRIMARY KEY(MARINA_NUM, SLIP_NUM));

MySQL stop inserting when duplicate key condition is met

I have a table constructed by the followinng:
CREATE TABLE IF NOT EXISTS test_table (
ID int(11) NOT NULL AUTO_INCREMENT,
ProfileID int(11) NOT NULL,
ForeignID int(11) NOT NULL,
PRIMARY KEY (ProfileID,ForeignID) )
ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I want to do something a little peculiar though, say there are 4 records in the database:
RecA, RecB, RecC, RecD
I would like to run the following query and have the insert behavior stop when a duplicate key was encountered:
INSERT IGNORE INTO test_table (ProfileID, ForeignID) VALUES(RecE, RecF, RecA, RecB, RecG);
So the query would only insert RecE and RecF, is there a way to do this in MySQL, perhaps using ON DUPLICATE KEY? Ideally the execution would just be terminated once a duplicate has been found, I am not too familiar with SQL syntax though.
Where RecG was explicitly not inserted.