SQL: Creating Databases and Tables - UNIQUE Constraint

UNIQUE Constraint

The NOT NULL constraint enforces that a value in a column is unique.

Example: UNIQUE Constraint

-- Create Table with UNIQUE Constraints
CREATE TABLE people(
id serial PRIMARY KEY,
first_name VARCHAR(50),
email VARCHAR(100) UNIQUE
);
## data frame with 0 columns and 0 rows
-- INSERT
INSERT INTO people(id,first_name,email) VALUES (1,'Joe' ,'joe@joe.com');
## data frame with 0 columns and 0 rows
-- SELECT
SELECT * FROM people;
##   id first_name       email
## 1  1        Joe joe@joe.com
-- INSERT, results in an error
INSERT INTO people(id,first_name,email) VALUES (2,'Joseph' ,'joe@joe.com');
## NULL