gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
give me a postgres query that will return the columns, data types, indexes and…
- User
give me a postgres query that will return the columns, data types, indexes and constraints of a table without using information_schema
- Assistant
To retrieve the columns, data types, indexes, and constraints of a table in PostgreSQL without using the information_schema, you can use the following query:
SELECT a.attname AS column_name, format_type(a.atttypid, a.atttypmod) AS data_type, ix.indisprimary AS is_primary_key, CASE WHEN a.attnotnull = true THEN 'NOT NULL' ELSE '' END AS is_not_null, CASE WHEN fk.constraint_type = 'FOREIGN KEY' THEN fk.constraint_name ELSE '' END AS foreign_key FROM pg_class c JOIN pg_attribute a ON a.attrelid = c.oid LEFT JOIN (SELECT conname, confrelid, confkey[1] AS confkey, confrelname, conrelname, confmatchtype, (SELECT string_agg(attname, ', ' ORDER BY x.n) FROM generate_subscripts(confkey, 1) x(n) JOIN pg_attribute k ON k.attnum = confkey[x.n] WHERE k.attrelid = confrelid) AS foreign_keys FROM pg_constraint WHERE confrelid != 0) fk ON fk.conrelname = c.relname AND fk.conname = any(c.conname) LEFT JOIN pg_index ix ON ix.indrelid = c.oid AND a.attnum = ANY(ix.indkey) WHERE c.relname = 'your_table_name' AND a.attnum > 0;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.