Reputation: 111
I am making my first website with a database. Lets say I have a table called "items". Users may send items to be stored in the database. However, I don't know what type of Primary Key the items table should use.
I thought about using an integer primary key. Every time the user enters an item, I would find the current maximum value in the table, and increment it by 1 for the new value. However with this method what happens if two users enter items at the same time into the table? Could the items possibly end up using the same maximum value and end up having the same primary key resulting in an error?
Another way I thought about was using a character primary key based on the userID and date entered in the users table. So I would create the primary key out of "userID - entryDate". This way seemed very "unclean" to me.
I was wondering is there a standard way of handling primary keys in databases of web applications that may have multiple users entering information at the same time?
Sorry I am completely new to web dev and databases. For information, I am using Node.js as my server, and PostgreSQL as my databse.
Upvotes: 0
Views: 262
Reputation: 8116
There's the data type SERIAL
which does auto increment and is usually used for primary keys.
SERIAL data type documentation
The data types serial and bigserial are not true types, but merely a notational convenience for setting up unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases). In the current implementation, specifying
Upvotes: 1