Query for creating table :
create table if not exists person ( roll_no int(4) AUTO_INCREMENT primary key, name varchar(25), city varchar(25));
Query to set start number for auto-increment primary key :
alter table person auto_increment = 1;
Query to insert data :
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");
Query to delete the row :
delete from person where roll_no=5;
Table structure after deleting the row:
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
Now, while looking to reinstate the deleted row, the compiler is throwing error as " Duplicate entry '5' for key 'PRIMARY' "
Query used to re-instate the deleted row.
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");
What could be the possible reason for this issue ? Any inputs would be highly recommended.
Aucun commentaire:
Enregistrer un commentaire