1. 查询语句 (SELECT)
基本查询
SELECT column1, column2, ...
FROM table_name;
带条件的查询
SELECT column1, column2, ...
FROM table_name
WHERE condition;
排序查询结果
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC|DESC, column2 ASC|DESC;
分组查询
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
2. 数据操作语句 (DML)
插入数据
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
更新数据
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
删除数据
DELETE FROM table_name
WHERE condition;
3. 数据定义语句 (DDL)
创建表
CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
column2 datatype,
...
);
修改表结构
ALTER TABLE table_name
ADD column_name datatype;
删除表
DROP TABLE table_name;
4. 高级查询
连接查询
SELECT table1.column1, table2.column2, ...
FROM table1
JOIN table2 ON table1.common_field = table2.common_field;
子查询
SELECT column1, column2, ...
FROM table_name
WHERE column1 IN (SELECT column1 FROM another_table WHERE condition);
聚合函数
SELECT COUNT(*), AVG(column1), SUM(column2)
FROM table_name
WHERE condition;
5. 事务控制语句
开始事务
START TRANSACTION;
提交事务
COMMIT;
回滚事务
ROLLBACK;
这些模板涵盖了日常数据库操作中最常用的SQL语句。根据具体需求,可以对这些模板进行组合和扩展,以实现更复杂的数据库操作。