Detailed Explanation and Application Scenarios of DDL, DML, DQL, and DCL Database Languages
Detailed Explanation and Application Scenarios of DDL, DML, DQL, and DCL Database Languages
I. Conceptual Description
Database operation languages are the core components of SQL, divided into four categories based on functionality:
- DDL (Data Definition Language): Used to define or modify database structures (such as tables, indexes), including statements like
CREATE,ALTER, andDROP. - DML (Data Manipulation Language): Used to manipulate data within tables (insert, update, delete), such as
INSERT,UPDATE, andDELETE. - DQL (Data Query Language): Specifically refers to the
SELECTstatement, used for querying data. - DCL (Data Control Language): Controls data access permissions, such as
GRANTfor authorization andREVOKEfor revoking permissions.
II. DDL Detailed Explanation and Examples
-
CREATE: Creates database objects.
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, salary DECIMAL(10,2) );- Key Points: Defines column data types and constraints (such as
PRIMARY KEY,NOT NULL).
- Key Points: Defines column data types and constraints (such as
-
ALTER: Modifies table structure.
ALTER TABLE employees ADD COLUMN department VARCHAR(20); ALTER TABLE employees MODIFY COLUMN salary INT; -- Modifies data type- Note: Modifying data types may lead to data loss; caution is required.
-
DROP: Deletes objects.
DROP TABLE employees; -- Permanently deletes both data and structure- Difference from
TRUNCATE(DML):DROPdeletes the structure, whileTRUNCATEonly clears data.
- Difference from
III. DML Detailed Explanation and Examples
-
INSERT: Inserts data.
INSERT INTO employees (id, name, salary) VALUES (1, 'Alice', 50000);- Batch insertion is more efficient:
INSERT INTO employees VALUES (2, 'Bob', 60000), (3, 'Charlie', 70000);
- Batch insertion is more efficient:
-
UPDATE: Modifies data.
UPDATE employees SET salary = 55000 WHERE id = 1;- Must use
WHEREto limit the scope; otherwise, the entire table will be updated.
- Must use
-
DELETE: Deletes data.
DELETE FROM employees WHERE id = 1;- Comparison with
TRUNCATE:DELETEremoves row by row and can be rolled back, whileTRUNCATEquickly clears data and cannot be rolled back.
- Comparison with
IV. DQL Core Syntax and Optimization
-
Basic Queries:
SELECT name, salary FROM employees WHERE salary > 50000 ORDER BY salary DESC;- Execution order:
FROM→WHERE→SELECT→ORDER BY.
- Execution order:
-
Multi-Table Joins:
SELECT e.name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.id;- Use aliases to simplify code, and be cautious with join conditions to avoid Cartesian products.
-
Aggregate Functions and Grouping:
SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department HAVING AVG(salary) > 60000; -- HAVING filters results after grouping
V. DCL Permission Management
-
GRANT: Grants permissions.
GRANT SELECT, INSERT ON employees TO user1;- Can be refined to the column level:
GRANT UPDATE (name) ON employees TO user1;
- Can be refined to the column level:
-
REVOKE: Revokes permissions.
REVOKE INSERT ON employees FROM user1;
VI. Application Scenario Summary
- DDL: Suitable for database design phases (e.g., creating tables) and structural changes (e.g., version upgrades).
- DML: Frequently used in business logic (e.g., user registration, order updates).
- DQL: Core tool for data analysis and report generation.
- DCL: Permission management in multi-user systems (e.g., distinguishing between administrators and regular users).
VII. Precautions
- DDL typically auto-commits transactions; data should be backed up before execution.
- DML operations should be combined with transactions (
BEGIN TRANSACTION,COMMIT) to ensure consistency. - Complex DQL queries should utilize indexes to avoid full table scans.