liveBook · Manning

CREATE TABLE statement

As explained in Build Python Web Apps with Streamlit

CREATE TABLE is a SQL statement that creates a table with specified columns, data types, and constraints.

Use CREATE TABLE when an application needs a new database table with a defined structure. The statement names the table and specifies each column’s data type and rules. section 8.2.4

CREATE TABLE users (
  username VARCHAR(100) PRIMARY KEY,
  password_hash VARCHAR(128)
);

This users table stores usernames and password hashes; VARCHAR limits text length, while PRIMARY KEY makes each username unique and required. section 8.2.4

Read each column specification as a compact design decision:

  • VARCHAR(100) accepts variable-length text up to 100 characters; longer values produce an error. section 8.2.4
  • PRIMARY KEY uniquely identifies rows and prevents duplicate or missing usernames. section 8.2.4

Check the result

Run the CREATE TABLE command in the PostgreSQL psql shell, then use \dt to list tables and confirm that users was created.

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines columns, data types, primary keys, foreign keys, defaults, and length limits, then verifies creation with \dt.
Examples
Uses users and haikus tables, VARCHAR fields, generated identifiers, timestamps, sample rows, and psql table listings.
What it assumes
Assumes access to a PostgreSQL database and a planned schema containing the attributes an application needs.
What you can do after
Operate PostgreSQL through psql to create tables and verify that the resulting relations exist.
How it connects
Connects SQL with tables, columns, data types, primary keys, foreign keys, defaults, and database schemas.

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 Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.