查询创建表格:
create table if not exists person ( roll_no int(4) AUTO_INCREMENT primary key, name varchar(25), city varchar(25));
查询设置自动增量主键的起始编号:
alter table person auto_increment = 1;
查询以插入数据:
insert into person (name,city) values("Maxwell", "Pune"); insert into person (name,city) values("Baldwin", "Bengaluru"); insert into person (name,city) values("Novartis", "Paris"); insert into person (name,city) values("Shaun", "Mumbai"); insert into person (name,city) values("Beckham", "Toronto"); insert into person (name,city) values("Ashish", "Bengaluru"); insert into person (name,city) values("David", "Paris"); insert into person (name,city) values("PK", "London"); insert into person (name,city) values("Chris", "Bengaluru"); insert into person (name,city) values("Aston", "Mumbai");
查询删除行:
delete from person where roll_no=5;
删除行后的表格结构:
roll_no name city 1 Maxwell Pune 2 Baldwin Bengaluru 3 Novartis Paris 4 Shaun Mumbai 6 Ashish Bengaluru 7 David Paris 8 PK London 9 Chris Bengaluru 10 Aston Mumbai
现在,在寻找恢复已删除行的同时,编译器 <错误 “Duplicate entry '5' for key 'PRIMARY'
”
用于重新设置已删除行的查询。
update person set roll_no = roll_no + 1 where roll_no >=4 order by roll_no desc; insert into person (roll_no, name, city) VALUES (5, "Beckham", "Toronto");
这个问题的可能原因是什么? 强烈建议任何输入。
答案 0 :(得分:1)
如此简单,在您的更新查询中,您将4变为5,然后尝试插入5.这就是它重复的原因。
答案 1 :(得分:1)
要恢复已删除的行,只需执行insert语句。可以简单地删除update语句并使用以下命令:
<强>查询强>
insert into person (roll_no, name, city) VALUES (5, "Beckham", "Toronto");