Creating tables and data entry in Datasette (Datasette Advent Day 3)

Learn how to create, modify, and relate tables in Datasette using simple commands and the new label column feature.

Creating tables and data entry in Datasette (Datasette Advent Day 3)

Introduction

Datasette makes it easy to turn SQLite databases into interactive web APIs. In the third Advent Day video, Simon Willison demonstrates the workflow for creating tables, inserting and updating rows, altering schemas, and defining relationships—including the newly added label column that helps Datasette choose a human‑readable summary for related records.

Creating and Populating Tables

The video starts by launching Datasette with the convenient `uvx` helper:

```bash uvx --pre datasette sightings.db --create --root ```

  • `--pre` pulls the latest pre‑release version.
  • `--create` generates `sightings.db` if it doesn’t exist.
  • `--root` starts the server with root privileges, allowing schema changes.

Once the server is running, you can use the built‑in SQL editor or the API to create a table:

```sql CREATE TABLE sightings ( id INTEGER PRIMARY KEY, species TEXT NOT NULL, location TEXT, observed_at TEXT ); ``` After the table exists, inserting data is straightforward:

```sql INSERT INTO sightings (species, location, observed_at) VALUES ('Barn Owl', 'Meadow', '2026-11-30 22:15'), ('Red Fox', 'Forest Edge', '2026-12-01 07:42'); ``` Updates follow the same pattern with `UPDATE … SET … WHERE …`.

Altering Tables and Adding Columns

Datasette supports standard SQLite `ALTER TABLE` commands. To add a column for photographer notes:

```sql ALTER TABLE sightings ADD COLUMN notes TEXT; ``` You can then back‑fill or edit notes using regular `UPDATE` statements. Because the server runs as root, these schema changes take effect immediately without restarting.

Defining Relationships

Foreign Keys

To relate sightings to a `users` table that stores observer information, first create the users table:

```sql CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE ); ``` Then add a foreign‑key column to `sightings`:

```sql ALTER TABLE sightings ADD COLUMN userid INTEGER REFERENCES users(id); ``` When you query a sighting, Datasette can now follow the `userid` link to display the observer’s name.

Many‑to‑Many Relationships

For more complex scenarios—such as linking sightings to multiple tags—you create a join table:

```sql CREATE TABLE sightingtags ( sightingid INTEGER REFERENCES sightings(id), tagid INTEGER REFERENCES tags(id), PRIMARY KEY (sightingid, tag_id) ); ``` This pattern enables flexible categorisation without duplicating data.

Using the New Label Column

Datasette 1.0a47 introduces a label column attribute. By setting a column as the label, you tell Datasette which field best represents a row when it appears in a relationship view. For example:

```sql ALTER TABLE users SET LABEL COLUMN name; ``` Now, whenever a user is displayed as part of a related record (e.g., in a sighting’s detail view), the UI will show the user’s name instead of the raw `id`.

Practical Takeaways

  • Quick launch – `uvx --pre datasette … --root` gets you a fully‑featured instance without a global install.
  • Schema changes are live – Root mode lets you create, alter, and drop tables on the fly.
  • Relationships are explicit – Use foreign keys for one‑to‑many and join tables for many‑to‑many.
  • Label columns improve readability – Designate a human‑friendly column to make related data clearer.

Closing Thoughts

Datasette’s blend of SQLite simplicity and web‑ready APIs makes it ideal for rapid prototyping and lightweight data publishing. By mastering table creation, data entry, and relationship mapping—including the new label column—you can build richer, more navigable datasets with minimal friction. The next step is to explore Datasette plugins for authentication, custom visualisations, and deployment to cloud platforms, turning your local database into a shareable, interactive resource.

Source

Read original source

Why we picked this

Tutorial on data entry in Datasette highlights new features useful for AI‑driven data workflows.

← Back to all articles