Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    SQLSQL

    First questions to the data

    • What are SQL and databases?
    • SELECT: choosing columns
    • WHERE: filtering rows
    • ORDER BY and LIMIT: sort and take the top

    Summarizing and grouping

    • Aggregates: COUNT, SUM, AVG
    • GROUP BY and HAVING
    • CASE, COALESCE and text functions

    Joining tables

    • Keys and relationships
    • INNER JOIN
    • LEFT JOIN: leave nobody out
    • Three-table joins and self joins
    • Subqueries and CTEs

    Building tables and changing data

    • CREATE TABLE and constraints
    • INSERT, UPDATE and DELETE
    • Transactions: all or nothing
    • SQL injection: the most dangerous mistake

    Professional level

    • Indexes: why is my query slow?
    • Window functions: rankings and totals without losing rows
    • Designing tables: normalization
    • Project: analytics for a learning platform
    SQL

    Building tables and changing data · Lesson 13 of 20

    CREATE TABLE and constraints

    Types, NOT NULL, UNIQUE, CHECK and DEFAULT: a database that protects itself.

    10 minutes2 games

    CREATE TABLE defines a table's shape: each column's name, its type, and the constraints that stop bad data from getting in at all. They matter more than any validation in your code, because they apply no matter who writes to the table.

    SQL
    CREATE TABLE badges (
      id INTEGER PRIMARY KEY,
      code TEXT NOT NULL UNIQUE,
      title TEXT NOT NULL,
      points INTEGER NOT NULL DEFAULT 10 CHECK (points > 0),
      created_at TEXT DEFAULT CURRENT_DATE
    );
    
    INSERT INTO badges (code, title) VALUES ('first-run', 'First program');
    INSERT INTO badges (code, title, points) VALUES ('streak-7', 'Seven-day streak', 50);
    
    SELECT id, code, title, points FROM badges;
    ConstraintWhat it prevents

    PRIMARY KEY

    a repeated or missing identifier

    NOT NULL

    an empty value

    UNIQUE

    two equal values in the column

    CHECK (points > 0)

    any value failing the condition

    DEFAULT 10

    prevents nothing; fills in a value when you omit one

    REFERENCES t(id)

    a reference to a missing row

    Try breaking the constraints. Each of these is rejected with a message saying why:

    SQL
    CREATE TABLE badges (
      id INTEGER PRIMARY KEY,
      code TEXT NOT NULL UNIQUE,
      points INTEGER CHECK (points > 0)
    );
    INSERT INTO badges (code, points) VALUES ('first-run', 10);
    
    -- This one fails on purpose: try each line on its own too
    INSERT INTO badges (code, points) VALUES ('first-run', 20);
    -- INSERT INTO badges (code, points) VALUES ('streak', -5);
    -- INSERT INTO badges (points) VALUES (5);

    Important

    SQLite is very relaxed about types: text can go into an INTEGER column without complaint (unless the table is STRICT). PostgreSQL is strict: a wrong type is an error. In PostgreSQL use precise types: BIGINT GENERATED ALWAYS AS IDENTITY for ids, TIMESTAMPTZ for times, BOOLEAN, and NUMERIC(10,2) for money (never store money in REAL/FLOAT).

    Complete the code

    The email is required. Complete it so it also can't repeat between users.

    CREATE TABLE users (
      id INTEGER PRIMARY KEY,
      email TEXT NOT NULL 
    );

    Interview question

    What's the difference between PRIMARY KEY and UNIQUE?

    Exercise

    Create a tasks table with: id (primary key), title text that can't be empty, and done an integer defaulting to 0. Add two tasks, 'Read' and 'Practice' (without giving done), then show the whole table.

    Expected result:
    id | title | done
    1 | Read | 0
    2 | Practice | 0
    SQL
    CREATE TABLE tasks (
      
    );
    

    Learn more from

    • PostgreSQL docs: Constraintspostgresql.org
    • SQLite: CREATE TABLEsqlite.org
    • SQLite: Datatypes in SQLitesqlite.org
    • PostgreSQL docs: Data typespostgresql.org

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousSubqueries and CTEsNextINSERT, UPDATE and DELETE
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.