Education

Ultimate Guide to SQL: Master DDL, DML, DQL, DCL, TCL Commands

SQL (Structured Query Language) is the standard language for managing and manipulating relational databases. It allows you to define database structures, manipulate data, query data, control access, and manage transactions effectively.

This article explains the core components of SQL, including detailed examples for each SQL command, helping you understand how to work with databases using this powerful language.

Table of Contents

    What is SQL?

    SQL is a language used to communicate with relational databases. It enables you to perform operations such as creating, altering, and querying database objects (tables, views, indexes), as well as manipulating data and controlling access. SQL commands are divided into five major categories:

    1. Data Definition Language (DDL)
    2. Data Manipulation Language (DML)
    3. Data Query Language (DQL)
    4. Data Control Language (DCL)
    5. Transactional Control Language (TCL)

    Data Definition Language (DDL)

    DDL commands are used to define and manage database structures, such as creating, altering, or deleting databases and tables. These commands form the foundation of a database system and are essential for setting up its structure.

    Key DDL Commands:
    1. CREATE: Used to create databases, tables, and other database objects.
    2. ALTER: Modifies an existing database object (like a table).
    3. DROP: Deletes an existing database object.
    4. TRUNCATE: Removes all rows from a table but does not delete the table structure.

    Creating a Database

    The CREATE DATABASE command is used to create a new database.

    Syntax:

    Example: To create a database called “company”:

    To verify the creation of the database, use:

    Dropping a Database

    The DROP DATABASE command is used to delete an existing database.

    Syntax:

    Example: To drop the “company” database:

    Verify the database has been deleted with:

    Creating Tables

    Before creating tables, you must select a database to work with using the USE command.

    Syntax to create a table:

    Example: To create a “customers” table with columns for customer details:

    Altering Tables

    The ALTER command is used to modify the structure of an existing table, such as adding, removing, or changing columns.

    Changing a Column

    Syntax:

    Example: To rename the “Second_name” column to “last_name” in the “customers” table:

    Modifying a Column

    Syntax:

    Example: To change the data type of the “first_name” column to VARCHAR(25) and make it NOT NULL:

    Adding a Column

    Syntax:

    Example: To add a “Salary” column to the “customers” table:

    Dropping a Column

    Syntax:

    Example: To drop the “Salary” column from the “customers” table:

    Dropping Tables

    The DROP TABLE command is used to delete an existing table from the database.

    Syntax:

    Example: To delete the “customers” table:

    Renaming Tables

    The RENAME TABLE command allows you to change the name of an existing table.

    Syntax:

    Example: To rename the “customers” table to “customer_info”:

    Data Manipulation Language (DML)

    DML commands are used to manipulate data stored in the database. These commands allow you to insert, update, delete, or select data in the database.

    Key DML Commands:
    1. INSERT: Adds new records to a table.
    2. UPDATE: Modifies existing records in a table.
    3. DELETE: Removes records from a table.
    4. SELECT: Retrieves data from one or more tables.

    Inserting Data into a Table

    The INSERT INTO command is used to add new records into a table.

    Syntax:

    Example: To add a new customer to the “customers” table:

    Updating Data in a Table

    The UPDATE command is used to modify existing records in a table.

    Syntax:

    Example: To update the last name of the customer with CustomerId = 1:

    Deleting Data from a Table

    The DELETE command is used to remove records from a table.

    Syntax:

    Example: To delete a customer with CustomerId = 1:

    Warning: If you omit the WHERE clause, all records in the table will be deleted.

    Selecting Data from a Table

    The SELECT command is used to retrieve data from one or more tables. It is one of the most commonly used SQL commands.

    Syntax:

    Example: To retrieve all customer names from the “customers” table:

    To retrieve customer details for customers from the USA:

    You can also sort the results using ORDER BY:

    Data Query Language (DQL)

    DQL is primarily used for querying and retrieving data from the database. The main DQL command is SELECT, which allows you to extract data based on specific criteria.

    As discussed earlier, the SELECT command allows you to retrieve records from one or more tables and filter them using various conditions. You can also perform joins, group data, and apply functions like COUNT, AVG, MAX, etc., to summarize the data.

    Data Control Language (DCL)

    DCL commands are used to control access to data in the database. They help manage permissions and roles for users.

    Key DCL Commands:
    1. GRANT: Provides privileges to users.
    2. REVOKE: Removes previously granted privileges.

    Granting Permissions

    The GRANT command is used to provide specific privileges to a user or role on a database object.

    Syntax:

    Example: To grant a user the ability to select data from the “customers” table:

    Revoking Permissions

    The REVOKE command is used to remove previously granted privileges from a user.

    Syntax:

    Example: To revoke the select privilege on the “customers” table from the user:

    Transaction Control Language (TCL)

    TCL commands are used to manage transactions, ensuring that operations are completed successfully or undone in case of errors.

    Key TCL Commands:
    1. COMMIT: Saves all changes made during the current transaction.
    2. ROLLBACK: Undoes changes made during the current transaction.
    3. SAVEPOINT: Sets a point within a transaction that can be rolled back to.
    4. SET TRANSACTION: Defines the characteristics of a transaction.

    Committing Changes

    The COMMIT command saves all changes made during the current transaction.

    Syntax:

    Example:

    Rolling Back Changes

    The ROLLBACK command is used to undo the changes made during the current transaction.

    Syntax:

    Example:

    Starting a Transaction:

    Or:

    Creating a Savepoint:

    Example:

    Rolling Back to a Savepoint:

    Example:

    Releasing a Savepoint:


    Once a savepoint is released, it can no longer be rolled back.

    Example:

    For more clarification and explanation, you can go to this website.

    SQL is a powerful language for managing and manipulating data in relational databases. This guide covers the key SQL commands and provides examples of their usage. Understanding these SQL commands—DDL, DML, DQL, DCL, and TCL—is essential for any database management or application development task. Master these commands to handle data efficiently, query information, and manage transactions securely.

    If you’re looking to expand your programming skills beyond SQL, check out our comprehensive guide on Python Basics for Beginners. It covers everything you need to start coding in Python.

    Ferry

    I am an AI Enthusiastic and Content Creator with solid background in Technical field

    Recent Posts

    Aircall Is Hiring Now For Remote Customer Support Role

    Are you looking for an exciting remote opportunity in customer support? Aircall is hiring now…

    23 hours ago

    Deloitte Is Hiring For Data Analyst Role -Apply Now

    Are you looking for an opportunity to grow your career in data analysis? Deloitte is…

    2 days ago

    American Express Is Hiring For Engineering Role Now

    Are you looking to advance your career with a top-tier financial institution? American Express is…

    3 days ago

    Apply Now For Remote Virtual Assistant At Persona

    Are you looking for a rewarding work-from-home career opportunity? Apply now for Remote Virtual Assistant…

    3 days ago

    Accenture New Job Opening For Associate in 2025

    If you are looking for an exciting career opportunity, Accenture New Job Opening for Associate…

    4 days ago

    HireVedia Is Hiring Now For Associate in 2025

    A new job opening for the job seekers where HireVeda Is Hiring Now For Associate…

    5 days ago