SQLite 是遵循一套独特的称为语法的规则和准则。本教程列出了所有基本的 SQLite 语法,向您提供了一个 SQLite 快速入门。
有个重要的点值得注意,SQLite 是不区分大小写的,但也有一些命令是大小写敏感的,比如 GLOB 和 glob 在 SQLite 的语句中有不同的含义。
SQLite 注释是附加的注释,可以在 SQLite 代码中添加注释以增加其可读性,他们可以出现在任何空白处,包括在表达式内和其他 SQL 语句的中间,但它们不能嵌套。
SQL 注释以两个连续的 "-" 字符(ASCII 0x2d)开始,并扩展至下一个换行符(ASCII 0x0a)或直到输入结束,以先到者为准。
您也可以使用 C 风格的注释,以 "/*" 开始,并扩展至下一个 "*/" 字符对或直到输入结束,以先到者为准。SQLite的注释可以跨越多行。
1 |
<span class="pln">sqlite</span><span class="pun">>.</span><span class="pln">help </span><span class="pun">--</span> <span class="typ">This</span> <span class="kwd">is</span><span class="pln"> a single line comment</span> |
所有的 SQLite 语句可以以任何关键字开始,如 SELECT、INSERT、UPDATE、DELETE、ALTER、DROP 等,所有的语句以分号(;)结束。
1 2 3 4 5 |
<span class="pln">ANALYZE</span><span class="pun">;</span> <span class="kwd">or</span><span class="pln"> ANALYZE database_name</span><span class="pun">;</span> <span class="kwd">or</span><span class="pln"> ANALYZE database_name</span><span class="pun">.</span><span class="pln">table_name</span><span class="pun">;</span> |
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE CONDITION</span><span class="pun">-</span><span class="lit">1</span> <span class="pun">{</span><span class="pln">AND</span><span class="pun">|</span><span class="pln">OR</span><span class="pun">}</span><span class="pln"> CONDITION</span><span class="pun">-</span><span class="lit">2</span><span class="pun">;</span> |
1 |
<span class="pln">ALTER TABLE table_name ADD COLUMN column_def</span><span class="pun">...;</span> |
1 |
<span class="pln">ALTER TABLE table_name RENAME TO new_table_name</span><span class="pun">;</span> |
1 |
<span class="pln">ATTACH DATABASE </span><span class="str">'DatabaseName'</span> <span class="typ">As</span> <span class="str">'Alias-Name'</span><span class="pun">;</span> |
1 2 3 |
<span class="kwd">BEGIN</span><span class="pun">;</span> <span class="kwd">or</span> <span class="kwd">BEGIN</span><span class="pln"> EXCLUSIVE TRANSACTION</span><span class="pun">;</span> |
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE column_name BETWEEN val</span><span class="pun">-</span><span class="lit">1</span><span class="pln"> AND val</span><span class="pun">-</span><span class="lit">2</span><span class="pun">;</span> |
1 |
<span class="pln">COMMIT</span><span class="pun">;</span> |
1 2 |
<span class="pln">CREATE INDEX index_name ON table_name </span><span class="pun">(</span><span class="pln"> column_name COLLATE NOCASE </span><span class="pun">);</span> |
1 2 |
<span class="pln">CREATE UNIQUE INDEX index_name ON table_name </span><span class="pun">(</span><span class="pln"> column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">,...</span><span class="pln">columnN</span><span class="pun">);</span> |
1 2 3 4 5 6 7 8 |
<span class="pln">CREATE TABLE table_name</span><span class="pun">(</span><span class="pln"> column1 datatype</span><span class="pun">,</span><span class="pln"> column2 datatype</span><span class="pun">,</span><span class="pln"> column3 datatype</span><span class="pun">,</span> <span class="pun">.....</span><span class="pln"> columnN datatype</span><span class="pun">,</span><span class="pln"> PRIMARY KEY</span><span class="pun">(</span><span class="pln"> one </span><span class="kwd">or</span><span class="pln"> more columns </span><span class="pun">)</span> <span class="pun">);</span> |
1 2 3 4 5 6 7 |
<span class="pln">CREATE TRIGGER database_name</span><span class="pun">.</span><span class="pln">trigger_name BEFORE INSERT ON table_name FOR EACH ROW </span><span class="kwd">BEGIN</span><span class="pln"> stmt1</span><span class="pun">;</span><span class="pln"> stmt2</span><span class="pun">;</span> <span class="pun">....</span> <span class="kwd">END</span><span class="pun">;</span> |
1 2 |
<span class="pln">CREATE VIEW database_name</span><span class="pun">.</span><span class="pln">view_name AS SELECT statement</span><span class="pun">....;</span> |
1 2 3 |
<span class="pln">CREATE VIRTUAL TABLE database_name</span><span class="pun">.</span><span class="pln">table_name USING weblog</span><span class="pun">(</span><span class="pln"> access</span><span class="pun">.</span><span class="pln">log </span><span class="pun">);</span> <span class="kwd">or</span><span class="pln"> CREATE VIRTUAL TABLE database_name</span><span class="pun">.</span><span class="pln">table_name USING fts3</span><span class="pun">(</span> <span class="pun">);</span> |
1 |
<span class="pln">COMMIT</span><span class="pun">;</span> |
1 2 3 |
<span class="pln">SELECT COUNT</span><span class="pun">(</span><span class="pln">column_name</span><span class="pun">)</span><span class="pln"> FROM table_name WHERE CONDITION</span><span class="pun">;</span> |
1 2 |
<span class="pln">DELETE FROM table_name WHERE </span><span class="pun">{</span><span class="pln">CONDITION</span><span class="pun">};</span> |
1 |
<span class="pln">DETACH DATABASE </span><span class="str">'Alias-Name'</span><span class="pun">;</span> |
1 2 |
<span class="pln">SELECT DISTINCT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name</span><span class="pun">;</span> |
1 |
<span class="pln">DROP INDEX database_name</span><span class="pun">.</span><span class="pln">index_name</span><span class="pun">;</span> |
1 |
<span class="pln">DROP TABLE database_name</span><span class="pun">.</span><span class="pln">table_name</span><span class="pun">;</span> |
1 |
<span class="pln">DROP VIEW view_name</span><span class="pun">;</span> |
1 |
<span class="pln">DROP TRIGGER trigger_name</span> |
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE column_name EXISTS </span><span class="pun">(</span><span class="pln">SELECT </span><span class="pun">*</span><span class="pln"> FROM table_name </span><span class="pun">);</span> |
1 2 3 |
<span class="pln">EXPLAIN INSERT statement</span><span class="pun">...;</span> <span class="kwd">or</span><span class="pln"> EXPLAIN QUERY PLAN SELECT statement</span><span class="pun">...;</span> |
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE column_name GLOB </span><span class="pun">{</span><span class="pln"> PATTERN </span><span class="pun">};</span> |
1 2 3 4 |
<span class="pln">SELECT SUM</span><span class="pun">(</span><span class="pln">column_name</span><span class="pun">)</span><span class="pln"> FROM table_name WHERE CONDITION GROUP BY column_name</span><span class="pun">;</span> |
1 2 3 4 5 |
<span class="pln">SELECT SUM</span><span class="pun">(</span><span class="pln">column_name</span><span class="pun">)</span><span class="pln"> FROM table_name WHERE CONDITION GROUP BY column_name HAVING </span><span class="pun">(</span><span class="pln">arithematic </span><span class="kwd">function</span><span class="pln"> condition</span><span class="pun">);</span> |
1 2 |
<span class="pln">INSERT INTO table_name</span><span class="pun">(</span><span class="pln"> column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN</span><span class="pun">)</span><span class="pln"> VALUES </span><span class="pun">(</span><span class="pln"> value1</span><span class="pun">,</span><span class="pln"> value2</span><span class="pun">....</span><span class="pln">valueN</span><span class="pun">);</span> |
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE column_name IN </span><span class="pun">(</span><span class="pln">val</span><span class="pun">-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> val</span><span class="pun">-</span><span class="lit">2</span><span class="pun">,...</span><span class="pln">val</span><span class="pun">-</span><span class="pln">N</span><span class="pun">);</span> |
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE column_name LIKE </span><span class="pun">{</span><span class="pln"> PATTERN </span><span class="pun">};</span> |
1 2 3 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE column_name NOT IN </span><span class="pun">(</span><span class="pln">val</span><span class="pun">-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> val</span><span class="pun">-</span><span class="lit">2</span><span class="pun">,...</span><span class="pln">val</span><span class="pun">-</span><span class="pln">N</span><span class="pun">);</span> |
1 2 3 4 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE CONDITION ORDER BY column_name </span><span class="pun">{</span><span class="pln">ASC</span><span class="pun">|</span><span class="pln">DESC</span><span class="pun">};</span> |
1 2 3 4 5 6 7 |
<span class="pln">PRAGMA pragma_name</span><span class="pun">;</span> <span class="typ">For</span><span class="pln"> example</span><span class="pun">:</span><span class="pln"> PRAGMA page_size</span><span class="pun">;</span><span class="pln"> PRAGMA cache_size </span><span class="pun">=</span> <span class="lit">1024</span><span class="pun">;</span><span class="pln"> PRAGMA table_info</span><span class="pun">(</span><span class="pln">table_name</span><span class="pun">);</span> |
1 |
<span class="pln">RELEASE savepoint_name</span><span class="pun">;</span> |
1 2 3 |
<span class="pln">REINDEX collation_name</span><span class="pun">;</span><span class="pln"> REINDEX database_name</span><span class="pun">.</span><span class="pln">index_name</span><span class="pun">;</span><span class="pln"> REINDEX database_name</span><span class="pun">.</span><span class="pln">table_name</span><span class="pun">;</span> |
1 2 3 |
<span class="pln">ROLLBACK</span><span class="pun">;</span> <span class="kwd">or</span><span class="pln"> ROLLBACK TO SAVEPOINT savepoint_name</span><span class="pun">;</span> |
1 |
<span class="pln">SAVEPOINT savepoint_name</span><span class="pun">;</span> |
1 2 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name</span><span class="pun">;</span> |
1 2 3 |
<span class="pln">UPDATE table_name SET column1 </span><span class="pun">=</span><span class="pln"> value1</span><span class="pun">,</span><span class="pln"> column2 </span><span class="pun">=</span><span class="pln"> value2</span><span class="pun">....</span><span class="pln">columnN</span><span class="pun">=</span><span class="pln">valueN </span><span class="pun">[</span><span class="pln"> WHERE CONDITION </span><span class="pun">];</span> |
1 |
<span class="pln">VACUUM</span><span class="pun">;</span> |
1 2 3 4 5 |
<span class="pln">SELECT column1</span><span class="pun">,</span><span class="pln"> column2</span><span class="pun">....</span><span class="pln">columnN FROM table_name WHERE CONDITION</span><span class="pun">; from:<a href="http://www.runoob.com/sqlite/sqlite-syntax.html">http://www.runoob.com/sqlite/sqlite-syntax.html</a></span> |