SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | DESCRIBE Statement

The DESCRIBE statement (often abbreviated as DESC) is used in SQL to retrieve metadata about a specified table or view. It provides details like column names, data types, whether a column can be NULL, and other attributes of the table. This command is particularly useful when you want to examine the structure of a table.

However, the availability and exact syntax of the DESCRIBE statement can vary between different relational database management systems (RDBMS). Here are some examples for popular RDBMS:

1. MySQL and Oracle:

DESCRIBE table_name;

or simply:

DESC table_name;

2. Microsoft SQL Server:

SQL Server does not have a DESCRIBE statement. Instead, you'd typically use the sp_help stored procedure or query the INFORMATION_SCHEMA.COLUMNS system view.

EXEC sp_help table_name;

or

SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_name';

3. PostgreSQL:

PostgreSQL doesn't have a DESCRIBE statement either. Instead, you can use the \d command in the psql command-line tool:

\d table_name

Alternatively, you can query the information_schema or pg_catalog schema to get details about tables and columns.

4. SQLite:

In SQLite, the .schema command is used to see the creation SQL for a specific table:

.schema table_name

The result will be the CREATE TABLE statement used to define the table, which includes column names, data types, and other constraints.

In Conclusion:

When working with an RDBMS, it's essential to know how to inspect the structure of a table, especially when dealing with unfamiliar databases. The method you use to achieve this will depend on the RDBMS in question, so always consult the relevant documentation if unsure.

  1. How to Use DESCRIBE in Oracle:

    • Description: The DESCRIBE statement in Oracle is used to display the structure of a table, including column names, data types, and constraints.
    • Example:
      DESCRIBE employees;
      
  2. DESCRIBE Command in Oracle SQL:

    • Description: The DESCRIBE command provides information about the structure of a table.
    • Example:
      DESCRIBE employees;
      
  3. View Table Structure in Oracle SQL:

    • Description: Displays the details of a table, such as column names, data types, and constraints.
    • Example:
      DESCRIBE employees;
      
  4. Oracle DESCRIBE Statement for Columns:

    • Description: Describes the attributes of each column in a table.
    • Example:
      DESCRIBE employees;
      
  5. DESCRIBE TABLE in Oracle SQL:

    • Description: Provides information about the structure of a table.
    • Example:
      DESCRIBE employees;
      
  6. Get Information About a Table in Oracle:

    • Description: Retrieves details about the specified table, including column names and data types.
    • Example:
      DESCRIBE employees;
      
  7. DESCRIBE Statement Alternatives in Oracle:

    • Alternative 1:
      SELECT COLUMN_NAME, DATA_TYPE
      FROM USER_TAB_COLUMNS
      WHERE TABLE_NAME = 'EMPLOYEES';
      
    • Alternative 2:
      SELECT * FROM ALL_TAB_COLUMNS WHERE TABLE_NAME = 'EMPLOYEES';
      
  8. Exploring Table Structure with DESCRIBE in Oracle:

    • Description: Analyzes the columns, data types, and constraints of a table.
    • Example:
      DESCRIBE employees;
      

MySQL DESCRIBE Statement Example:

  1. Using DESCRIBE in MySQL:

    • Description: In MySQL, the DESCRIBE statement is replaced by the SHOW COLUMNS FROM statement to view the structure of a table.
    • Example:
      DESCRIBE employees;
      
  2. SHOW COLUMNS FROM in MySQL:

    • Description: Displays information about the columns of a table in MySQL.
    • Example:
      SHOW COLUMNS FROM employees;
      
  3. View Table Structure in MySQL:

    • Description: Shows details of a table, including column names and data types.
    • Example:
      SHOW COLUMNS FROM employees;
      
  4. Get Information About a Table in MySQL:

    • Description: Retrieves details about the specified table in MySQL.
    • Example:
      SHOW COLUMNS FROM employees;
      
  5. MySQL SHOW COLUMNS FROM Statement:

    • Description: Equivalent to the DESCRIBE statement, it provides information about the columns of a table in MySQL.
    • Example:
      SHOW COLUMNS FROM employees;
      
  6. MySQL DESCRIBE vs. SHOW COLUMNS FROM:

    • Description: Both statements serve the same purpose in MySQL to view table structure.
    • Example:
      DESCRIBE employees;
      SHOW COLUMNS FROM employees;
      
  7. Exploring Table Structure in MySQL:

    • Description: Analyzes the columns, data types, and constraints of a table in MySQL.
    • Example:
      SHOW COLUMNS FROM employees;
      
  8. Retrieve Column Details in MySQL:

    • Description: Retrieves details about each column in a MySQL table.
    • Example:
      SHOW COLUMNS FROM employees;