CodeBlanks Logo Icon CodeBlanks

Summary - Constraints

Combine UNIQUE, CHECK and FOREIGN KEY to design a table.

Summary - Constraints

In this group you learned PRIMARY KEY, UNIQUE, CHECK and FOREIGN KEY constraints. They are the basis of data integrity.

CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT,
  total DECIMAL(10,2) CHECK (total >= 0),
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

A complete table with multiple constraints.

The table orders is created.

Practice Summary - Constraints

Create products with id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, price DECIMAL(10,2) CHECK (price > 0).

Starter code

query.sql
-- summary table
CREATE TABLE products

Sample solution

query.sql
CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  price DECIMAL(10,2) CHECK (price > 0)
)

Expected output

Tablo oluşturuldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which ensures a relation between two tables?

  • PRIMARY KEY
  • CHECK
  • FOREIGN KEY Correct answer
  • UNIQUE

Explanation: FOREIGN KEY ensures a relation between tables.

2 Summary - Constraints: match the terms

Term Meaning
PRIMARY KEY primary
UNIQUE unique
CHECK check
FOREIGN KEY relation

All lessons in Beginner