I have a table accounts in Postgres which had id and name as fields. id is the primary key. I want to fetch the last primary key inserted into the table using this query :-
select id from accounts order by id desc limit 1 ;
Using a preparedStatement I run this query and get the value for my next primary key by incrementing it by 1.
String query="SELECT "+columnName+" FROM "+TableName+" ORDER BY "+columnName+" DESC limit 1";
PreparedStatement stmt=conn.prepareStatement(query);
ResultSet rs= stmt.executeQuery();
int counter= rs.getInt(1);
My question is if there are 0 rows, what will be the counter value?
If the value is null which i don't think can be stored in int, how do I assign it a starting primary int key value say 10 ?