-- DDL (Data Definition Language)
CREATE TABLE table_name (column1 datatype constraints, column2 datatype);
-- EN: Creates a new table with specified columns, datatypes, and constraints.
-- CH: 创建一个包含指定列、数据类型和约束的新表。
ALTER TABLE table_name ADD column_name datatype;
-- EN: Adds a new column to an existing table.
-- CH: 向现有表中添加一个新列。
DROP TABLE table_name;
-- EN: Permanently deletes a table and all its data.
-- CH: 永久删除一个表及其所有数据。
-- DML (Data Manipulation Language)
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
-- EN: Inserts a new row of data into specified columns of a table.
-- CH: 向表的指定列中插入一行新数据。
UPDATE table_name SET column1 = value1 WHERE condition;
-- EN: Modifies existing data in a table where the specified condition is met.
-- CH: 修改表中满足指定条件的现有数据。
DELETE FROM table_name WHERE condition;
-- EN: Removes rows from a table that match the specified condition.
-- CH: 从表中删除满足指定条件的行。
-- DQL (Data Query Language)
SELECT column1, column2 FROM table_name;
-- EN: Retrieves data from specific columns of a table.
-- CH: 从表的指定列中检索数据。
SELECT DISTINCT column_name FROM table_name;
-- EN: Retrieves only unique (non-duplicate) values from a column.
-- CH: 仅从某一列中检索唯一(不重复)的值。
SELECT column1 FROM table_name WHERE condition1;
-- EN: Filters and retrieves rows that match a specific condition.
-- CH: 过滤并检索满足特定条件的行。
SELECT column1 FROM table_name WHERE condition1 AND condition2;
-- EN: Filters and retrieves rows only when both condition1 and condition2 are true.
-- CH: 仅当 condition1 和 condition2 同时满足时,才过滤并检索行。
SELECT column1 FROM table_name WHERE condition1 OR condition2;
-- EN: Filters and retrieves rows when at least one of the conditions is true.
-- CH: 只要满足 condition1 或 condition2 中的任意一个,就过滤并检索行。
SELECT column1 FROM table_name ORDER BY column_name ASC LIMIT number;
-- EN: Sorts results in ascending order and limits the number of rows returned.
-- CH: 将结果按升序排序,并限制返回的行数。
SELECT column1 FROM table_name ORDER BY column_name DESC LIMIT number;
-- EN: Sorts results in descending order and limits the number of rows returned.
-- CH: 将结果按降序排序,并限制返回的行数。
SELECT MIN(column), MAX(column), AVG(column), COUNT(*) FROM table_name;
-- EN: Calculates the minimum, maximum, average, and total row count.
-- CH: 计算最小值、最大值、平均值和总行数。
SELECT column1, AVG(column2) FROM table_name GROUP BY column1;
-- EN: Groups rows by column1 and calculates the average of column2 for each group.
-- CH: 按 column1 分组,并计算每组中 column2 的平均值。
SELECT column1, AVG(column2) FROM table_name GROUP BY column1 HAVING condition;
-- EN: Groups rows and filters the aggregated group results using a condition.
-- CH: 对行进行分组,并使用条件过滤聚合后的分组结果。
-- JOINS (Relational Queries)
SELECT t1.col, t2.col FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id;
-- EN: Returns rows with matching values in both tables based on the join condition.
-- CH: 根据连接条件返回两张表中具有匹配值的行(内连接)。
SELECT t1.col, t2.col FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id;
-- EN: Returns all rows from the left table and matched rows from the right table.
-- CH: 返回左表中的所有行以及右表中匹配的行(左连接)。
-- DCL (Data Control Language)
GRANT privilege_list ON object_name TO user_name;
-- EN: Grants specific database permissions or privileges to a user.
-- CH: 授予用户特定的数据库权限。
REVOKE privilege_list ON object_name FROM user_name;
-- EN: Removes specific database permissions or privileges from a user.
-- CH: 撤销用户特定的数据库权限。