Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire 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.
A primary key guarantees data is unique within a column.
Just like unique key, but there are several impulse and differences.
Like unique keys the primary keys are unique values.
A unique key can allow aid no value.
However a primary key will never allow a no value,
each table can only have one primary key.
But can have many unique keys.
Lastly you cannot modify the value of a primary key.
But you can modify the value of a unique key
as long as it doesn't violate the uniqueness criteria of that column.
A primary key doesn't necessarily need to be a numeric or integer data type.
But in a lot of instances,
they are implemented as auto incrementing number fields.
Because they're primarily used for
relating tables of data within the database itself, primary keys don't
really have any useful values that have any meaning to business or application users.
The use of number values as opposed to text data reduces disk storage space and
speeds up query results.
In this example the ID field is a primary key.
Let's assume it isn't an auto incrementing field.
So we're on the hook for inserting that value.
If we make a mistake or
a duplicate value the database will not allow the record to be written.
If we accidentally try to write a null value the system would return
an error as well.
Only a non null numeric value can be written to this primary key field.
Remember that database systems can handle the generation of the ID field for us.
Normally it's already set up for us to do this by the database designer.
And then we don't have to worry about doing this ourselves.
In the next video, we'll see how a database creates and
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