SQL 1.75 SQL DESC 

DESC

Sure, let’s dive into learning the basics of SQL with a focus on the DESC command, which is used to describe the structure of a table in a database. We’ll cover what DESC does, how to use it, and provide detailed explanations and examples.

Introduction to SQL and the DESC Command

SQL (Structured Query Language) is a standard programming language used for managing and manipulating databases. One of the basic commands you will use in SQL is the DESC command. DESC is short for DESCRIBE, and it is used to provide information about the structure of a table.

What is the DESC Command?

The DESC command is used to describe the schema of a table. It displays the column names, data types, and other attributes of the columns in the table. This is useful for understanding the layout of a table, especially if you did not create it and need to know its structure before querying or modifying it.

Basic Syntax of DESC

DESC table_name;

Where table_name is the name of the table you want to describe.

Step-by-Step Tutorial with Examples

Step 1: Setting Up Your Database Environment

First, ensure you have access to a SQL database. You can use a local setup with software like MySQL, PostgreSQL, SQLite, or an online database service.

For this tutorial, we’ll assume you’re using MySQL. Open your SQL client and connect to your database.

Step 2: Creating a Sample Table

Before we can use the DESC command, we need a table to describe. Let’s create a sample table named employees.

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    date_of_birth DATE,
    hire_date DATE,
    job_title VARCHAR(50),
    salary DECIMAL(10, 2)
);

This command creates a table with seven columns: employee_id, first_name, last_name, date_of_birth, hire_date, job_title, and salary.

Step 3: Using the DESC Command

Now that we have our employees table, let’s describe its structure using the DESC command.

DESC employees;

Output Explanation

Executing the DESC employees; command will produce an output similar to this:

FieldTypeNullKeyDefaultExtra
employee_idint(11)NOPRINULLauto_increment
first_namevarchar(50)YESNULL
last_namevarchar(50)YESNULL
date_of_birthdateYESNULL
hire_datedateYESNULL
job_titlevarchar(50)YESNULL
salarydecimal(10,2)YESNULL

Breakdown of Output Columns

  1. Field: The name of the column.
  2. Type: The data type of the column (e.g., int, varchar, date).
  3. Null: Indicates whether the column can contain NULL values (YES or NO).
  4. Key: Indicates if the column is indexed (PRI for primary key, UNI for unique index, etc.).
  5. Default: The default value for the column if no value is specified during insertion.
  6. Extra: Additional information about the column (e.g., auto_increment).

Step 4: Adding and Describing Another Table

Let’s add another table for more practice. This time we’ll create a departments table.

CREATE TABLE departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(50),
    manager_id INT
);

Describe the departments table:

DESC departments;

Output Explanation

FieldTypeNullKeyDefaultExtra
department_idint(11)NOPRINULLauto_increment
department_namevarchar(50)YESNULL
manager_idint(11)YESNULL

Step 5: Understanding Different Data Types

Different databases might have slight variations in data types, but generally, they include:

  • INT: Integer values.
  • VARCHAR(n): Variable-length string with a maximum length of n.
  • DATE: Date values.
  • DECIMAL(m, d): Decimal values with m digits in total and d digits after the decimal point.

Step 6: Modifying Table Structure

If you modify the structure of a table, you can use DESC again to see the changes. For example, let’s add an email column to the employees table:

ALTER TABLE employees ADD email VARCHAR(100);

Describe the table again:

DESC employees;

Output Explanation After Modification

FieldTypeNullKeyDefaultExtra
employee_idint(11)NOPRINULLauto_increment
first_namevarchar(50)YESNULL
last_namevarchar(50)YESNULL
date_of_birthdateYESNULL
hire_datedateYESNULL
job_titlevarchar(50)YESNULL
salarydecimal(10,2)YESNULL
emailvarchar(100)YESNULL

The new email column has been added and is now part of the table structure.

Summary

  • The DESC command is a simple yet powerful tool to understand the structure of a table in SQL.
  • Use DESC table_name; to see the columns, their data types, nullability, keys, default values, and extra attributes.
  • It’s helpful for getting a quick overview of table design, especially when working with unfamiliar databases.
  • Practice creating and modifying tables to see how DESC reflects those changes.

By mastering the DESC command, you enhance your ability to navigate and manage databases effectively. Keep practicing with different table structures and data types to become proficient in SQL.

Leave a Reply