CREATE TABLE Statement in SQL

11/22/2025
All Articles

SQL CREATE TABLE tutorial with examples for beginners

CREATE TABLE Statement in SQL

CREATE TABLE Statement in SQL – SQL Tutorial for Beginners

The CREATE TABLE statement in SQL is used to create a new table in a database. It defines the table structure, including column names, data types, constraints, and default values.

In this beginner-friendly SQL tutorial, you will learn:

  • What CREATE TABLE does

  • Syntax of CREATE TABLE

  • Common data types

  • Adding constraints

  • Creating tables with primary & foreign keys

  • Real-world examples

  • Best practices


๐Ÿ”น What Is CREATE TABLE?

CREATE TABLE is an SQL command used to create a new table in a database.

Every table contains:

  • Columns

  • Data types

  • Optional constraints (PRIMARY KEY, UNIQUE, NOT NULL, etc.)


๐Ÿ”ธ Basic Syntax

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

๐Ÿ”น Common SQL Data Types

Type Description
INT Whole numbers
VARCHAR(n) Text up to n characters
DATE Stores date
DATETIME Date + time
BOOLEAN True/false values
DECIMAL(a, b) Fixed decimal value

๐Ÿ”ธ Example 1: Create a Simple Table

CREATE TABLE users (
    id INT,
    name VARCHAR(100),
    email VARCHAR(150),
    city VARCHAR(100)
);

๐Ÿ”ธ Example 2: Create Table with PRIMARY KEY

CREATE TABLE customers (
    customer_id INT PRIMARY KEY,
    name VARCHAR(100),
    phone VARCHAR(15)
);

๐Ÿ”ธ Example 3: Create Table with AUTO_INCREMENT (MySQL)

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    product_name VARCHAR(200),
    price DECIMAL(10,2)
);

๐Ÿ”ธ Example 4: Create Table with NOT NULL and UNIQUE

CREATE TABLE employees (
    emp_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(150) UNIQUE,
    salary INT
);

๐Ÿ”ธ Example 5: Create Table with FOREIGN KEY

Link orders to users.

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    user_id INT,
    product VARCHAR(100),
    amount DECIMAL(10,2),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

๐Ÿ”ธ Example 6: Create Table with Default Values

CREATE TABLE accounts (
    id INT PRIMARY KEY,
    username VARCHAR(100),
    status VARCHAR(20) DEFAULT 'active'
);

๐Ÿ”ธ Example 7: Create Table with CHECK Constraint

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    age INT CHECK (age >= 18)
);

๐Ÿ”น Real-World Use Cases

โœ” User registration system

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(150) UNIQUE,
    password VARCHAR(255)
);

โœ” Product inventory system

CREATE TABLE inventory (
    item_id INT PRIMARY KEY,
    item_name VARCHAR(150),
    stock INT,
    price DECIMAL(10,2)
);

โœ” E-commerce order management

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    total_amount DECIMAL(12,2)
);

๐Ÿ”น Best Practices for CREATE TABLE

โœ” Always define a PRIMARY KEY
โœ” Use appropriate data types (avoid oversized VARCHAR)
โœ” Use NOT NULL when possible
โœ” Use FOREIGN KEY for relational data
โœ” Add DEFAULT values where needed
โœ” Use CHECK constraints for validation


Summary

In this SQL CREATE TABLE tutorial, you learned:

  • How to create tables with columns and data types

  • How to use constraints (PRIMARY KEY, UNIQUE, NOT NULL)

  • How to add relations using FOREIGN KEY

  • Real-world examples and best practices

The CREATE TABLE statement is the foundation of building structured databases.

Article