zaro

How Does CREATE TABLE Work?

Published in SQL Table Creation 4 mins read

The CREATE TABLE statement in SQL is the fundamental command used to construct a new table within a relational database. It's the blueprint for how your data will be organized and stored.

Understanding the Basics

At its core, the CREATE TABLE statement defines the structure of a table. The SQL CREATE TABLE statement is used to define and create a new table within a relational database. This means you specify:

  1. Table Name: A unique name for the table.
  2. Columns: The individual vertical data fields.
  3. Data Types: What kind of data each column will hold (e.g., text, numbers, dates).
  4. Constraints: Rules that enforce data integrity and relationships between tables.

Tables are crucial because they serve as the containers for structured data, organized into rows (records) and columns (fields).

The Structure of the Statement

The basic syntax looks something like this:

CREATE TABLE table_name (
    column1_name data_type constraint(s),
    column2_name data_type constraint(s),
    ...
    columnN_name data_type constraint(s)
);

Let's break down the key components:

  • CREATE TABLE: The command itself.
  • table_name: The name you assign to the new table. It should be descriptive and follow database naming conventions.
  • ( ... ): Parentheses enclose the list of column definitions.
  • column_name: The name for each column.
  • data_type: Specifies the type of data that can be stored in the column (e.g., INT, VARCHAR, DATE, BOOLEAN).
  • constraint(s): Optional rules applied to the column or the table (e.g., NOT NULL, PRIMARY KEY, UNIQUE, DEFAULT).

Specifying Columns and Data Types

Defining columns is the first step after naming the table. For each column, you must provide a name and its data type.

Common Data Types

Choosing the correct data type is vital for performance and data integrity. Here are a few examples:

  • Numeric:
    • INT (Integer)
    • DECIMAL(p, s) (Fixed-point number)
    • FLOAT (Floating-point number)
  • String:
    • VARCHAR(n) (Variable-length string, max length n)
    • CHAR(n) (Fixed-length string, length n)
    • TEXT (Large text block)
  • Date and Time:
    • DATE (Date value)
    • TIME (Time value)
    • DATETIME or TIMESTAMP (Date and time value)
  • Boolean:
    • BOOLEAN or BOOL (True/False)

Adding Constraints

Constraints are rules that are enforced on data columns in a table to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data.

Key Constraints Explained

Constraint Description
PRIMARY KEY Uniquely identifies each record in a table. Must contain unique, non-NULL values.
FOREIGN KEY Links two tables together; enforced relationship between columns in different tables.
UNIQUE Ensures all values in a column are different. Can contain NULL values.
NOT NULL Ensures that a column cannot have a NULL value.
DEFAULT Provides a default value for a column when none is specified during insertion.
CHECK Ensures that all values in a column satisfy a specific condition.
  • A table typically has one PRIMARY KEY.
  • FOREIGN KEY constraints are essential for defining relationships between tables (e.g., linking an Orders table to a Customers table via a Customer ID).

Example: Creating a Simple 'Customers' Table

Here's a practical example demonstrating the CREATE TABLE statement with various elements:

CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY, -- Unique identifier for each customer, cannot be NULL
    FirstName VARCHAR(50) NOT NULL, -- Customer's first name, required
    LastName VARCHAR(50) NOT NULL, -- Customer's last name, required
    Email VARCHAR(100) UNIQUE, -- Customer's email, must be unique (can be NULL if optional)
    RegistrationDate DATE DEFAULT CURRENT_DATE, -- Defaults to today's date if not provided
    LoyaltyPoints INT CHECK (LoyaltyPoints >= 0) -- Loyalty points, must be non-negative
);

In this example:

  • A table named Customers is created.
  • It has columns for CustomerID, FirstName, LastName, Email, RegistrationDate, and LoyaltyPoints.
  • Constraints like PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, and CHECK are applied to specific columns to enforce data rules.

By executing this SQL statement, the database system reserves space and defines the structure for the new Customers table, making it ready to accept data inserts. The constraints ensure that data entered into the table adheres to the defined rules, maintaining data integrity.

This statement specifies the table's structure, including column names, data types, constraints, and more, exactly as described in the reference.