SQL CREATE TABLE Keyword

The CREATE TABLE command is fundamental in SQL. It's how you build new tables to store your data within a database.



Creating a New Table

Basic Table Creation

This example shows how to create a table with several columns, specifying their data types.

Syntax

CREATE TABLE Persons (
  PersonID INT,
  LastName VARCHAR(255),
  FirstName VARCHAR(255),
  Address VARCHAR(255),
  City VARCHAR(255)
);
      
Output

A new table named "Persons" is created. It contains columns for 'PersonID' (integer), 'LastName', 'FirstName', 'Address', and 'City' (all variable-length strings with a maximum of 255 characters).

Creating a Table from Another Table

Creating a Table as a Select Statement

You can also create a new table by selecting data from an existing table. This creates a copy of the selected columns.

Syntax

CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;
      
Output

A new table called "TestTable" is created. It contains only the 'customername' and 'contactname' columns, copying the data from the existing 'customers' table.