LogoInterview Master

SQL Learning

SQL CREATE TABLE Statement

What Is the SQL CREATE TABLE Statement?

The CREATE TABLE statement is used to define a new table in a database. It lets you specify column names, data types, and constraints like primary keys or NOT NULL requirements.

Basic Syntax

CREATE TABLE table_name (
  column1 datatype constraint,
  column2 datatype constraint,
  ...
);

Practical CREATE TABLE Example

Here's how you might define a simple Users table in the TokTuk dataset:

CREATE TABLE Users (
  user_id INTEGER PRIMARY KEY,
  username TEXT NOT NULL,
  email TEXT UNIQUE NOT NULL,
  created_at DATE DEFAULT CURRENT_DATE
);

Using Constraints

  • PRIMARY KEY – Uniquely identifies each row.
  • NOT NULL – Prevents null values in a column.
  • UNIQUE – Ensures all values in a column are distinct.
  • DEFAULT – Assigns a default value if none is provided.

More Examples

Videos Table

CREATE TABLE Videos (
  video_id INTEGER PRIMARY KEY,
  title TEXT NOT NULL,
  upload_date DATE,
  views INTEGER DEFAULT 0
);

Interactions Table

CREATE TABLE Interactions (
  interaction_id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL,
  video_id INTEGER NOT NULL,
  interaction_type TEXT,
  timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);

Pro Tips for CREATE TABLE

  • Use clear, descriptive column names.
  • Define appropriate data types for each column.
  • Always set a primary key — it's your table's unique fingerprint.
  • Use NOT NULL to prevent incomplete data.
  • Use DEFAULT values to avoid repetitive inserts.

Loading SQL editor...