POSTGRESQL-ESSENTIALS practice question 15 of 15

Which psql meta-command displays the structure of a specific table including columns, types, and constraints?

Full set: 71 questions
Answer options
A: \d tablename Correct answer
B: \dt tablename
C: DESCRIBE tablename;
D: SHOW TABLE tablename;
Correct Answer:
A. \d tablename
Explanation

The psql meta-command \d tablename is used to describe a relation (table, view, index, sequence, or foreign table). When used with a table name, it provides a detailed description of the table's structure, listing all columns, their data types, collation, nullability, and default values. Crucially, it also displays associated indexes, constraints (such as primary keys, foreign keys, and check constraints), and triggers, which fully satisfies the requirements of the question. It is the direct equivalent of the describe command in other database clients.

Why Incorrect

B. \dt tablename: This command lists tables matching the specified pattern; it does not display the detailed structure (columns, constraints) of a specific table.
C. DESCRIBE tablename;: This is not a valid command in psql for describing table structure. DESCRIBE is used for prepared statements, and this syntax is common in other RDBMS like MySQL.
D. SHOW TABLE tablename;: This is invalid syntax. The SHOW command in PostgreSQL is used to display the value of a run-time parameter, not to describe a table.

References

1. PostgreSQL 16 Documentation, psql, Meta-Commands: The official documentation explicitly lists and describes the \d command. It states: "For tables...lists the columns, their types...and any special attributes...Associated indexes, constraints, rules, and triggers are also listed."

Source: PostgreSQL 16 Documentation, Chapter 35. psql, Section 35.5. Meta-Commands.

Link: https://www.postgresql.org/docs/16/app-psql.html#PSQL-META-COMMANDS-DESCRIBE

2. University of California, Berkeley, Courseware: In the "Connecting to PostgreSQL" guide for the Data 101 course, the \d command is introduced as the primary method for inspecting the schema of a table within psql.

Source: UC Berkeley, Data 101, "Lab 01: Introduction to Jupyter and Data 101," Section on psql.

Reference: Course materials frequently demonstrate \d to "describe a relation."

3. PostgreSQL Tutorial Website (Official Vendor Resource): The official tutorial demonstrates the use of psql commands. In the "Accessing A Database" section, it introduces \d as the command to "find out what tables the database contains and what their columns are."

Source: PostgreSQL Tutorial, Chapter 1. Getting Started, Section 1.3. Accessing a Database.

Link: https://www.postgresql.org/docs/current/tutorial-accessdb.html

Scroll to Top