What Is DDL and DML in SQL

Structured Query Language (SQL) is a powerful tool used for managing and manipulating relational databases. At its core, SQL is divided into two primary sublanguages: Data Definition Language (DDL) and Data Manipulation Language (DML). Understanding and mastering these sublanguages are crucial for database professionals, as they form the foundation for designing, implementing, and maintaining databases. This article dives deep into DDL and DML, exploring their commands, best practices, and advanced techniques.

Data Definition Language (DDL)

DDL is responsible for defining and managing the structure of database objects. It includes commands for creating, modifying, and deleting database objects such as tables, indexes, and schemas. Here are the essential DDL commands.

1. CREATE

The CREATE command is used to create new database objects. The most common use is for creating tables.

Syntax

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    ...
);

Example

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    BirthDate DATE,
    HireDate DATE,
    Salary DECIMAL(10, 2)
);

2. ALTER

The ALTER command modifies an existing database object. It can be used to add, delete, or modify columns in a table.

Syntax

ALTER TABLE table_name
ADD column_name datatype;

ALTER TABLE table_name
DROP COLUMN column_name;

ALTER TABLE table_name
MODIFY COLUMN column_name datatype;

Example

ALTER TABLE Employees
ADD DepartmentID INT;

ALTER TABLE Employees
DROP COLUMN BirthDate;

ALTER TABLE Employees
MODIFY COLUMN Salary DECIMAL(12, 2);

3. DROP

The DROP command deletes an existing database object. It permanently removes the object and all its data.

Syntax

DROP TABLE table_name;

Example

DROP TABLE Employees;

4. TRUNCATE

The TRUNCATE command removes all records from a table but keeps the table structure intact. It's faster than DELETE without a WHERE clause.

Syntax

TRUNCATE TABLE table_name;

Example

TRUNCATE TABLE Employees;

Data Manipulation Language (DML)

DML is used for managing data within existing database objects. It includes commands for inserting, updating, deleting, and querying data. Here are the key DML commands:

1. INSERT

The INSERT command adds new records to a table.

Syntax

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Example

INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate, Salary)
VALUES (1, 'John', 'Doe', '2023-01-15', 60000);

2. SELECT

The SELECT command retrieves data from one or more tables. It's the most used DML command and can be complex, involving various clauses like WHERE, JOIN, GROUP BY, HAVING, and ORDER BY.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition
GROUP BY column
HAVING condition
ORDER BY column;

Example

SELECT FirstName, LastName, Salary
FROM Employees
WHERE Salary > 50000
ORDER BY Salary DESC;

3. UPDATE

The UPDATE command modifies existing records in a table.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example

UPDATE Employees
SET Salary = Salary * 1.10
WHERE HireDate < '2022-01-01';

4. DELETE

The DELETE command removes records from a table. Unlike TRUNCATE, DELETE can be used with a WHERE clause to delete specific records.

Syntax

DELETE FROM table_name
WHERE condition;

Example

DELETE FROM Employees
WHERE EmployeeID = 1;

Advanced Techniques and Best Practices
 

1. Constraints and Indexes

Proper use of constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK) ensures data integrity and consistency. Indexes improve query performance but should be used judiciously to avoid overhead.

Example

CREATE TABLE Departments (
    DepartmentID INT PRIMARY KEY,
    DepartmentName VARCHAR(50) NOT NULL,
    CONSTRAINT Unique_DepartmentName UNIQUE (DepartmentName)
);

CREATE INDEX idx_Employee_LastName
ON Employees (LastName);

2. Transactions

Transactions ensure that a sequence of DML operations is completed successfully as a unit. If any operation fails, the transaction can be rolled back to maintain data integrity.

Syntax

BEGIN TRANSACTION;

-- DML operations

COMMIT;

-- or

ROLLBACK;

Example

BEGIN TRANSACTION;

UPDATE Employees
SET Salary = Salary * 1.05
WHERE DepartmentID = 1;

DELETE FROM Employees
WHERE HireDate < '2020-01-01';

COMMIT;

3. Handling Large Datasets

For large datasets, consider using batch operations, indexing, and partitioning to optimize performance.

Example of Batch Operation

-- Updating in batches
DECLARE @BatchSize INT = 1000;

WHILE (1 = 1)
BEGIN
    UPDATE TOP (@BatchSize) Employees
    SET Salary = Salary * 1.05
    WHERE SalaryUpdated = 0;

    IF @@ROWCOUNT = 0 BREAK;
END

4. Security and Permissions

Granting appropriate permissions and using roles ensures that users have the necessary access without exposing sensitive data or operations.

Example

-- Creating a read-only role
CREATE ROLE ReadOnly;

-- Granting SELECT permission to the role
GRANT SELECT ON Employees TO ReadOnly;

-- Adding a user to the role
EXEC sp_addrolemember 'ReadOnly', 'username';

Conclusion

Mastering DDL and DML commands in SQL is essential for database professionals. These commands provide the tools needed to define, manipulate, and secure data within a relational database. By following best practices and leveraging advanced techniques, you can ensure that your databases are efficient, reliable, and maintainable. Whether you are designing a new database or optimizing an existing one, a deep understanding of DDL and DML commands will significantly enhance your capabilities as a database expert.


Similar Articles