SQLite 语法

sqlite 语法

sqlite 遵循一套独特的称为语法的规则和准则。本教程列出了所有基本的 sqlite 语法。

 

1. 大小写敏感性

有个重要的点值得注意,sqlite 是不区分大小写的,但也有一些命令是大小写敏感的,比如 globglob 在 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;

下一节:sqlite 数据类型

sqlite教程

相关文章