SQL CREATE TABLE Statement — Syntax, Examples

Create Tables

The office needs a consistent place to store roll numbers, names and marks before it can save the first student.

CREATE TABLE defines a table’s columns and constraints. It establishes the structure for future rows; it does not insert student records by itself.

  1. Explain and apply

    Define a student table and explain how its columns and primary key describe valid records.

  2. Work it out

    Create a small mark table, insert one valid row and read it back.

  3. Avoid mistakes

    A default is not the same as a universal validation rule.

Create Tables: the request and its result

  1. School need

    The office needs a consistent place to store roll numbers, names and marks before it can save the first student.

  2. Database operation

    CREATE TABLE defines a table’s columns and constraints. It establishes the structure for future rows; it does not insert student records by itself.

  3. Check the outcome

    The definition creates an empty table with explicit rules. The separate INSERT provides the data that SELECT can then return.

A conceptual model. The query examples below show the actual syntax, input data and results.

Understand what the database does

Each column definition combines a name with a type and optional rules. A primary key identifies rows uniquely and disallows missing key values. NOT NULL requires a value; CHECK expresses a permitted condition. A default supplies a value when a column is omitted, subject to the engine’s rules.

Related concepts: Insert Rows and Data Constraints.

CREATE TABLE

Every database begins with a table. A table is where your data lives. In SQL, the CREATE TABLE statement helps you define this storage space — specifying what kind of data you’ll collect, and how it should behave. It's like designing a form before collecting responses.

Syntax of CREATE TABLE

SQL · portable concepts; preserved examples identify their engine

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

Let’s break it down:

  • table_name: Name of your table (e.g., students)
  • column: Name of each field (e.g., roll_no, name)
  • datatype: Defines the kind of value stored (e.g., INT, VARCHAR)
  • constraint: Optional rules (e.g., PRIMARY KEY, NOT NULL)

1. Creating a Basic Students Table

SQL · portable concepts; preserved examples identify their engine

Let’s design a table to store student details at a school in India.

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  class VARCHAR(10),
  age INT,
  city VARCHAR(30)
);

Explanation

Query discussed in this section:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  class VARCHAR(10),
  age INT,
  city VARCHAR(30)
);
  • roll_no uniquely identifies each student. Marked as PRIMARY KEY.
  • name cannot be empty. Hence, NOT NULL.
  • class, age, and city store remaining student information.

2. Creating a Table with Default Values

SQL · portable concepts; preserved examples identify their engine

We can predefine values using DEFAULT. Suppose most students belong to ‘Hyderabad:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  class VARCHAR(10),
  age INT,
  city VARCHAR(30) DEFAULT 'Hyderabad'
);

Best Practices for CREATE TABLE

Query discussed in this section:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  class VARCHAR(10),
  age INT,
  city VARCHAR(30) DEFAULT 'Hyderabad'
);
  • Always define a PRIMARY KEY to uniquely identify rows.
  • Use NOT NULL to prevent blank values in critical columns.
  • Use CHECK and DEFAULT constraints to control behavior.

Important differences and mistakes

A default is not the same as a universal validation rule. IF NOT EXISTS does not verify that an existing table has the intended schema. Avoid experimenting on a production table, and verify engine-specific type and constraint support.

Try it with the school data

Create a small mark table, insert one valid row and read it back.

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

Worked answer and reasoning

This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

SQL · create only these practice tables in a disposable database:

CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');

SQL · this practice variation runs with SQLite 3; the preserved examples retain their stated dialect

CREATE TABLE practice_marks (roll_no INTEGER PRIMARY KEY, marks INTEGER NOT NULL CHECK (marks BETWEEN 0 AND 100)); INSERT INTO practice_marks VALUES (1,85);
SELECT roll_no, marks FROM practice_marks;
Expected query result
roll_nomarks
185

The definition creates an empty table with explicit rules. The separate INSERT provides the data that SELECT can then return.

Check your understanding

What is present immediately after CREATE TABLE succeeds?
Choose one answer. You can try again.

Answer and explanation

The table structure, with no inserted rows yet.

The definition creates an empty table with explicit rules. The separate INSERT provides the data that SELECT can then return.

Use the idea independently

CREATE TABLE defines a table’s columns and constraints. It establishes the structure for future rows; it does not insert student records by itself.

The definition creates an empty table with explicit rules. The separate INSERT provides the data that SELECT can then return.

Continue your understanding with Insert Rows and Data Constraints.

Source for the engine-specific behavior: MySQL 8.4 reference documentation.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: SQL INSERT INTO Statement — Adding Data to SQL Tables →