In this PostgreSQL tutorial, you'll learn everything about Primary Keys, SERIAL, UUID v4, and UUID v7 with practical examples and real-world use cases.
We start by understanding what a Primary Key is and why every database table needs a unique identifier. Then we compare SERIAL and UUID-based IDs, discuss security concerns such as IDOR (Insecure Direct Object Reference), and explore why modern applications are moving toward UUID v7.
You'll also learn the differences between UUID v4 and UUID v7, understand B-Tree index performance, discover best practices used by professional developers, and build practical PostgreSQL examples step by step.
Whether you're a beginner learning PostgreSQL or an experienced developer preparing for interviews, this video will help you understand when to use SERIAL, BIGSERIAL, UUID v4, and UUID v7 in real projects.
⌛TIMELINE⏳ 0:00 Introduction to PostgreSQL Primary Key & SERIAL vs UUID 0:16 Real-World Example: Which ID Type Should You Choose? 3:04 What is a Primary Key? 4:10 Understanding SERIAL in PostgreSQL 5:05 UUID v4 vs UUID v7 Explained 6:00 Syntax & Table Creation Examples 7:40 Best Practices with Real-World Scenarios 9:40 Internal Table Example Using SERIAL 11:00 Public-Facing Table Example Using UUID v7 13:55 Understanding ID Types in PostgreSQL 16:10 Practical Query #1 16:50 Practical Query #2 18:16 Practical Query #3 19:50 Summary & Outro
AI Generated·May contain errors
Video Summary
AI Generated
This video explains the differences between Primary Keys and Identifiers in PostgreSQL, specifically comparing sequential integers (Serial/BigSerial) and Universally Unique Identifiers (UUIDs). The core focus is on security, performance, and choosing the right identifier based on whether the data is used internally or exposed via public URLs.
Hello, what's up, guys?
Welcome to PostgreSQL Series
In this video, we will see Primary Keys and Identifiers
What is it?
Before telling you that
I have kept some visuals in front of you
To make it engaging
Where you can see clearly
Whenever we insert data
We write like this
ID, BigSerial
If you remember
If you see this part
And the second one is UUID
These two ways we insert data
I will come here in a while.
But before that
Let's understand this
Why are we watching this video?
Why you should watch
Let's say we are sharing a payment or certificate link
Let's say
My company Codifier
I have to pay the salary
Payments
I will share the payment link with them.
You can see very clearly
If you use ID Serial
Which is default
1, 2, 3, 4
In this case, if I want to share the payment
My domain is here
You can see clearly.
Payment
And my unique identifier
1
I will send payment forward slash 2
What will happen
It will be predictable.
Very bad
Easy to enumerate
Anyone can change
Numbers and access other records
Obviously
Forward slash 2
Vulnerable to IDOR
Insecure Direct Object Reference
You are thinking
If you are an attacker
I have two questions-short
Rather than Serial
If you have used UUID
Here
See your link
Here you can see
Here
You can see
Payment /
Random integer
Not an integer
String
By the way
Here
If you use this
As an identifier
Here
We can't guess.
We don't know
1
2
3
4
5
6
7iser
8
9
Don't worry
Practical will come at the end
Here is a quick hint
You can't see it
I will do one thing.
Here you can see
My quick hint
If you are using a public URL
Always go with UUID
If you are using
Normal internal
You can go with Serial and Big Serial
Everyone suggests
To go with Big Serial
Like this
Let's go here
Let's understand
I hope the example I shared with you
You've got an idea
How important is this identifier?
Let's see
What is a primary key?
It is an absolute unique identifier
For a row
It has a primary key.
It is a unique identifier.
Here
The moment you use a primary key
That particular field
Like ID
That particular field
By default
Unique and not null
You don't need to write
Unique and not null
The moment you write primary key
By default
Use case such as student ID
In the users table
Here
A lot of people
Like me
This serial
Is not a data type
It is a pseudo type.
You can call it a convenience feature.
PostgreSQL provides
The moment you use serial
It automatically
Add auto increment
ELSE in MySQL
Second
It forms a sequence.
Third
In the heart of the city, a new wave of innovation is transforming the skyline.
Sequential
Auto-increment
It is predictable.
Fully predictable
Example
If you add payment
First
ID will be 1
If you add a second
It will be 2
Something called
Universally Unique Identifier
This is 128-bit.
We will see it later
There is one more.
Big serial
I will tell you
We have something called UUID version 7
This is for better performance
In which
It gives you
A random code
In which there is no idea
Whenever you use serial
There is a bit
On each page
Data has been added.
UUID
When data is added
In your B-tree
Without adding sequentially
On any page
In any field
It was added.
It was difficult to search.
To solve this problem
They introduced UUID version 7
In which
Based on your time
Whenever you inserted
The data was random.
In sorting
In searching
It was sequential.
And made it random
I will tell you
What to use
Before that
Let's see the syntax
This video is too long.
Standard integer
Primary
You can see
Create table
We have already created the table.
ID
You can see
Serial
You have to write a serial keyword
Basically pseudotype
Primary
We have seen
Unique identifier
This is your serial.
If you want to create this
You have to use this
At the end
You will pass this method
Jan
Random
UUID
Version 4
If you want to check
Code
Random
It is here
Why have we written
They have something.
Default
In case
User
UUID
By default
UUID
Default value
Not mandatory
If
Data is absent
By default
You can check
We have some best practices.
I will tell you
Example
We need to create
Payment
If you want to create
Payment
I hope
You are not bored.
Please comment
We are creating
Payment table
ID
Big serial
Public
UUID
We are using both
It is clear
We can use it
Amount
Numeric
Internal
External
Default
We have created a table.
We need to insert
Some data
Insert
Insert into payments
Amount
Insert
See
Data in payments
Select
Table
ID
Public
Version 7
UUID
V7
Amount
Internal
External
UUID
V7
Time-Ordered
Index Friendly
Exam
Next
Internal
Accountant
number
Accountant
ID
for payment ID equal to 2
Now an accountant has arrived; he needs to check his ID.
The second one was
So what will she do? Simple one, just select id
public id amount created at from payments
where id equals 2, look how much
Easy, we filter using ID.
because it's fast and efficient
It is fast and efficient, isn't it?
Look, we've actually managed to get the data here in this manner
Here you can see
See result, return to the database
So we got the result in this way
Now why is using ID better
It is being internally reported
Big serial zip takes 8 bytes
smaller, less storage
Meaning better indexing
It is clearly visible here, very clearly to you
Joins, filters, and lookups are much faster
Perfect for dashboards, reports
admin, panel, and internal operation
What is showing up in the take away?
inside the organization
If you are not part of any organization
as in dashboard, reports, admin
panel, queries, always use id
Big serial, it's fast
compact and index-friendly
Alright, now it's here, right here.
public receive link
Here is your UUID version 7
Here we can see, there is a student.
A link to his email
is being received here, there's a public
The API is available; it needs to be used.
so we use at that time
At that time, we will use it.
UUID version 7 instead of
Internal ID, why am I telling you
As a student receives an email here
Look, the mail has arrived.
Payment was successful
and you can download your
receipt or invoice using
this particular link, the link in question
we send the link with
public id in this way
not with ID because
Here we can see very clearly.
If this link is for
In exchange, if we have integer
What would have happened if it had been used?
payment slash 1 occurs at the start
What else happens, as I told you?
2 would be, what would the third be 3
In this way, an employee or college student
Here you go, anything certificate
Anything like that would just mess things up, wouldn't it?
You understand, that's why this way
to the public
Where you're sharing with outside people
Whenever the topic of external comes up
comes when you have external data
If you're working with him, then there you go, this
have to use, don't you, then
Second, when the student clicks in, the same thing is coming up.
that if you click on it, he'll get
Receipt in this manner of payment receipt
ID is being received, and data is here
If done this way, then look here at what is being displayed
if we used payment
forward slash 2, anyone could
try payment / 3
4, 5, and guess
So this is what your enumeration says.
Now the second example
Me, see, response from public API
Yeah, like our API
also returns public ID, in this manner
rather than internal ID, sometimes
If anyone needs this as an API or like an API
If you want to use it like this, then API
forward slash, payment forward slash, look at this
No, unique identifier versus 7
It will give you data of this kind.
Which one of these is your time-based starting point?
And the rest, your random guess
So, success, truth, data, payment ID is this
So it's safe now, right, here?
If it were serial, payment ID 1 or 2
That's how it works, so what is written here
Why do we want to use this?
Hard to guess and enumerate
safe to share in links and APIs
does not expose
How important this has become
And here it is.
The tech wave, look at you
When and what to do if you have a big serial
This is for internal operation
big serial you internal operation
Do not do it for your own company inside
Inside the organization where you have 10 members
So it was made just for them.
There, if you are with the world
Share it, then go ahead
Why UUID version 7 because
It is safe and non-enumerable.
Basically, we cannot guess there.
And there's also a tip provided here by them
Look, always think internally
World ID Fast
public underscore ID for the external world
I mean, his UUID is like this way
And guys, this is the best practice.
and tell me quickly once more, please
No, it's fine. Just look here from the goat.
The thing about serials is that it's yours
32-bit is big serial
Your 64-bit one is this
UUID 128 even this one
Version 7 also has 128-bit
It basically takes up space.
and example if you look, you'll see something
This will give you some idea of it in this manner
Generated ID whenever
It generates when you insert data
You will see 12345 in your ID.
It will come through this method, even here too, it will be the same.
Generated ID but here
You'll see it's just this
Here it is now
Clearly Nonne has stated that
When should we do what and why?
Look, this serial here, this one
32 beats, that's basically your 4
It will become 4 × 32 bytes
Understood. 8 bits equal 1 byte in this manner.
Not even 4 bytes of it
The loss will surely become insignificant.
Small application
Big Gash Bites
In the Laj app
Yud Pange's Bites
For the StriBUT Rate
But it's not Squinch Beach.
But this is version 7, Tam Order
If you're working for Extenal, go with this part
You both can use it together at a table.
Alright
And look here, so comfortably
Learning Projects, Serial, Large-Scale System, Big Serial
Distributed and Modern Application
As explained here, this is how it works with Wi-Fi
And lo, there was a puddle.
Patt was there
But you must have found out by now
Straight ahead, we walk here.
If you need to create a table
So how will you do it?
I'm checking the copy paste.
Because there's nothing big about it.
We have already seen the alderedī.
Create table exams Now we are creating the table for exams
id
serial and then primary
Look, I didn't write auto increment
No, I did not write NOT NULL and UNIQUE
Because NOT NULL and UNIQUE will make it
This is yours
total marks int not null
If you run this, you will get a table
Ready, and now here
You can easily select
Start from your what exams
If you do this
So you have to do it this way
It will show up, okay
And he looked just like a cow.
It has appeared right here
And Deva, we don't have any data
If you don't insert it, you won't see it now
Seen it, okay, so a sample exam
Pimbersl Unique Identifier
He's here, you're here
Imagining, it says, that now you have a course
To certify
So now you're very tired
You can see this student ID
Int Reference Users this reference
Now, instead of using this in your default
And in exchange for it, you take this turique
You can also do this with V7
You can also go from this Turkic place.
And from my Asabse, this will be even better.
Go there only if version
Here is yours.
Universally Unique Identifier
Look at this
You are looking.
What will happen to my bat if you don't give it back?
So, what we've read after such a long time
I have read it specifically for this purpose.
If it were a serial, Van
What do you feel?
27-inch monitor
I just took it, and it's very
The points look too small or is it okay?
What suggestion do you want to give?
I'm speaking too quickly or over-speaking?
东风 17 (DF-17) is a hypersonic glide vehicle (HGV) developed by China, capable of carrying nuclear or conventional warheads. It is designed to fly at extremely high speeds and maneuver during re-entry, making it difficult to intercept by existing missile defense systems. The DF-17 is part of China's strategic missile arsenal and represents a significant advancement in hypersonic technology. It is believed to be operational and deployed with the People's Liberation Army Rocket Force.
Liking the video
Don't forget to like it at all.
My like target is 500 likes
If you haven't subscribed to the channel yet, please subscribe now
This all costs 8 likes.
When you press it, it will tell you about the button.
Already seen, already taken