UUID primary keys with gen_random_uuid()
A SERIAL primary key is simple and fast, but it leaks information (row
count, insert order) and it only stays unique within one table in one
database - two services each handing out their own 1, 2, 3... will collide
the moment you try to merge their data. A UUID primary key sidesteps both
problems: it's a 128-bit value that's effectively guaranteed unique no matter
where or when it's generated, so services can create IDs independently and
never clash.
Postgres can generate one for you automatically with gen_random_uuid() as
the column's DEFAULT - you never have to invent an ID yourself, just leave
the column out of your INSERT's column list and Postgres fills it in.
You have a sessions table: id (UUID PRIMARY KEY DEFAULT gen_random_uuid()),
label. It already has one row ('login').
Your task: insert a new session labeled 'checkout' - without specifying
id - then return the total number of sessions.
You'll practice:
Letting a DEFAULT generate a UUID instead of supplying one
Leaving a column out of an INSERT's column list entirely
Show a hint
Show solution
Previous Next