SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | DDL, DML, TCL and DCL

Let's delve into these SQL command categories again, focusing on Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL).

1. Data Definition Language (DDL):

DDL commands define and manage the structure of database objects.

Common DDL commands include:

  • CREATE: Used to create database objects such as tables, views, or indexes.

    CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
    
  • ALTER: Used to modify the structure of an existing database object, such as adding a column to a table.

    ALTER TABLE table_name ADD column_name datatype;
    
  • DROP: Deletes database objects.

    DROP TABLE table_name;
    
  • TRUNCATE: Empties a table, removing all its rows.

    TRUNCATE TABLE table_name;
    

2. Data Manipulation Language (DML):

DML commands manipulate and handle the data within the database.

Common DML commands include:

  • SELECT: Retrieves data from a table.

    SELECT column1, column2 FROM table_name WHERE condition;
    
  • INSERT: Inserts data into a table.

    INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
    
  • UPDATE: Modifies existing data in a table.

    UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
    
  • DELETE: Removes data from a table.

    DELETE FROM table_name WHERE condition;
    

3. Data Control Language (DCL):

DCL commands manage the permissions and rights in the database.

Common DCL commands are:

  • GRANT: Grants permissions to users.

    GRANT permission ON object TO user;
    
  • REVOKE: Removes permissions from users.

    REVOKE permission ON object FROM user;
    

4. Transaction Control Language (TCL):

TCL commands control the transactions in the database to ensure data integrity.

Common TCL commands are:

  • COMMIT: Commits and saves all the changes made during the current transaction.

    COMMIT;
    
  • ROLLBACK: Undoes changes made during the current transaction or since a particular savepoint.

    ROLLBACK TO savepoint_name;
    
  • SAVEPOINT: Sets a point within a transaction to which you can later roll back.

    SAVEPOINT savepoint_name;
    
  • SET TRANSACTION: Configures the properties of a transaction.

    SET TRANSACTION [ READ WRITE | READ ONLY ];
    

These command categories serve as the backbone of SQL operations. However, the specific syntax or support for certain commands might differ across various RDBMSs, so it's essential to consult the official documentation for the specific SQL system you are using.

  1. CREATE:

    • Description: Creates a new database object (table, view, index, etc.).
    • Example:
      CREATE TABLE employees (
        emp_id INT PRIMARY KEY,
        emp_name VARCHAR(50),
        emp_salary DECIMAL(10, 2)
      );
      
  2. ALTER:

    • Description: Modifies the structure of an existing database object.
    • Example:
      ALTER TABLE employees
      ADD COLUMN emp_department VARCHAR(50);
      
  3. DROP:

    • Description: Removes an existing database object (table, view, index, etc.).
    • Example:
      DROP TABLE employees;
      

DML Commands in SQL with Examples:

  1. INSERT:

    • Description: Adds new records to a table.
    • Example:
      INSERT INTO employees (emp_name, emp_salary)
      VALUES ('John Doe', 60000);
      
  2. UPDATE:

    • Description: Modifies existing records in a table.
    • Example:
      UPDATE employees
      SET emp_salary = 65000
      WHERE emp_name = 'John Doe';
      
  3. DELETE:

    • Description: Removes records from a table.

    SQL Transaction Control Language Commands:

    1. COMMIT:

      • Description: Saves changes made during the current transaction.
      • Example:
        COMMIT;
        
    2. ROLLBACK:

      • Description: Undoes changes made during the current transaction.
      • Example:
        ROLLBACK;
        
    3. SAVEPOINT:

      • Description: Sets a savepoint within a transaction to which you can later roll back.
      • Example:
        SAVEPOINT my_savepoint;
        
    4. ROLLBACK TO SAVEPOINT:

      • Description: Rolls back a transaction to a specific savepoint.
      • Example:
        ROLLBACK TO SAVEPOINT my_savepoint;