liveBook · Manning

CREATE TABLE statement

As explained in Grokking Relational Database Design

CREATE TABLE is an SQL statement that defines a new relational table and its columns and constraints.

CREATE TABLE defines a new relational table. It gives the table a name, lists its columns, assigns data types, and adds rules such as NOT NULL and PRIMARY KEY. section 1.10

CREATE TABLE table_name (
 column1_name datatype [optional_parameters],
 column2_name datatype [optional_parameters],
 ...
 columnN_name datatype [optional_parameters],
 PRIMARY KEY (columnX_name)
);

Use this general pattern as a checklist: name the table, define each column, add optional parameters, and declare the primary key. section 1.10

A product table

A product table can contain product_id as INT, name and description as TEXT, price as DECIMAL(5, 2), and manufacturer as TEXT. PRIMARY KEY identifies the key column, while NOT NULL requires values for the other columns. section 1.10

When running a CREATE TABLE command:

  • Run it once for a given table name; running it again can produce a duplicate-table complaint. section 1.10
  • If a prepared script already created the table, reset the SQLite Online workspace or delete the existing table before retrying. section 1.10
  • Treat CREATE TABLE as table-management DDL, alongside ALTER TABLE and DROP TABLE. section 1.12

How Grokking Relational Database Design covers it

How deep it goes
Explains general syntax, column data types, constraints, and duplicate-table errors, but defers altering and deleting tables.
Examples
Uses a product table with five columns and prepared scripts containing sample product data.
What you can do after
Builds a product table from scratch and avoids rerunning its definition when the table already exists.
How it connects
Connects CREATE TABLE with relational databases, primary keys, SQL data types, DDL, INSERT, ALTER TABLE, and DROP TABLE.

Also explained in

Compare how 6 books explain CREATE TABLE statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Relational Database Design. The section links point to the parts of the book it draws on.