Keywords: DDL | DML | SQL Commands | Database Languages | Data Definition | Data Manipulation
Abstract: This article provides an in-depth exploration of DDL (Data Definition Language) and DML (Data Manipulation Language) in database systems. Through detailed SQL code examples, it analyzes the specific usage of DDL commands like CREATE, ALTER, DROP and DML commands such as SELECT, INSERT, UPDATE. The article elaborates on their distinct roles in database design, data manipulation, and transaction management, while also discussing the supplementary functions of DCL (Data Control Language) and TCL (Transaction Control Language) to offer comprehensive technical guidance for database development and administration.
Overview of Database Language Classification
In database management systems, SQL (Structured Query Language) serves as the standard interface and is systematically divided into multiple functional subsets, with DDL (Data Definition Language) and DML (Data Manipulation Language) being the most fundamental components. DDL focuses on defining and modifying database structures, while DML handles data manipulation operations. Together, they form the foundational framework for database operations.
Detailed Examination of Data Definition Language (DDL)
DDL is used to define and manage database structures. It does not directly manipulate data but creates and maintains the storage framework for data. DDL commands are typically auto-committed, taking effect immediately upon execution and cannot be rolled back.
Major DDL Commands and Their Functions
CREATE command is used to create database objects. For example, creating an employee table:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50)
);
ALTER command modifies the structure of existing database objects. For example, adding a salary column to the employee table:
ALTER TABLE Employees ADD Salary DECIMAL(10,2);
DROP command deletes database objects. For example, dropping the employee table:
DROP TABLE Employees;
TRUNCATE command quickly removes all records from a table while preserving the table structure:
TRUNCATE TABLE Employees;
RENAME command renames database objects:
RENAME TABLE OldEmployees TO NewEmployees;
COMMENT command adds comments to database objects:
COMMENT ON TABLE Employees IS 'Stores basic employee information';
Detailed Examination of Data Manipulation Language (DML)
DML is used to manipulate actual data within the database through various commands for data retrieval, insertion, updating, and deletion. DML commands typically support transaction control and can be rolled back.
Major DML Commands and Their Functions
SELECT command retrieves data from the database:
SELECT EmployeeID, FirstName, LastName FROM Employees WHERE Department = 'IT';
INSERT command inserts new records into a table:
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary)
VALUES (1, 'John', 'Smith', 'Technology', 50000);
UPDATE command modifies existing data in a table:
UPDATE Employees SET Salary = 55000 WHERE EmployeeID = 1;
DELETE command deletes records from a table:
DELETE FROM Employees WHERE EmployeeID = 1;
MERGE command implements UPSERT operations (insert or update):
MERGE INTO Employees AS target
USING NewEmployees AS source
ON target.EmployeeID = source.EmployeeID
WHEN MATCHED THEN UPDATE SET target.Salary = source.Salary
WHEN NOT MATCHED THEN INSERT (EmployeeID, FirstName, Salary) VALUES (source.EmployeeID, source.FirstName, source.Salary);
CALL command invokes stored procedures or functions:
CALL CalculateEmployeeBonus();
EXPLAIN PLAN command analyzes query execution plans:
EXPLAIN PLAN FOR SELECT * FROM Employees WHERE Salary > 50000;
LOCK TABLE command controls concurrent access:
LOCK TABLE Employees IN EXCLUSIVE MODE;
Core Differences Between DDL and DML
DDL and DML differ fundamentally in functional positioning, operational objects, and execution characteristics. DDL focuses on defining database structures, while DML specializes in actual data manipulation. DDL commands are typically non-rollbackable and immediately alter the database schema upon execution; DML commands support transactions and can be rolled back within transactions. In practical applications, DDL is primarily used by database administrators during system design phases, while DML is frequently employed by application developers in daily operations.
Related Language Extensions: DCL and TCL
Beyond DDL and DML, SQL includes DCL (Data Control Language) and TCL (Transaction Control Language), which together form a comprehensive data management framework.
Data Control Language (DCL)
DCL manages database access permissions and security controls:
GRANT SELECT, INSERT ON Employees TO user1;
REVOKE DELETE ON Employees FROM user2;
Transaction Control Language (TCL)
TCL manages database transactions:
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 1;
UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 2;
COMMIT;
During transaction execution, SAVEPOINT can be used to set savepoints and roll back to specific states when needed:
SAVEPOINT before_update;
UPDATE Employees SET Salary = Salary * 1.1;
-- If updates encounter issues
ROLLBACK TO before_update;
Analysis of Practical Application Scenarios
In real-world database development, DDL and DML must work collaboratively. For instance, in e-commerce systems, DDL is first used to create data structures like product tables and order tables, then DML implements functions such as product listing/delisting, order creation, and querying. Proper DDL design enhances the efficiency of DML operations, while optimized DML statements improve system performance.
Best Practice Recommendations
When using DDL and DML, follow these best practices: use specific DDL statements to avoid unintended effects; employ transactions for multi-step data modifications to ensure consistency; avoid using DDL statements within stored procedures; execute DML operations cautiously to ensure data validity; regularly back up databases to prevent data loss. These practices help maintain database stability and data integrity.