数据库操作:
1. 创建数据库:
CREATE DATABASE database_name;
2. 使用数据库:
USE database_name;
3. 删除数据库:
DROP DATABASE database_name;
表操作:
4. 创建表:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
5. 查看表结构:
PRAGMA table_info(table_name);
6. 修改表结构(添加列):
ALTER TABLE table_name ADD COLUMN new_column datatype;
数据操作:
7. 插入数据:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
8. 查询数据:
SELECT column1, column2, ... FROM table_name WHERE condition;
9. 更新数据:
UPDATE table_name SET column1 = value1 WHERE condition;
10. 删除数据:
DELETE FROM table_name WHERE condition;
约束:
11. 主键约束:
CREATE TABLE table_name (
column1 INTEGER PRIMARY KEY,
...
);
12. 唯一约束:
CREATE TABLE table_name (
column1 INTEGER UNIQUE,
...
);
连接:
13. 内连接:
SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;
14. 左连接:
SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
条件:
15. WHERE 子句:
SELECT * FROM table_name WHERE condition;
16. AND/OR 运算符:
SELECT * FROM table_name WHERE condition1 AND condition2;
排序和限制:
17. ORDER BY 子句:
SELECT * FROM table_name ORDER BY column1 ASC, column2 DESC;
18. LIMIT 子句:
SELECT * FROM table_name LIMIT 10;
聚合函数:
19. COUNT:
SELECT COUNT(column) FROM table_name;
20. SUM:
SELECT SUM(column) FROM table_name;
21. AVG:
SELECT AVG(column) FROM table_name;
22. GROUP BY:
SELECT column, COUNT(*) FROM table_name GROUP BY column;
这只是 SQL 和 SQLite 的一小部分语法,你可以根据需要查阅 [SQLite 文档](https://www.sqlite.org/lang.html) 获取更详细的信息。SQLite 的语法和功能非常接近标准 SQL,因此学习 SQLite 也会为你提供广泛的 SQL 知识。
转载请注明出处:http://www.pingtaimeng.com/article/detail/9137/SQLite