I have a table in a PostgreSQL database called reviews that has an id, comment, latitude, longitude, address and username column. I'm updating the contents of the table via input from a Spring Boot application therefore I will have a query inside the application that will pass variables into the table via the query. My issue is that I cannot update the id from the application so I would like to have the row's id to automatically update by one when a new row is added/inserted. This will be the query in the applicaiton (where the values will be represented by java variables based on the user input).
INSERT INTO reviews (id, comment, longitude, latitude, address, "user")
VALUES (nextval(id), 'Test 2 ',6.204510300000038, 53.3660165, '16 Curzon Street, Dublin', 'user1234');
So based on this I have two questions:
- Is it possible to create a sequence for a table that already exists so I can update the id by one everytime a new row is added?
- And do I have to create this sequence before every query from the application or do I create one and the database knows to update the value by one based on each query?
I find the documentation to be quite broad for this topic and I'm a novice with SQL/PostgreSQL so forgive me if I've explained this inefficiently.