Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Unique Keys are specifically configured so that no value can be repeated within it.
There are 3 types of Database Keys we will be reviewing in this course:
For more info on Unique Keys, Wikipedia: Unique Keys is a good introduction.
This article from W3 Schools shows How unique keys are implemented in various SQL database platforms
Remember that relational databases are called
relational because the tables are connected by common attributes.
The attributes are the columns with common data that link the rows of data together.
These special columns are called keys.
Database keys aren't only used for relating data together.
Some keys guarantee that each row is distinct from one another.
Other keys guarantee a certain value doesn't repeat within a particular column.
We'll look at the three most common database case keys.
Unique keys, primary keys and foreign keys.
The first key we're going to take a look at is a unique key.
This is very much what it sounds like.
It's a column specifically configured so that no value can be repeated within it.
Let's see it in action.
Let's use a simple table to demonstrate a unique key.
This is a person table with an id, name and ssn, or Social Security number.
A number of rows are inserted into the table.
By accident, someone entered in the same Social Security number for
Dorian as Steve's.
Obviously, this is not good.
This Social Security number column is not a unique key.
Let's start over again with a person table,
where the Social Security number column is a unique key.
This instructs the database to enforce uniqueness
among all social security number values.
Let's see what happens when we add the same data again.
Dorian's row is rejected.
And this is exactly what we wanted.
There's a few other things you should know about unique keys.
If the schema permits, they can be null.
There can be multiple unique keys per table.
For example, a unique email address column
could also be in the same table as a unique Social Security number column.
Also, values in unique columns can be changed to new values
as long as they don't conflict with any existing values in that column.
You need to sign up for Treehouse in order to download course files.Sign up