Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Primary Keys are one of the most important aspects of good database design.
Primary Key Properties
- May never be null
- One primary key per table
- Cannot be modified to a new value
Most of what you will find about Primary Keys will pertain to most or all database platforms, but there may be some minor differences in how they are implemented and how they behave on disk.
There is a lot of information on the internet about Primary Keys. A quick search will turn up thousands of articles, examples, blog posts, forums and question/answer threads.
Here are a few we like, but feel free to continue the search:
The Wikipedia page for Primary Keys actually redirects to a larger page on Unique Keys.
-
0:00
A primary key guarantees data is unique within a column.
-
0:03
Just like unique key, but there are several impulse and differences.
-
0:08
Like unique keys the primary keys are unique values.
-
0:12
A unique key can allow aid no value.
-
0:15
However a primary key will never allow a no value,
-
0:19
each table can only have one primary key.
-
0:22
But can have many unique keys.
-
0:25
Lastly you cannot modify the value of a primary key.
-
0:28
But you can modify the value of a unique key
-
0:31
as long as it doesn't violate the uniqueness criteria of that column.
-
0:35
A primary key doesn't necessarily need to be a numeric or integer data type.
-
0:41
But in a lot of instances,
-
0:42
they are implemented as auto incrementing number fields.
-
0:46
Because they're primarily used for
-
0:48
relating tables of data within the database itself, primary keys don't
-
0:53
really have any useful values that have any meaning to business or application users.
-
0:59
The use of number values as opposed to text data reduces disk storage space and
-
1:05
speeds up query results.
-
1:07
In this example the ID field is a primary key.
-
1:11
Let's assume it isn't an auto incrementing field.
-
1:15
So we're on the hook for inserting that value.
-
1:18
If we make a mistake or
-
1:19
a duplicate value the database will not allow the record to be written.
-
1:24
If we accidentally try to write a null value the system would return
-
1:29
an error as well.
-
1:30
Only a non null numeric value can be written to this primary key field.
-
1:36
Remember that database systems can handle the generation of the ID field for us.
-
1:41
Normally it's already set up for us to do this by the database designer.
-
1:45
And then we don't have to worry about doing this ourselves.
-
1:49
In the next video, we'll see how a database creates and
-
1:53
enforces relationships between tables using primary keys and foreign keys.
You need to sign up for Treehouse in order to download course files.
Sign up