SQL Create Table

The CREATE TABLE statement is used in SQL to define a new table and its columns. When creating a table, you must specify the column names, data types, and optionally, constraints like primary keys or default values.

Tutorials dojo strip

SQL Create Table Syntax

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

SQL Create Table Using Another Table

Sometimes, you might want to create a new table based on an existing one, either copying just its structure or both its structure and data.

This query creates a new table with the same structure as an existing one, but without copying any data.

CREATE TABLE new_table
AS
SELECT * FROM existing_table
WHERE 1 = 0;

This query creates a new table and also copies all the data from the existing one. This approach is useful for backups, testing, or creating archive tables.

CREATE TABLE new_table
AS
SELECT * FROM existing_table;

SQL Create Table Example

This command creates a table named Students with several useful fields.

CREATE TABLE Drivers (
    DriversID INT PRIMARY KEY,
    FirstName VARCHAR(50) NOT NULL,
    LastName VARCHAR(50),
    BirthDate DATE,
    RegistrationYear INT DEFAULT 2023
);

SQL Create Table Labs

Tutorials dojo strip
Scroll to Top