1

I'm doing this homework for school and I would like my primary key (id) to go 1, 2, 3 and so forth. but if I delete a row and add a new person, it skips that number.

This is my query from mySQL workbench

CREATE TABLE IF NOT EXISTS person
(
id              int(11)         NOT NULL auto_increment primary key,
first_name      varchar(45),
last_name       varchar(45)
);     

I'm not really sure where I have to look and what to change.

My addPerson statement is this:

public Person addPerson(Person p){

String sql = "INSERT INTO person (first_name, last_name) VALUES(?,?) ";

template.update(sql, p.getFirst_name(), p.getLast_name());

return null;
}

Also im not asking why it shouldn't be done but rather if it's possible or not

FakeRune
  • 63
  • 1
  • 3
  • 13

2 Answers2

0

The whole point of auto increment is so that you don't have to count for yourself. When you query your database you should use another column to do that(like name,age or something else) then from the result you can get the id of that row. So there's really no point in keeping track of the id

Morpheus
  • 57
  • 8
0

That's not something that you can do. Nor is it something that you'd really ever want to do in a real-world scenario. If keys can be reused, it would cause a significant performance hit of having to do a full table scan every time you add a new record to find an available key (the functionality you're seeing results from simply tracking the highest key value generated). Not to mention the risk of ending up with inconsistent data in instances where you have foreign key constraints disabled for a more complex database.

As such, this isn't an ability that is built into any databases that I'm aware of. Is there a reason (other than aesthetics) for wanting to do this?

Jordan
  • 2,273
  • 9
  • 16