Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error: Duplicate entry '1' for key 1

Posted on 2004-10-23
6
Medium Priority
?
18,191 Views
Last Modified: 2011-10-03
Hi,
When i am inserting date in just created table it returns following error: Duplicate entry '1' for key 1. The primary key is set to auto_increament. What could cause the problem and where should i look(beginer do not know where)? Here is the code:
//inset data into "movie" table
$insert="INSERT INTO movie (movie_id, movie_name, movie_type, movie_year, movie_leadactor, movie_director) VALUES (1, 'Bruce Almighty', 5, 2003, 1, 2),
(2, 'Office Space', 5, 1999, 5, 6),
(3, 'Grand Canyon', 2, 1991, 4, 3)";
$results = mysql_query($insert)
or die(mysql_error());
########
for creating table:
//creat movie table
$movie="CREATE TABLE movie (
movie_id INT(11) NOT NULL AUTO_INCREMENT,
movie_name VARCHAR(255) NOT NULL,
movie_type TINYINT(2) NOT NULL DEFAULT 0,
movie_year INT(4) NOT NULL DEFAULT 0,
movie_leadactor INT(11) NOT NULL DEFAULT 0,
movie_director INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY (movie_id),
KEY movie_type (movie_type,movie_year)
) TYPE=MyISAM AUTO_INCREMENT=4 ";

Thank's
m
0
Comment
Question by:margotsk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12391602
Hi margotsk,

Primary keys are unique, meaning that they must always have different values. If you want to change the row that has movie_id of 1, use this:

"UPDATE movie SET movie_name='Bruce Almighty', movie_type=5, movie_year=2003, movie_leadactor=1, movie_director=2 WHERE movie_id=1"

for the first one, (for example)

Regards,
Zyloch
0
 
LVL 8

Accepted Solution

by:
sigmacon earned 2000 total points
ID: 12391620
margotsk, I ran your queries, they both worked fine. If you run the insert statement TWICE, it gives the error you are describing. Run

select * from movie

first before you try to insert data to see that's not already in there.
0
 
LVL 26

Expert Comment

by:Umesh
ID: 12392122
Hi,

in your SQL statement..

AUTO_INCREMENT=4

This means your auto increment should start from 4 or so..

try with this..

//inset data into "movie" table
$insert="INSERT INTO movie (movie_id, movie_name, movie_type, movie_year, movie_leadactor, movie_director) VALUES ('','Bruce Almighty', 5, 2003, 1, 2),
('','Office Space', 5, 1999, 5, 6),
('','Grand Canyon', 2, 1991, 4, 3)";



0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 15

Expert Comment

by:JakobA
ID: 12396183
The standad way is to just not mention the auto-increment field at all in the INSERT statement:

INSERT INTO movie
            ( movie_name,      movie_type, movie_year, movie_leadactor, movie_director)
VALUES ( 'Bruce Almighty', 5,               2003,          1,                       2                    )
0
 

Author Comment

by:margotsk
ID: 12397124
Thank's guys for getting back,
it was the case as Sigmacon pointed out and it happene because at the first time, i was trying to insert separate entries for each table(total 3 table) at the same time using php code, but it return error after insereting first two and failing on last one. So, afterwards, when i was trying to run each insertion separetly it return this error, because i had the entries already inserted.
Thank's
All the best,
margotsk
0
 

Expert Comment

by:zzapper
ID: 14385783
Thanks this solution helped me!!
This is one of those brilliantly obscure error messages.
It is actually saying "You are trying to re-insert a record that already exists (duplicate key).

In my case I'd duplicated a table, but the Primary Key autoincrement had not been carried across.
I assigned the PK autoincrement and all was OK

0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question