SQLite 語法
sqlite 語法
sqlite 遵循一套獨特的稱為語法的規則和準則。本教程列出了所有基本的 sqlite 語法。
1. 大小寫敏感性
有個重要的點值得注意,sqlite 是不區分大小寫的,但也有一些命令是大小寫敏感的,比如 glob 和 glob 在 sqlite 的語句中有不同的含義。
2. 注釋
sqlite 注釋是附加的注釋,可以在 sqlite 代碼中添加注釋以增加其可讀性,他們可以出現在任何空白處,包括在表達式內和其他 sql 語句的中間,但它們不能嵌套。
sql 注釋以兩個連續的 "-" 字符(ascii 0x2d)開始,并擴展至下一個換行符(ascii 0x0a)或直到輸入結束,以先到者為準。
您也可以使用 c 風格的注釋,以 "/*" 開始,并擴展至下一個 "*/" 字符對或直到輸入結束,以先到者為準。sqlite的注釋可以跨越多行。
sqlite>.help -- 這是一個簡單的注釋
3. sqlite 語句
所有的 sqlite 語句可以以任何關鍵字開始,如 select、insert、update、delete、alter、drop 等,所有的語句以分號 ; 結束。
4. sqlite analyze 語句:
analyze; or analyze database_name; or analyze database_name.table_name;
5. sqlite and/or 子句:
select column1, column2....columnn from table_name where condition-1 {and|or} condition-2;
6. sqlite alter table 語句:
alter table table_name add column column_def...;
7. sqlite alter table 語句(rename):
alter table table_name rename to new_table_name;
8. sqlite attach database 語句:
attach database 'databasename' as 'alias-name';
9. sqlite begin transaction 語句:
begin; or begin exclusive transaction;
10. sqlite between 子句:
select column1, column2....columnn from table_name where column_name between val-1 and val-2;
11. sqlite commit 語句:
commit;
12. sqlite create index 語句:
create index index_name on table_name ( column_name collate nocase );
13. sqlite create unique index 語句:
create unique index index_name on table_name ( column1, column2,...columnn);
14. sqlite create table 語句:
create table table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnn datatype, primary key( one or more columns ) );
15. sqlite create trigger 語句:
create trigger database_name.trigger_name before insert on table_name for each row begin stmt1; stmt2; .... end;
16. sqlite create view 語句:
create view database_name.view_name as select statement....;
17. sqlite create virtual table 語句:
create virtual table database_name.table_name using weblog( access.log ); or create virtual table database_name.table_name using fts3( );
18. sqlite commit transaction 語句:
commit;
19. sqlite count 子句:
select count(column_name) from table_name where condition;
20. sqlite delete 語句:
delete from table_name where {condition};
21. sqlite detach database 語句:
detach database 'alias-name';
22. sqlite distinct 子句:
select distinct column1, column2....columnn from table_name;
23. sqlite drop index 語句:
drop index database_name.index_name;
24. sqlite drop table 語句:
drop table database_name.table_name;
25. sqlite drop view 語句:
drop view view_name;
26. sqlite drop trigger 語句:
drop trigger trigger_name
27. sqlite exists 子句:
select column1, column2....columnn from table_name where column_name exists (select * from table_name );
28. sqlite explain 語句:
explain insert statement...; or explain query plan select statement...;
29. sqlite glob 子句:
select column1, column2....columnn from table_name where column_name glob { pattern };
30. sqlite group by 子句:
select sum(column_name) from table_name where condition group by column_name;
31. sqlite having 子句:
select sum(column_name) from table_name where condition group by column_name having (arithematic function condition);
32. sqlite insert into 語句:
insert into table_name( column1, column2....columnn) values ( value1, value2....valuen);
33. sqlite in 子句:
select column1, column2....columnn from table_name where column_name in (val-1, val-2,...val-n);
34. sqlite like 子句:
select column1, column2....columnn from table_name where column_name like { pattern };
35. sqlite not in 子句:
select column1, column2....columnn from table_name where column_name not in (val-1, val-2,...val-n);
36. sqlite order by 子句:
select column1, column2....columnn from table_name where condition order by column_name {asc|desc};
37. sqlite pragma 語句:
pragma pragma_name; for example: pragma page_size; pragma cache_size = 1024; pragma table_info(table_name);
38. sqlite release savepoint 語句:
release savepoint_name;
39. sqlite reindex 語句:
reindex collation_name; reindex database_name.index_name; reindex database_name.table_name;
40. sqlite rollback 語句:
rollback; or rollback to savepoint savepoint_name;
41. sqlite savepoint 語句:
savepoint savepoint_name;
42. sqlite select 語句:
select column1, column2....columnn from table_name;
43. sqlite update 語句:
update table_name set column1 = value1, column2 = value2....columnn=valuen [ where condition ];
44. sqlite vacuum 語句:
vacuum;
45. sqlite where 子句:
select column1, column2....columnn from table_name where condition;