0

I have problem with ids when adding new object to sql table.

My super class has name and id + 2 constructors:

public Car(String name ) {
    this.name = name ;
}

public Car(long id, String name ){
    this.id = id;
    this.name = name ;    
}

Subclass has also 2 constructors with id and without.

From H2 i read like this:

  public static List<Car> addAllCars() throws SQLException, IOException {
        Connection connection= connectToDatabase();
        List<Car> listOfCars= new ArrayList<>();
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM CARS");

        while(rs.next()){
            Long id = rs.getLong("id");
            String name= rs.getString("name");
            Integer noOfDoors = rs.getInt("noOfDoors");        
            Car newCar= new Car(name, noOfDoors);
            newCar.setId(id);

            listOfCars.add(newCar);

        }

        closeConnectionToDataBase(connection);

        return listOfCars;

    }

When I add new Car (user inputs name and noOfDoors, I should get new id)

 Connection connection= Database.connectToDatabase();

        PreparedStatement query=
                veza.prepareStatement("INSERT INTO CAR(name,noOfDoors) VALUES(?,?) ");

        query.setString(1, cars.getName());
        query.setInt(2, cars.getNoOfDoors());
     

        query.executeUpdate();
        
        Database.closeConnectionToDataBase(connection);

The problem is I import 5 cars. Ids from 1 to 5. But when I add a new car it goes from id 40 and always increases by 1. If I import 10 cars I would go from 40 to 50. If I delete those 10 cars it will also continue from id 50 and so on. I thought id in h2 will auto-increment automatically.

2
  • Do you mind showing the DDL for the table by updating the main post? Commented Apr 26, 2021 at 19:01
  • I would by I dont know how to get ddl from H2. I'll try to do that. Commented Apr 26, 2021 at 19:10

1 Answer 1

2

Don't expect Primary Key IDs to be sequential.

It's a common misconception that primary keys should be sequential. The one and only requirement is that the PK should have unique values, so it can act as a row identifier.

If the values grow one by one, or if there are gaps, or if the numbers are random, it doesn't really matter. As long as they are unique your application will work well.

It's typical that database engines will buffer segments of numbers for different threads, so different users won't step on each other toes when adding new rows. The side effect is that you'll see gaps of unused values in the table. Again, this is very unimportant.

Also typical is that numbers in the sequence are not recycled. You may later delete rows, but the identifier numbers of those rows will not be assigned again to new rows. Again, this means gaps may appear in your table’s row numbers, and this is very unimportant.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I asked a friend of mine he also said that but I didn't know that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.