capypad
0 day streak
sql / expert
Snippet

Metadata Introspection via Information Schema

Expert SQL developers use the INFORMATION_SCHEMA to write database-agnostic scripts for auditing or dynamic query generation. This snippet queries the standard-compliant metadata views to retrieve the exact structure of a specific table.

snippet.sql
sql
1
2
3
4
5
6
7
8
9
SELECT
table_name,
column_name,
data_type,
is_nullable
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'employees'
ORDER BY ordinal_position;
Breakdown
1
FROM information_schema.columns
Accesses the ANSI-standard view containing information about all columns in the database.
2
WHERE table_schema = 'public'
Filters by the specific schema to avoid metadata from system tables.