What is Constraints?

Constraints in SQL are rules that define what data can be stored in a table.

Stakeholder Map Team Whiteboard in Green Yellow Purple Trendy Stickers Style (7).png

  1. NOT NULL: It makes sure a column always has a value; it can’t be left empty.
CREATE TABLE customers (
    customer_id INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
);
  1. CHECK: It makes sure the values in a column meet a specific condition, like only allowing positive numbers.
CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    age INT,
    CHECK (age >= 18)
);
  1. DEFAULT: Sets a default value for a column if no value is provided during insertion.
CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    order_date DATE DEFAULT CURRENT_DATE
);
  1. UNIQUE
  2. PRIMARY KEY
  3. FOREIGN KEY

To know about these go to the next page → Primary key, Foreign key & Unique key