Bird
Raised Fist0
PostgreSQLquery~5 mins

Column constraints (NOT NULL, UNIQUE, CHECK) in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the NOT NULL constraint do in a database column?
The NOT NULL constraint ensures that a column cannot have a NULL value. Every row must have a value for this column.
Click to reveal answer
beginner
Explain the purpose of the UNIQUE constraint in a table column.
The UNIQUE constraint makes sure that all values in a column are different from each other. No two rows can have the same value in that column.
Click to reveal answer
intermediate
What is a CHECK constraint and how is it used?
A CHECK constraint limits the values that can be stored in a column by specifying a condition. Only values that meet the condition are allowed.
Click to reveal answer
intermediate
Can a column have both NOT NULL and UNIQUE constraints? What does that mean?
Yes, a column can have both. It means every row must have a value (not null) and all values must be different (unique).
Click to reveal answer
beginner
Write a simple SQL example adding a CHECK constraint to ensure age is 18 or older.
Example:
CREATE TABLE users (id SERIAL PRIMARY KEY, age INT CHECK (age >= 18));
This means age must be 18 or more.
Click to reveal answer
What happens if you try to insert a NULL value into a column with a NOT NULL constraint?
AThe insert fails with an error.
BThe NULL value is accepted.
CThe NULL is converted to zero.
DThe row is ignored.
Which constraint ensures all values in a column are different?
APRIMARY KEY
BCHECK
CNOT NULL
DUNIQUE
What does this SQL do? CHECK (salary > 0)
AAllows salary to be zero or negative.
BAllows only positive salary values.
CMakes salary optional.
DNo effect on salary.
Can a UNIQUE column contain NULL values in PostgreSQL?
AYes, but only one NULL is allowed.
BNo, NULLs are not allowed.
CYes, multiple NULLs are allowed.
DUNIQUE constraint ignores NULLs.
Which constraint would you use to prevent empty values in a column?
ANOT NULL
BFOREIGN KEY
CCHECK
DUNIQUE
Describe the differences and purposes of NOT NULL, UNIQUE, and CHECK constraints in a database column.
Think about what each constraint controls about the data.
You got /3 concepts.
    Write an example SQL statement to create a table with columns using NOT NULL, UNIQUE, and CHECK constraints.
    Use simple column names and conditions like age >= 18.
    You got /4 concepts.

      Practice

      (1/5)
      1. Which column constraint in PostgreSQL ensures that a column cannot have empty or missing values?
      easy
      A. PRIMARY KEY
      B. NOT NULL
      C. CHECK
      D. UNIQUE

      Solution

      1. Step 1: Understand the NOT NULL constraint

        NOT NULL means the column must always have a value; it cannot be left empty.
      2. Step 2: Compare with other constraints

        UNIQUE prevents duplicates but allows NULLs; CHECK enforces custom rules; PRIMARY KEY implies NOT NULL and UNIQUE but is more specific.
      3. Final Answer:

        NOT NULL -> Option B
      4. Quick Check:

        NOT NULL = no empty values [OK]
      Hint: NOT NULL means no empty values allowed in the column [OK]
      Common Mistakes:
      • Confusing UNIQUE with NOT NULL
      • Thinking CHECK prevents NULLs
      • Assuming PRIMARY KEY is the only way to disallow NULL
      2. Which of the following is the correct syntax to add a UNIQUE constraint to the column email in a table users?
      easy
      A. ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email);
      B. ALTER TABLE users ADD UNIQUE email;
      C. ALTER TABLE users ADD UNIQUE (email) CONSTRAINT unique_email;
      D. ALTER TABLE users ADD CONSTRAINT email UNIQUE;

      Solution

      1. Step 1: Recall the syntax for adding UNIQUE constraint

        The correct syntax is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);
      2. Step 2: Match the syntax with options

        ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email); matches the correct syntax exactly, naming the constraint and specifying the column.
      3. Final Answer:

        ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email); -> Option A
      4. Quick Check:

        ADD CONSTRAINT name UNIQUE (column) [OK]
      Hint: Use ADD CONSTRAINT name UNIQUE (column) to add UNIQUE [OK]
      Common Mistakes:
      • Omitting CONSTRAINT keyword
      • Placing UNIQUE before CONSTRAINT name incorrectly
      • Not specifying column in parentheses
      3. Given the table definition:
      CREATE TABLE products (
      id SERIAL PRIMARY KEY,
      price NUMERIC CHECK (price > 0),
      sku TEXT UNIQUE
      );

      What happens if you try to insert price = -5 and sku = 'A123'?
      medium
      A. Insert fails due to UNIQUE constraint violation on sku
      B. Insert succeeds because CHECK allows negative values
      C. Insert fails due to CHECK constraint violation on price
      D. Insert succeeds because price can be negative

      Solution

      1. Step 1: Analyze the CHECK constraint on price

        The CHECK constraint requires price > 0, so negative values like -5 violate this rule.
      2. Step 2: Consider the UNIQUE constraint on sku

        Since 'A123' is new, UNIQUE is not violated here.
      3. Final Answer:

        Insert fails due to CHECK constraint violation on price -> Option C
      4. Quick Check:

        CHECK (price > 0) rejects negative price [OK]
      Hint: CHECK enforces rules; negative price breaks price > 0 [OK]
      Common Mistakes:
      • Ignoring CHECK constraint effects
      • Confusing UNIQUE violation with CHECK violation
      • Assuming negative numbers are allowed
      4. You have this table:
      CREATE TABLE employees (
      id SERIAL PRIMARY KEY,
      email TEXT UNIQUE NOT NULL,
      age INT CHECK (age >= 18)
      );

      Which statement will cause an error when inserting data?
      medium
      A. INSERT INTO employees (email, age) VALUES ('john@example.com', 25);
      B. INSERT INTO employees (email, age) VALUES ('jane@example.com', 20);
      C. INSERT INTO employees (email, age) VALUES ('jane@example.com', 18);
      D. INSERT INTO employees (email, age) VALUES (NULL, 30);

      Solution

      1. Step 1: Check NOT NULL constraint on email

        Email cannot be NULL because of NOT NULL constraint.
      2. Step 2: Check CHECK constraint on age

        Age must be 18 or older; the ages in A(25), B(20), and D(18) all satisfy this.
      3. Final Answer:

        INSERT with NULL email causes error due to NOT NULL constraint -> Option D
      4. Quick Check:

        NOT NULL rejects NULL email [OK]
      Hint: NOT NULL disallows NULL values, CHECK enforces conditions [OK]
      Common Mistakes:
      • Focusing on CHECK constraint instead of NOT NULL
      • Ignoring NOT NULL on email
      • Assuming UNIQUE causes error on first insert
      5. You want to create a table orders where the quantity column must be a positive integer and the order_code must be unique and never empty. Which is the best column definition for quantity and order_code?
      hard
      A. quantity INT CHECK (quantity > 0), order_code TEXT UNIQUE NOT NULL
      B. quantity INT NOT NULL, order_code TEXT CHECK (order_code != '')
      C. quantity INT CHECK (quantity >= 0), order_code TEXT UNIQUE
      D. quantity INT, order_code TEXT NOT NULL UNIQUE CHECK (order_code IS NOT NULL)

      Solution

      1. Step 1: Ensure quantity is positive integer

        CHECK (quantity > 0) enforces positive values only, better than >= 0 which allows zero.
      2. Step 2: Ensure order_code is unique and not empty

        UNIQUE prevents duplicates; NOT NULL disallows empty; this combination ensures order_code is unique and present.
      3. Final Answer:

        quantity INT CHECK (quantity > 0), order_code TEXT UNIQUE NOT NULL -> Option A
      4. Quick Check:

        CHECK positive + UNIQUE NOT NULL = correct constraints [OK]
      Hint: Use CHECK for positive, UNIQUE + NOT NULL for unique non-empty [OK]
      Common Mistakes:
      • Allowing zero quantity
      • Missing NOT NULL on unique column
      • Using CHECK without NOT NULL for order_code
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard