Linkedin_Articles

View on GitHub

SQL Operations: DDL, DML, TCL - A Comprehensive Guide with Best Practices & Performance Considerations

Structured Query Language (SQL) is the backbone of relational databases, enabling users to define, manipulate, and control data efficiently. In this article, we will explore Data Definition Language (DDL), Data Manipulation Language (DML), and Transaction Control Language (TCL) operations in detail. We’ll cover their syntax, usage, and best practices across Oracle SQL, MySQL, and PostgreSQL. Additionally, we’ll discuss performance considerations, common pitfalls, and real-world scenarios to help both beginners and professionals master SQL.

Table of Contents

  1. SQL Objects Overview
    1.1 Database
    1.2 Tables
    1.3 Views

  2. Data Definition Language (DDL)
    2.1 CREATE
    2.2 ALTER
    2.3 DROP

  3. Data Manipulation Language (DML)
    3.1 INSERT
    3.2 UPDATE
    3.3 DELETE

  4. Transaction Control Language (TCL)
    4.1 COMMIT
    4.2 ROLLBACK
    4.3 SAVEPOINT

  5. Data Control Language (DCL)
    5.1 GRANT
    5.2 REVOKE

  6. Common SQL Pitfalls & How to Avoid Them

  7. Hands-On Exercises
    7.1 For Beginners
    7.2 For Professionals

  8. Glossary

  9. Conclusion


1. SQL Objects Overview

Before diving into SQL operations, let’s understand the key database objects:

1.1 Database

A database is a structured collection of data managed by a Database Management System (DBMS). It stores tables, schemas, indexes, views, and other objects.

Example:

CREATE DATABASE company_db;
USE company_db;

1.2 Tables

Tables store data in rows and columns. Each column has a data type and constraints, ensuring data integrity.

Performance Tip:

Example:

CREATE TABLE employees (
    emp_id INT PRIMARY KEY,  -- Unique identifier for each employee
    name VARCHAR(100) NOT NULL,  -- Employee name, cannot be null
    department VARCHAR(50),
    salary DECIMAL(10,2) CHECK (salary > 0),  -- Salary must be positive
    hire_date DATE DEFAULT CURRENT_DATE  -- Defaults to the current date
);

1.3 Views

A view is a virtual table representing the result of a SQL query. It does not store data but simplifies complex queries.

Limitations:

Example:

CREATE VIEW active_employees AS
SELECT emp_id, name FROM employees WHERE status = 'Active';

2. Data Definition Language (DDL)

DDL commands define and modify database structures.

2.1 CREATE

Used to create tables, views, schemas, sequences, and indexes.

Best Practices:

Create Table Example:

CREATE TABLE employees (
    emp_id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    department VARCHAR(50),
    salary DECIMAL(10,2) CHECK (salary > 0),
    hire_date DATE DEFAULT CURRENT_DATE
);

2.2 ALTER

Used to modify existing database objects, such as adding or modifying columns.

Example:

ALTER TABLE employees ADD COLUMN email VARCHAR(100) UNIQUE;

Performance Consideration:


2.3 DROP

Deletes a database object permanently.

Example:

DROP TABLE employees;

Best Practice:


3. Data Manipulation Language (DML)

DML commands manipulate stored data.

3.1 INSERT

Inserts new records into a table.

Example:

INSERT INTO employees (emp_id, name, department, salary) 
VALUES (1, 'Alice', 'HR', 50000);

Performance Tip:


3.2 UPDATE

Modifies existing records in a table.

Example:

UPDATE employees SET salary = 55000 WHERE emp_id = 1;

Performance Consideration:


3.3 DELETE

Removes specific rows from a table.

Example:

DELETE FROM employees WHERE emp_id = 1;

Best Practices:


4. Transaction Control Language (TCL)

TCL commands manage database transactions.

4.1 COMMIT

Saves all changes made in a transaction permanently.

Example:

BEGIN TRANSACTION;
UPDATE employees SET salary = 60000 WHERE emp_id = 2;
COMMIT;

4.2 ROLLBACK

Reverts changes made within a transaction.

Example:

BEGIN TRANSACTION;
UPDATE employees SET salary = 60000 WHERE emp_id = 2;
ROLLBACK;

4.3 SAVEPOINT

A SAVEPOINT is a point within a transaction to which you can later roll back without affecting the entire transaction.

Syntax:

SAVEPOINT savepoint_name;
ROLLBACK TO savepoint_name;

Example:

BEGIN TRANSACTION;
INSERT INTO employees (emp_id, name) VALUES (1, 'Alice');
SAVEPOINT sp1;
INSERT INTO employees (emp_id, name) VALUES (2, 'Bob');
ROLLBACK TO sp1;  -- Undo the second insert, but keep the first
COMMIT;

5. Data Control Language (DCL)

DCL is used for granting and revoking permissions on database objects.

5.1 GRANT

Gives specific privileges to users.

Example:

GRANT SELECT, INSERT ON employees TO user1;

Security Tip:


5.2 REVOKE

Removes previously granted privileges.

Example:

REVOKE INSERT ON employees FROM user1;

6. Common SQL Pitfalls & How to Avoid Them

Pitfall Explanation Solution
*Using SELECT ** Fetches unnecessary columns, increasing load Always specify required columns
Not indexing properly Slow queries due to full table scans Index frequently used search columns
Ignoring transactions Data inconsistency risk Use COMMIT/ROLLBACK appropriately
Hard-deleting records Data loss without recovery Use soft deletes with status column
Overuse of triggers Can slow down inserts/updates Use stored procedures instead

7. Hands-On Exercises

For Beginners:

  1. Create a table named students with columns: student_id, name, age, and grade.
  2. Insert 3 rows into the students table.
  3. Update the grade of a student with student_id = 1.

For Professionals:

  1. Create a partitioned table for sales_data based on the year column.
  2. Write a query to update salaries for employees in a specific department using a transaction.
  3. Use MERGE (or equivalent) to perform an upsert operation.

8. Glossary


Conclusion

This article provided an in-depth look at SQL operations, including DDL, DML, TCL, and DCL, with best practices, performance considerations, and common pitfalls. Whether you are a beginner or a professional, these insights will help you write efficient and reliable SQL queries. In the next article, we will cover constraints, indexes, and primary keys in detail.