SQLite 语法
SQLite遵循一组称为语法的规则和准则。本章列出了所有基本的SQLite语法。
大小写敏感性
需要注意的重要一点是SQLite是不区分大小写的,即在SQLite语句中,诸如 GLOB 和 glob 的条件具有相同的含义。
注释
SQLite注释是可在SQLite代码中添加的额外注释,以增加其可读性。它们可以出现在任何地方;包括表达式中的空格以及其他SQL语句的中间位置,但不能嵌套。
SQL注释以连续两个”-“字符(ASCII 0x2d)开始,并延伸到包括下一个换行符(ASCII 0x0a)或输入结束,以先到者为准。
您还可以使用C风格注释,以”/“开始,并延伸到包括下一个”/”字符对或输入结束,以先到者为准。 C风格注释可以跨多行。
sqlite> .help -- This is a single line comment
SQLite 语句
所有的SQLite语句都以关键字SELECT、INSERT、UPDATE、DELETE、ALTER、DROP等开头,并且所有语句都以分号(;)结尾。
SQLite ANALYZE语句
ANALYZE;
or
ANALYZE database_name;
or
ANALYZE database_name.table_name;
SQLite AND/OR 语句
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION-1 {AND|OR} CONDITION-2;
SQLite ALTER TABLE语句
ALTER TABLE table_name ADD COLUMN column_def...;
SQLite ALTER TABLE语句(重命名)
ALTER TABLE table_name RENAME TO new_table_name;
SQLite ATTACH DATABASE语句
ATTACH DATABASE 'DatabaseName' As 'Alias-Name';
SQLite BEGIN TRANSACTION 语句
BEGIN;
or
BEGIN EXCLUSIVE TRANSACTION;
SQLite BETWEEN语句
SELECT column1, column2....columnN
FROM table_name
WHERE column_name BETWEEN val-1 AND val-2;
SQLite COMMIT语句
COMMIT;
SQLite CREATE INDEX语句
CREATE INDEX index_name
ON table_name ( column_name COLLATE NOCASE );
SQLite创建唯一索引语句
CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...columnN);
SQLite创建表语句
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
SQLite CREATE TRIGGER语句
CREATE TRIGGER database_name.trigger_name
BEFORE INSERT ON table_name FOR EACH ROW
BEGIN
stmt1;
stmt2;
....
END;
SQLite创建视图语句
CREATE VIEW database_name.view_name AS
SELECT statement....;
SQLite创建虚拟表语句
CREATE VIRTUAL TABLE database_name.table_name USING weblog( access.log );
or
CREATE VIRTUAL TABLE database_name.table_name USING fts3( );
SQLite COMMIT TRANSACTION语句
COMMIT;
SQLite COUNT语句
SELECT COUNT(column_name)
FROM table_name
WHERE CONDITION;
SQLite DELETE语句
DELETE FROM table_name
WHERE {CONDITION};
SQLite DETACH DATABASE语句
DETACH DATABASE 'Alias-Name';
SQLite DISTINCT语句
SELECT DISTINCT column1, column2....columnN
FROM table_name;
SQLite DROP INDEX语句
DROP INDEX database_name.index_name;
SQLite DROP TABLE语句
DROP TABLE database_name.table_name;
SQLite DROP VIEW语句
DROP INDEX database_name.view_name;
SQLite DROP TRIGGER语句
DROP INDEX database_name.trigger_name;
SQLite EXISTS语句
SELECT column1, column2....columnN
FROM table_name
WHERE column_name EXISTS (SELECT * FROM table_name );
SQLite EXPLAIN语句
EXPLAIN INSERT statement...;
or
EXPLAIN QUERY PLAN SELECT statement...;
SQLite GLOB语句
SELECT column1, column2....columnN
FROM table_name
WHERE column_name GLOB { PATTERN };
SQLite GROUP BY 语句
SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name;
SQLite HAVING语句
SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithematic function condition);
SQLite插入语句
INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);
SQLite IN 语句
SELECT column1, column2....columnN
FROM table_name
WHERE column_name IN (val-1, val-2,...val-N);
SQLite Like语句
SELECT column1, column2....columnN
FROM table_name
WHERE column_name LIKE { PATTERN };
SQLite NOT IN 语句
SELECT column1, column2....columnN
FROM table_name
WHERE column_name NOT IN (val-1, val-2,...val-N);
SQLite ORDER BY语句
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION
ORDER BY column_name {ASC|DESC};
SQLite PRAGMA语句
PRAGMA pragma_name;
For example:
PRAGMA page_size;
PRAGMA cache_size = 1024;
PRAGMA table_info(table_name);
SQLite RELEASE SAVEPOINT 语句
RELEASE savepoint_name;
SQLite REINDEX语句
REINDEX collation_name;
REINDEX database_name.index_name;
REINDEX database_name.table_name;
SQLite ROLLBACK 语句
ROLLBACK;
or
ROLLBACK TO SAVEPOINT savepoint_name;
SQLite SAVEPOINT语句
SAVEPOINT savepoint_name;
SQLite SELECT语句
SELECT column1, column2....columnN
FROM table_name;
SQLite UPDATE语句
UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ];
SQLite VACUUM语句
VACUUM;
SQLite WHERE语句
SELECT column1, column2....columnN
FROM table_name
WHERE CONDITION;