Make your columns UNIQUE with ADD CONSTRAINT
As seen in the video, you add the UNIQUE
keyword after the column_name
that should be unique. This, of course, only works for new tables:
CREATE TABLE table_name (
column_name UNIQUE
);
If you want to add a unique constraint to an existing table, you do it like that:
ALTER TABLE table_name
ADD CONSTRAINT some_name UNIQUE(column_name);
Note that this is different from the ALTER COLUMN
syntax for the not-null constraint. Also, you have to give the constraint a name some_name
.
This exercise is part of the course
Introduction to Relational Databases in SQL
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
-- Make universities.university_shortname unique
ALTER ___ ___
ADD ___ ___ UNIQUE(___);