T
Tech Town
Log InSign Up Free
← Back to Blog

June 17, 2025 · admin

CREATE TABLE Statement

๐Ÿงฑ SQL CREATE TABLE Statement โ€“ How to Create a New Table in SQL

The CREATE TABLE command in SQL is used to define a new table in your database, specifying its name, columns, and data types.

This is one of the most important SQL commands, forming the foundation for storing structured data in relational databases like MySQL, SQL Server, PostgreSQL, and Oracle.


๐Ÿ“˜ What is SQL CREATE TABLE?

The CREATE TABLE statement is used to create a new database table with specified columns, their data types, and optional constraints (like PRIMARY KEY, NOT NULL, etc.).

You can use it to design a schema that reflects your business logic.


๐Ÿงพ SQL CREATE TABLE Syntax

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

๐Ÿงฉ Common Data Types

Data TypeDescription
INTInteger (whole number)
VARCHAR(n)Variable-length string (e.g., VARCHAR(100))
DATEDate (YYYY-MM-DD)
DECIMAL(x,y)Decimal with x digits and y decimals
BOOLEANTrue or False

โœ… Example: Create a customers Table

CREATE TABLE customers (
  customer_id INT PRIMARY KEY,
  first_name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50) NOT NULL,
  email VARCHAR(100),
  signup_date DATE
);

This creates a table named customers with:

  • A customer_id as the primary key
  • First and last names (required)
  • An optional email
  • A signup date

๐Ÿง  Best Practices for Creating Tables

  • Always define a primary key
  • Use appropriate data types to save storage and ensure accuracy
  • Apply NOT NULL constraints to critical columns
  • Consider naming conventions for clarity and consistency
  • Plan for indexes and foreign keys (if joining other tables)

โš ๏ธ Common Errors to Avoid

MistakeTip to Fix
Forgetting data typesEvery column must have a defined type
Duplicate column namesEach column name must be unique
Invalid constraint placementUse proper syntax for PRIMARY KEY, UNIQUE, etc.
Missing commas between columnsEnsure every column line ends with a comma (except the last one)

๐Ÿš€ Creating a Table with Constraints

You can add constraints like:

  • PRIMARY KEY โ€“ Unique, not null identifier
  • UNIQUE โ€“ Prevent duplicate values
  • NOT NULL โ€“ Mandatory value
  • CHECK โ€“ Conditional validation
  • FOREIGN KEY โ€“ Relationship with another table
CREATE TABLE orders (
  order_id INT PRIMARY KEY,
  customer_id INT,
  order_date DATE NOT NULL,
  amount DECIMAL(10,2) CHECK (amount > 0),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

๐Ÿ“ Summary

  • The CREATE TABLE statement lets you define a new table in the database
  • Specify each column name, data type, and optional constraints
  • Use constraints (PRIMARY KEY, NOT NULL, etc.) to enforce data integrity
  • A well-structured table leads to scalable and efficient databases