Schema Understanding and Injection
Extracting and formatting DB schema for LLM context: tables, columns, relations.
Why Schema Context Matters
The LLM knows SQL syntax but knows nothing about your database. Without schema context, it will hallucinate table and column names.
Schema injection means programmatically extracting your DB structure and including it in every prompt — making the LLM aware of your exact tables, columns, and types.
Querying INFORMATION_SCHEMA
All major relational databases expose metadata through INFORMATION_SCHEMA. You can query it to get every table, column name, and data type without touching application code.
This works in PostgreSQL, MySQL, SQL Server, and SQLite (with minor differences).
import psycopg2
def get_schema(conn):
query = '''
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'public'
ORDER BY table_name, ordinal_position
'''
with conn.cursor() as cur:
cur.execute(query)
return cur.fetchall()All lessons in this course
- How NL-to-SQL Agents Work
- Schema Understanding and Injection
- Generating and Validating SQL Queries
- Handling Ambiguous Database Questions