liveBook · Manning

CREATE TABLE statement

As explained in Learn SQL in a Month of Lunches

A CREATE TABLE statement is an SQL statement that creates a new database table with specified columns and constraints.

Use CREATE TABLE when you need a new table for organized data, such as product categories. State the table name, then list each column with its data type inside parentheses. section 18.1.2

CREATE TABLE category (
    CategoryID int,
    CategoryName varchar(20)
    );

This example creates category with an integer identifier and a variable-length category name. section 18.1.2

Commas separate column definitions, just as they separate items in other SQL clauses. The parentheses are required: leaving them out is a common beginner mistake. A successful CREATE TABLE command returns no result rows; instead, the output reports that zero rows were affected. section 18.1.2

Protecting unique rows

A table representing unique entities such as categories, products, orders, or customers can define a named primary key while it is created: CREATE TABLE category ( CategoryID int, CategoryName varchar(20), CONSTRAINT PK_category PRIMARY KEY (CategoryID) ); A multi-column key is also possible, as with an orderitem table using OrderID and ItemID together. section 18.3.2

CREATE TABLE is a starting point rather than a complete table design. Additional properties can be added later, and the next practical step after creating a table is inserting data. section 18.1.2

Reading a table script critically

A payment table might define ID and Author as int, PaymentYear as char(4), and PaymentAmount as decimal(7,2). Reviewing such a script means checking whether its columns, data types, and constraints fit the intended data. section 23.1.1

How Learn SQL in a Month of Lunches covers it

How deep it goes
Explains names, data types, commas, parentheses, and primary keys, while deferring additional table properties.
Examples
Uses category, authorpayment, and orderitem examples to demonstrate basic definitions, script review, and composite keys.
What it assumes
Assumes familiarity with table names, column names, data types, and basic SQL comma-separated lists.
What you can do after
Builds a table from scratch and defines a named primary key for uniquely identifiable rows.
How it connects
Connects table creation with columns, data types, INSERT, primary keys, foreign keys, and table relationships.

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 Learn SQL in a Month of Lunches. The section links point to the parts of the book it draws on.