1. 数据定义语言(DDL):
1.1 创建表:
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
salary REAL
);
1.2 修改表结构:
ALTER TABLE employees
ADD COLUMN department TEXT;
1.3 删除表:
DROP TABLE employees;
2. 数据操作语言(DML):
2.1 插入数据:
INSERT INTO employees (name, age, salary)
VALUES ('John', 25, 50000),
('Alice', 28, 60000),
('Bob', 22, 55000);
2.2 更新数据:
UPDATE employees
SET salary = 58000
WHERE name = 'John';
2.3 删除数据:
DELETE FROM employees
WHERE name = 'Bob';
3. 数据查询语言(DQL):
3.1 查询所有数据:
SELECT * FROM employees;
3.2 条件查询:
SELECT * FROM employees
WHERE age > 25;
3.3 排序:
SELECT * FROM employees
ORDER BY salary DESC;
3.4 聚合函数:
SELECT AVG(salary) AS average_salary,
MAX(age) AS max_age
FROM employees;
3.5 分组:
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department;
3.6 连接表:
SELECT employees.name, departments.department_name
FROM employees
JOIN departments ON employees.department_id = departments.id;
4. 事务:
BEGIN TRANSACTION;
-- 执行一系列 SQL 语句
COMMIT; -- 或 ROLLBACK;
5. 索引:
5.1 创建索引:
CREATE INDEX idx_name ON employees (name);
5.2 删除索引:
DROP INDEX idx_name;
以上是一些基本的 SQLite 语法示例。需要注意的是,SQLite 是一种嵌入式数据库,语法相对简单,但仍然支持大多数标准 SQL 操作。在实际应用中,可以根据具体的需求深入学习更多的 SQLite 语法和功能。SQLite 的官方文档提供了详细的语法说明和示例,可作为参考:
- [SQLite Documentation](https://www.sqlite.org/docs.html)
转载请注明出处:http://www.pingtaimeng.com/article/detail/14161/SQLite