liveBook · Manning

Schema

As explained in Data Analysis with LLMs

A database schema is the defined structure of a database, including its tables, columns, data types, and relationships.

A database schema describes how data is organized. For a SQL database, table definitions give a query writer the table names, column names, and data types it needs. For a graph database, the useful description instead lists node labels, relationship types, and properties. When asking a language model to turn a question into a query, provide this structure alongside the question and the target query language. section 5.2.4 section 5.4.5

Describe a table before asking for SQL

Suppose you want to ask questions about video-game sales. A prompt can begin with the games table’s CREATE TABLE statement, which names columns such as platform, genre, and globalsales. Follow it with an instruction to translate a question into SQL, then insert the question. The same template works for different questions as long as the database structure stays the same. section 5.2.4

Database:                                          #1
CREATE TABLE games(rank int, name text, platform text,
year int, genre text, publisher text, americasales numeric,
eusales numeric, japansales numeric, othersales numeric,
globalsales numeric);
Translate this question into SQL query:  #2
[Question]                          #3

This prompt places the table definition before the translation instruction and question, so the model can use the actual column names. section 5.2.4

Hardcoding a schema is convenient for one database, but an interface for arbitrary SQLite databases needs to discover the current structure. SQLite’s automatically created sqlite_master table stores SQL statements used to create database objects. Retrieve its table-creation statements and join them into a description for the prompt; Python’s sqlite3 module can perform this step. A reusable prompt function then accepts that description and the user’s question as separate inputs. section 5.3.2 section 5.3.3

import sqlite3

def get_structure(data_path):
    with sqlite3.connect(data_path) as connection:  #1
        cursor = connection.cursor()          #2
        cursor.execute("select sql from sqlite_master where type =
         'table';")             #3
        table_rows = cursor.fetchall()      #4
        table_ddls = [r[0] for r in table_rows]  #5
        return '\n'.join(table_ddls)        #6

The Python function connects to a SQLite file, fetches its table definitions, and returns them as prompt-ready text. section 5.3.2

For a Neo4j movie database, a corresponding prompt names node labels such as Movie and Person, relationship types such as ACTED_IN, and relevant property keys before requesting a Cypher query. Replace that description when changing databases. Whether the target is SQL or Cypher, treat the generated query as a proposal: it may be incorrect or fail to execute. Copy the database before running generated queries. section 5.4.5 section 5.5

How Data Analysis with LLMs covers it

How deep it goes
Shows hardcoded definitions and automatic SQLite table extraction, while hardcoding rather than extracting the graph description.
Examples
Uses a video-game sales table and a movie graph’s node labels, relationship types, and properties.
What it assumes
Assumes basic Python database connections and query execution; introduces schema descriptions through tables, columns, and data types.
What you can do after
Build it from scratch by extracting SQLite table definitions with sqlite3 and inserting them into reusable question-to-SQL prompts.
How it connects
Connects schemas to text-to-SQL prompting, SQLite metadata, graph databases, and Cypher query generation.

Also explained in

Compare how 44 books explain schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.