MySQL错误:主键重复输入

时间:2018-09-19 05:54:24

标签: mysql phpmyadmin duplicates

SQL查询:

转储表new_recipe的数据

INSERT INTO `new_recipe` (`id`, `post_title`, `post_image`, `post_author`, `post_date`, `post_desc`) VALUES
(4, 'Daal Chawal', 'DDAa.jpg', 'Asad Khan', '2016-05-29', '\r\n Gujranwala agr pyara na hota\r\n\r\nGulshan Iqbal Park ka nizara na hota\r\n\r\nBypass pr ishara na hota\r\n\r\nSialkoti drwazy ka shara na hota\r\n\r\nPace pr janay ka mode dobara na hota\r\n\r\nBashir k dal chawal ka swad krara na hota\r\n\r\nsb Sattelite Town Girls Collage ka ksoor hai\r\n\r\nw\r\na Gujranwala ka koi larrka aawara na hota("\r\n\r\nGeo Gujranwala'),
(5, 'Daal Makhani', 'maxresdefault.jpg', 'Waqas Ali', '2016-05-29', '\r\n   siosadflwq r0ewlk hopxcyfsajr w4dfwsaf654wq13\r\nt1sd3v5sa +et5wq+e f80w+e5t+eqd7fv+6sd1v 3x4c6\r\n5v321d35g 02df6s0+52wer+t q032va0sd')
MySQL said: Documentation

#1062 - Duplicate entry '4' for key 'PRIMARY'

这是我的MYSQL查询

new_recipe的表结构

CREATE TABLE IF NOT EXISTS new_recipe (
  id int(10) NOT NULL,
  post_title varchar(250) NOT NULL,
  post_image varchar(250) NOT NULL,
  post_author varchar(250) NOT NULL,
  post_date date NOT NULL,
  post_desc text NOT NULL,
  PRIMARY KEY (id)
) AUTO_INCREMENT=6 ;

转储表new_recipe的数据

INSERT INTO `new_recipe` (`id`, `post_title`, `post_image`, `post_author`, `post_date`, `post_desc`) VALUES
(4, 'Daal Chawal', 'DDAa.jpg', 'Asad Khan', '2016-05-29', '\r\n Gujranwala agr pyara na hota\r\n\r\nGulshan Iqbal Park ka nizara na hota\r\n\r\nBypass pr ishara na hota\r\n\r\nSialkoti drwazy ka shara na hota\r\n\r\nPace pr janay ka mode dobara na hota\r\n\r\nBashir k dal chawal ka swad krara na hota\r\n\r\nsb Sattelite Town Girls Collage ka ksoor hai\r\n\r\nw\r\na Gujranwala ka koi larrka aawara na hota("\r\n\r\nGeo Gujranwala'),
(5, 'Daal Makhani', 'maxresdefault.jpg', 'Waqas Ali', '2016-05-29', '\r\n   siosadflwq r0ewlk hopxcyfsajr w4dfwsaf654wq13\r\nt1sd3v5sa +et5wq+e f80w+e5t+eqd7fv+6sd1v 3x4c6\r\n5v321d35g 02df6s0+52wer+t q032va0sd');

Image of the PHPmyadmin error`

1 个答案:

答案 0 :(得分:0)

在第二个转储数据库中,您使用相同的id,它已存在于“ new_recipe”中,这就是它显示重复条目的原因。

代码:

CREATE TABLE IF NOT EXISTS new_recipe (
                        id int(10) NOT NULL AUTO INCREMENT,
                        post_title varchar(250) NOT NULL,
                        post_image varchar(250) NOT NULL,
                        post_author varchar(250) NOT NULL,
                        post_date date NOT NULL,
                        post_desc text NOT NULL,
                        PRIMARY KEY (id)
                       ) AUTO_INCREMENT=6 ;

INSERT INTO `new_recipe` (`id`, `post_title`, `post_image`, `post_author`, `post_date`, `post_desc`) VALUES
('', 'Daal Chawal', 'DDAa.jpg', 'Asad Khan', '2016-05-29', '\r\n Gujranwala agr pyara na hota\r\n\r\nGulshan Iqbal Park ka nizara na hota\r\n\r\nBypass pr ishara na hota\r\n\r\nSialkoti drwazy ka shara na hota\r\n\r\nPace pr janay ka mode dobara na hota\r\n\r\nBashir k dal chawal ka swad krara na hota\r\n\r\nsb Sattelite Town Girls Collage ka ksoor hai\r\n\r\nw\r\na Gujranwala ka koi larrka aawara na hota("\r\n\r\nGeo Gujranwala'),
('', 'Daal Makhani', 'maxresdefault.jpg', 'Waqas Ali', '2016-05-29', '\r\n   siosadflwq r0ewlk hopxcyfsajr w4dfwsaf654wq13\r\nt1sd3v5sa +et5wq+e f80w+e5t+eqd7fv+6sd1v 3x4c6\r\n5v321d35g 02df6s0+52wer+t q032va0sd');

添加自动递增,并且在插入数据期间将id清空为自动递增。