SQL CREATE TABLE Statement

The SQL CREATE TABLE statement is used to create a new table in a database. It allows you to define the structure of the table, including the names and data types of the columns. CREATE TABLE is a powerful operation and is an important part of any SQL query. It is commonly used to create a new table from scratch or to modify an existing table.

Syntax

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

Example

CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  email VARCHAR(255),
  city VARCHAR(255)
);
    

This example creates a new table called "customers" with four columns: id, name, email, and city. The id column is defined as an INT data type and is set as the primary key, which means it is unique and cannot be null. The other columns are defined as VARCHAR data types with a maximum length of 255 characters.

SQL Data Types

There are several different data types that you can use in the SQL CREATE TABLE statement to define the structure of a table. Here is a list of some common data types:

Here is an example of a table that uses several different data types:

CREATE TABLE products (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  price DECIMAL,
  image BLOB,
  active BOOLEAN,
  last_updated DATE
);
    

This example creates a table called "products" with six columns: id, name, price, image, active, and last_updated. The id column is an INT data type and is set as the primary key. The name column is a VARCHAR data type, the price column is a DECIMAL data type, the image column is a BLOB data type, the active column is a BOOLEAN data type, and the last_updated column is a DATE data type.

You can learn more about data types and other advanced topics in our SQL tutorials and references.