liveBook · Manning

Schema

As explained in Build Python Web Apps with Streamlit

A database schema is the formal definition of a database's tables, fields, relationships, and constraints.

A database schema is the formal shape of persistent data: its tables, columns, data types, relationships, and constraints. Unlike an in-memory dataframe, a database keeps this structure and its records after the program stops. section 8.2.1

Design a schema by moving from app requirements to database structure. Identify the important entities, define how they relate, list each entity’s attributes, and convert the result into tables. section 8.2.2

For a small app, make each design decision explicit:

  • Identify entities from the app’s core nouns, such as users and haikus. section 8.2.2
  • Describe relationships and cardinality; one user can write many haikus, while each haiku has one author. section 8.2.2
  • List attributes such as username, password, haiku text, author, numeric ID, and creation time. section 8.2.2
  • Add constraints that protect the model: a primary key uniquely identifies a row, while a foreign key connects related tables. section 8.2.4

A users-and-haikus schema

CREATE TABLE users (username VARCHAR(100) PRIMARY KEY, password_hash VARCHAR(128)); CREATE TABLE haikus (haiku_id SERIAL PRIMARY KEY, created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, author VARCHAR(100), text TEXT, FOREIGN KEY (author) REFERENCES users(username)); The users table gives each user a unique username. The haikus table gives each entry an ID and creation time, then links its author to an existing user. section 8.2.4

Figure 8.4 Database schema with a foreign key relationship between the users and haikus tables

Use the database schema diagram to see the users and haikus tables and their foreign-key relationship before writing SQL. section 8.2.2

A schema supports the CRUD cycle: creating, reading, updating, and deleting records. SQL implements these operations, while commands such as \dt help verify that tables exist. In production, keep authentication data as password hashes rather than plain-text passwords. section 8.7 section 8.2.4

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains modeling steps and PostgreSQL constraints, but stops short of comprehensive production schema design.
Examples
Models users and haikus with PostgreSQL tables, sample records, queries, updates, and deletions.
What it assumes
Assumes basic application data needs while teaching entities, attributes, relationships, tables, and SQL fundamentals.
What you can do after
Build a small relational schema and operate it with PostgreSQL SQL commands.
How it connects
Connects relational databases, SQL, entity-relationship diagrams, primary keys, foreign keys, and CRUD.

Also explained in

Compare how 44 books explain schema →

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.