SQL数据操作语言(DML)是数据库管理和数据变更的核心工具。无论是初学者还是数据库专家,了解DML命令如INSERT、SELECT、UPDATE和DELETE的功能、语法示例和事务控制都是极其重要的。
SQL数据操作语言允许在数据库实例中添加、删除、更新和选择数据。DML管理数据库中的所有数据修改。DML SQL包括以下命令:
DML主要分为两种类型:
首先,需要创建一个表格,用于下面的示例。使用以下命令创建表格:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
department_id INT,
salary DECIMAL(10, 2),
FOREIGN KEY (department_id) REFERENCES departments(department_id) ON DELETE CASCADE
);
CREATE TABLE employees_backup (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
department_id INT,
salary DECIMAL(10, 2)
);
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);
现在让详细探讨DML命令。
可以使用SQL提供的INSERT语句将数据插入表中。使用INSERT语句,可以:
单行插入示例:
INSERT INTO departments (department_id, department_name)
VALUES (1, 'HR');
INSERT INTO employees (employee_id, first_name, last_name, email, department_id, salary)
VALUES (1, 'John', 'Doe', '[email protected]', 1, 50000.00);
多行插入示例:
INSERT INTO departments (department_id, department_name)
VALUES
(2, 'Finance'),
(3, 'IT'),
(4, 'Sales'),
(5, 'Marketing'),
(6, 'Support'),
(8, 'Content');
INSERT INTO employees (employee_id, first_name, last_name, email, department_id, salary)
VALUES
(2, 'Jane', 'Doe', '[email protected]', 2, 55000.00),
(3, 'Mike', 'Smith', '[email protected]', 1, 60000.00),
(4, 'Anna', 'Taylor', '[email protected]', 3, 70000.00),
(5, 'Bob', 'Brown', '[email protected]', 4, 45000.00),
(6, 'Alice', 'White', '[email protected]', 5, 48000.00),
(7, 'Charlie', 'Black', '[email protected]', 6, 47000.00);
从另一个表复制行示例:
INSERT INTO employees_backup
SELECT * FROM employees
WHERE department_id = 1;
要查询表中的数据,可以使用SQL SELECT语句,该语句具有对数据进行分组、连接表、选择行、选择列和执行简单计算的语法。
SELECT column1, column2, ...
FROM table_name;
要检索所有列,请使用SELECT *。
SELECT first_name, last_name
FROM employees;
完整的查询语法包括:
SELECT DISTINCT column1, AGG_FUNC(column_or_expression), ...
FROM table1
JOIN table2 ON table1.column = table2.column
WHERE condition
GROUP BY column
HAVING condition
ORDER BY column ASC|DESC
LIMIT count OFFSET count;
查询执行顺序:
多子句示例:
SELECT department_name, AVG(salary) AS avg_salary
FROM employees
JOIN departments ON employees.department_id = departments.department_id
WHERE salary > 50000
GROUP BY department_name
HAVING AVG(salary) > 60000
ORDER BY avg_salary DESC
LIMIT 10;
要更改表中的现有数据,可以使用UPDATE语句。
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
单行更新示例:
UPDATE employees
SET last_name = 'Blue'
WHERE employee_id = 7;
使用子查询在UPDATE中:
UPDATE employees
SET salary = (SELECT MAX(salary) FROM employees)
WHERE department_id = 3;
多行更新示例:
UPDATE employees
SET salary = salary * 1.1
WHERE department_id = 2;
要删除表中的一行或多行,可以使用DELETE语句。
DELETE FROM table_name
WHERE condition;
单行删除示例:
DELETE FROM employees
WHERE employee_id = 3;
多行删除示例:
DELETE FROM employees
WHERE department_id IN (5, 6, 7);
自动从相关表中删除行(等效):
DELETE FROM departments
WHERE department_id = 4;
DML用于执行解释型数据查询、数据操作,并且可以与事务控制集成以确保数据完整性。
任何由DML语句所做的修改都被视为事务,必须通过事务控制语言(TCL)语句控制以确保数据的完整性和一致性。TCL命令包括COMMIT、ROLLBACK和SAVEPOINT。