Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this 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:
Unique Keys
Primary Keys
Foreign Keys
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
[MUSIC]
0:00
Remember that relational
databases are called
0:04
relational because the tables
are connected by common attributes.
0:07
The attributes are the columns with common
data that link the rows of data together.
0:11
These special columns are called keys.
0:17
Database keys aren't only used for
relating data together.
0:19
Some keys guarantee that each row
is distinct from one another.
0:23
Other keys guarantee a certain value
doesn't repeat within a particular column.
0:27
We'll look at the three most
common database case keys.
0:33
Unique keys, primary keys and
foreign keys.
0:36
The first key we're going to
take a look at is a unique key.
0:40
This is very much what it sounds like.
0:43
It's a column specifically configured so
that no value can be repeated within it.
0:45
Let's see it in action.
0:51
Let's use a simple table to
demonstrate a unique key.
0:53
This is a person table with an id,
name and ssn, or Social Security number.
0:56
A number of rows
are inserted into the table.
1:02
By accident, someone entered in
the same Social Security number for
1:05
Dorian as Steve's.
1:09
Obviously, this is not good.
1:11
This Social Security number
column is not a unique key.
1:13
Let's start over again
with a person table,
1:18
where the Social Security
number column is a unique key.
1:20
This instructs the database
to enforce uniqueness
1:24
among all social security number values.
1:27
Let's see what happens when
we add the same data again.
1:30
Dorian's row is rejected.
1:34
And this is exactly what we wanted.
1:36
There's a few other things you
should know about unique keys.
1:39
If the schema permits, they can be null.
1:43
There can be multiple
unique keys per table.
1:46
For example, a unique email address column
1:49
could also be in the same table as
a unique Social Security number column.
1:53
Also, values in unique columns
can be changed to new values
1:58
as long as they don't conflict with
any existing values in that column.
2:02
You need to sign up for Treehouse in order to download course files.
Sign up