SQL Database Schema and Metadata: Inspect Tables and Columns

A schema describes database objects and relationships, while metadata catalogs expose information about those objects, their columns and constraints.

Lesson 60 of 102Intermediate10 min read

A schema describes database objects and relationships, while metadata catalogs expose information about those objects, their columns and constraints.

When a query fails because a column is missing, guessing is slower than inspecting the schema. Metadata queries reveal the database’s actual structure. They also help migration tools verify definitions instead of trusting a stale document.

This reference adds detail to the main course. You can read it independently: the small database below supplies every table and row used in the examples. Start with the question each query answers, then compare the returned values with the data rather than memorising a line of syntax.

Set up the practice database

Run this setup once in a new practice database, before the examples. It uses four customers and six sales. Ana has two sales, Ben has one, Cara has two, and Dev has none.

Sale 106 has no customer link; sale 104 has an unknown amount. These deliberate test cases expose missing information and unmatched relationships.

SQL
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name VARCHAR(40) NOT NULL,
  city VARCHAR(40),
  manager_id INTEGER
);
CREATE TABLE sales (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER,
  amount DECIMAL(10,2),
  status VARCHAR(20) NOT NULL,
  created_on DATE NOT NULL
);
INSERT INTO customers (id,name,city,manager_id) VALUES
  (1,'Ana','Leeds',NULL),(2,'Ben','Leeds',1),
  (3,'Cara','Porto',1),(4,'Dev',NULL,2);
INSERT INTO sales (id,customer_id,amount,status,created_on) VALUES
  (101,1,10,'paid','2026-01-01'),
  (102,1,30,'pending','2026-01-02'),
  (103,2,20,'paid','2026-01-02'),
  (104,3,NULL,'pending','2026-02-01'),
  (105,3,20,'paid','2026-02-02'),
  (106,NULL,5,'paid','2026-02-03');

An amount of zero would be a known value. The NULL amount here means the value is unknown. A customer with no matching sale differs from a sale whose customer_id is NULL.

The primary keys identify individual records even when amounts or cities repeat. Dates use year-month-day format. The examples specify sorting whenever order matters; insertion order alone does not guarantee display order.

How Database schema and metadata works

The word schema has two common meanings. It can describe the overall design of tables, columns, keys and relationships. In PostgreSQL and SQL Server it also names a namespace that contains objects, so public.sales or dbo.sales is a qualified object name. MySQL commonly uses schema as a synonym for database. SQLite uses attached database names such as main and temp rather than the same server namespace model.

INFORMATION_SCHEMA supplies standard-style views in PostgreSQL, MySQL and SQL Server for common metadata such as tables and columns. Filter by the relevant catalog and schema as well as the table name to avoid combining objects with the same name from different namespaces. Metadata visibility depends on permissions; an absent row can mean the current account cannot see the object, not necessarily that the object does not exist.

Engine-specific catalogs and PRAGMA

Engine-specific catalogs expose details beyond standard views. PostgreSQL has pg_catalog, SQL Server has sys views, MySQL has its own catalog and information-schema extensions, and SQLite stores schema definitions in sqlite_schema and offers PRAGMA inspection commands. SQLite PRAGMA table_info returns common column details, while table_xinfo includes additional hidden or generated-column information. Catalog fields and meanings must be interpreted through the engine’s documentation.

Inspect types, nullability, default expressions and key definitions separately. A column named customer_id does not prove a foreign key exists, and a column named id does not prove it is a primary key. Reading a raw CREATE statement can help, but automated tooling should use reliable catalog information rather than fragile text matching. Metadata is not the same as business data: table definitions cannot tell you whether every saved email is valid. Combine schema inspection with targeted data-quality queries when validating a migration or import.

MySQL, PostgreSQL, SQL Server and SQLite

This PRAGMA is SQLite-specific. In PostgreSQL, MySQL and SQL Server, query INFORMATION_SCHEMA.COLUMNS with table_name and the appropriate schema filter. PostgreSQL and SQL Server namespace semantics differ from MySQL’s database/schema equivalence. Use engine catalogs for details that INFORMATION_SCHEMA does not expose reliably.

Check the engine-specific syntax and version requirements in SQLite table_info metadata, PostgreSQL information schema.

Practice task and worked solution

In SQLite, list the CREATE definitions of the two main practice tables using sqlite_schema. Restrict the result to ordinary tables and sort by name.

Try the task before reading its worked solution. The supplied rows make the expected answer checkable by hand.

SQL
SELECT name,sql FROM sqlite_schema
WHERE type='table' AND name IN ('customers','sales') ORDER BY name;
Output
name | sql
customers | The CREATE TABLE customers definition from setup
sales | The CREATE TABLE sales definition from setup

The rows contain the saved schema SQL for the two known objects, not their business records. The complete sql strings correspond to the setup definitions above; formatting may reflect the original creation text. The type and name filters exclude indexes and unrelated temporary objects.

Compare PRAGMA table_info(customers) with the CREATE text in sqlite_schema and locate the primary-key declaration in both representations. Then inspect sales and note that customer_id has no enforced foreign key in this deliberately simple fixture. A matching name and valid-looking sample data are not substitutes for an actual relationship constraint.

SQL Database Schema and Metadata: Inspect Tables and Columns Syntax

Database schema and metadata

-- SQLite schema inspection example.
PRAGMA table_info(customers);

SQL Database Schema and Metadata: Inspect Tables and Columns Summary

  • "Schema" means both the overall design of tables and, in PostgreSQL and SQL Server, a namespace such as public or dbo.
  • INFORMATION_SCHEMA.TABLES and INFORMATION_SCHEMA.COLUMNS describe tables and columns in PostgreSQL, MySQL and SQL Server.
  • SQLite uses PRAGMA table_info(table) and the sqlite_schema table instead.
  • Filter by schema as well as table name, because the same table name can exist in several namespaces.
  • Metadata shows only objects your account may see, so an empty result can be a permissions issue.

SQL Database Schema and Metadata: Inspect Tables and Columns Examples

Each example shows the code, the output it produces and what each part does.

Example 1

The columns of the sales table in SQLite

SQLite reports four column definitions and flags the primary key. Its notnull metadata flag for this INTEGER PRIMARY KEY can be zero even though the rowid alias gives special key behaviour. Interpret that field with SQLite’s documented semantics rather than assuming it is a complete cross-engine nullability description.

SQL
-- SQLite schema inspection example.
PRAGMA table_info(customers);
Output
cid | name | type | notnull | dflt_value | pk
0 | id | INTEGER | 0 | NULL | 1
1 | name | VARCHAR(40) | 1 | NULL | 0
2 | city | VARCHAR(40) | 0 | NULL | 0
3 | manager_id | INTEGER | 0 | NULL | 0

Common SQL Database Schema and Metadata: Inspect Tables and Columns Mistakes

Searching metadata by table name alone

Searching only WHERE table_name='sales' can combine or confuse objects in different namespaces. A similarly named column is also not evidence that the expected constraint exists. Include catalog/schema filters where relevant and inspect the real key definitions rather than inferring them from naming conventions.

SQL Database Schema and Metadata: Inspect Tables and Columns in Real Projects

Checking a migration before deployment

A migration check can confirm that a new required field exists, has the expected type and has the intended constraint before deploying code that uses it. Keep the inspection account’s permissions in mind so restricted metadata visibility does not lead to a false conclusion about a missing object.

A migration verification should inspect type, nullability and constraints as separate properties. Naming a field customer_id does not prove a foreign key exists. Filter metadata by the intended namespace and account context, and distinguish permission-limited visibility from object absence. Use engine catalogs when a standard information-schema view lacks the necessary detail.

SQL Database Schema and Metadata: Inspect Tables and Columns FAQ

Does a column name establish a constraint?

No. A column called customer_id isn't necessarily a foreign key. Inspect the actual primary-key, unique and foreign-key definitions, for example in INFORMATION_SCHEMA.TABLE_CONSTRAINTS or with SQLite's PRAGMA foreign_key_list(sales).

Why can metadata appear empty?

Check the current database, the namespace and the account's permissions: metadata views show only objects the user can access. In PostgreSQL, also check that you're filtering the right schema, often public, and that the table name is lowercase.

Is INFORMATION_SCHEMA identical everywhere?

No. It offers shared concepts, but supported views, extensions and visibility rules still differ between databases, and SQLite doesn't have it. Use it for common questions, and the engine's own catalog for details such as index definitions.

How do I list all tables in a database?

In PostgreSQL and SQL Server, query INFORMATION_SCHEMA.TABLES filtered by schema, or use \dt in psql. In MySQL, run SHOW TABLES;. In SQLite, run SELECT name FROM sqlite_schema WHERE type = 'table'; or .tables in the command-line shell.

How do I see a table's full CREATE statement?

MySQL has SHOW CREATE TABLE sales;, and SQLite stores the original statement in sqlite_schema.sql. PostgreSQL's pg_dump --schema-only -t sales prints it, and \d sales in psql shows columns, indexes and constraints. Tools such as DBeaver can also generate the DDL.

SQL Database Schema and Metadata: Inspect Tables and Columns Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choicemedium

Which field does SQLite table_info mark as the customers primary key?

SQL Database Schema and Metadata: Inspect Tables and Columns Quiz

2 questions to check what you learned.

  1. What is metadata?

    • Information describing database objects and their definitions
    • Only the ordinary customer names stored as records
    • A duplicate copy of each table’s business rows
  2. Which SQLite command inspects ordinary table columns?

    • DESCRIBE table_name, as universal SQLite syntax
    • PRAGMA table_info(table_name)
    • INFORMATION_SCHEMA.COLUMNS, which SQLite always provides as a built-in view