SQLancer
LESSON 16

SQL (Core) - Creating tables

Progress: 0%

SQL Lesson 16: Creating tables

Before inserting data, you must establish relational table schemas using the CREATE TABLE Data Definition Language (DDL) query.

1. CREATE TABLE Syntax & Constraints

Every column requires a name, data type (INTEGER, TEXT, REAL, BOOLEAN, DATETIME), and optional schema enforcement constraints:

CREATE TABLE IF NOT EXISTS database_courses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE,
category TEXT DEFAULT 'General',
rating REAL CHECK (rating >= 0 AND rating <= 5),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

2. Schema Constraint Breakdown

  • PRIMARY KEY: Guarantees row uniqueness and speeds up table lookups.
  • NOT NULL: Rejects insertions missing this required attribute.
  • UNIQUE: Prevents duplicate emails, usernames, or titles.
  • DEFAULT: Automatically populates timestamps or fallbacks if omitted.
  • CHECK: Validates value boundaries before writing to disk.

🗄️ Schema Explorer SELECTABLE

movies
|

🎯 Exercises Checklist:

1. Create a new table named Database with columns: Name (TEXT), Version (FLOAT), Download_count (INTEGER)
2. Create a new table named Users with columns: id (INTEGER PRIMARY KEY), username (TEXT NOT NULL), email (TEXT UNIQUE)
⚡ Practice Console

Data Output

No results. Run query to fetch data.

Current Task Target

5 rows