The PostgreSQL DESCRIBE TABLE
statement is used to display the structure of a table. This statement is essential for understanding the schema of a table, including its columns, data types, and constraints.
\d table_name
The \d
command is used in the psql command-line interface to describe a table.
Let's look at some examples of PostgreSQL DESCRIBE TABLE
statement queries:
\d employees
This command describes the structure of the employees
table, displaying its columns, data types, and constraints.
\d+ employees
This command provides a more detailed description of the employees
table, including additional information such as storage and description.
Let's go through a complete example that includes creating a table, inserting data, and then describing the table.
This step involves creating a new table named employees
to store employee data.
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100)
);
In this example, we create a table named employees
with columns for id
, first_name
, last_name
, and email
.
This step involves inserting some sample data into the employees
table.
INSERT INTO employees (first_name, last_name, email)
VALUES ('John', 'Doe', 'john.doe@example.com');
INSERT INTO employees (first_name, last_name, email)
VALUES ('Jane', 'Smith', 'jane.smith@example.com');
INSERT INTO employees (first_name, last_name, email)
VALUES ('Jim', 'Brown', 'jim.brown@example.com');
Here, we insert data into the employees
table.
This step involves describing the structure of the employees
table.
\d employees
This command displays the structure of the employees
table, showing its columns, data types, and constraints.
The PostgreSQL DESCRIBE TABLE
statement is a fundamental tool for understanding the schema of a table. Understanding how to use the \d
command and its syntax is essential for effective database management and schema exploration in PostgreSQL.