📜  SQLite语句或语法

📅  最后修改于: 2021-08-29 11:22:34             🧑  作者: Mango

SQLite之后是称为语法的一组规则和准则。

注意: SQLite不区分大小写,即SYNTAX和语法在SQLite语句中具有相同的含义。

SQLite语句:
所有SQLite语句均以分号(;)结尾。下面列出了所有基本的SQLite语法。

1. ANALYZE声明:

ANALYZE;
OR
ANALYZE dbname;
OR
ANALYZE dbname.tablename;

2. AND / OR条款:

SELECT col1, col2
FROM tablename
WHERE CONDITION-1 {AND|OR} CONDITION-2;

3. ALTER TABLE语句:

ALTER TABLE tablename ADD COLUMN coldef;
OR (Rename)
ALTER TABLE tablename RENAME TO newtablename;

4. ATTACH DATABASE语句:

ATTACH DATABASE 'DbName' As 'AliasName';

5.开始交易声明:

BEGIN;
OR
BEGIN EXCLUSIVE TRANSACTION;

6.条款之间:

SELECT col1, col2
FROM table_name
WHERE colname BETWEEN val1 AND val2;

7.提交交易声明:

COMMIT;

8. CREATE INDEX语句:

CREATE INDEX indexname
ON tablename ( colname COLLATE NOCASE );
UNIQUE INDEX 
CREATE UNIQUE INDEX indexname
ON tablename ( col1, col2, ...coln);

9. CREATE TABLE语句:

CREATE TABLE tablename(
  col1 datatype,
  col2 datatype,
  PRIMARY KEY( one or more columns )
);

10. CREATE TRIGGER语句:

CREATE TRIGGER databasename.triggername  
BEFORE INSERT ON tablename FOR EACH ROW
BEGIN  
  stmt1;  
  stmt2;
END;

11. CREATE VIEW语句:

CREATE VIEW databasename.viewname AS
SELECT statements;

12.创建虚拟表语句:

CREATE VIRTUAL TABLE dbname.tablename USING weblog( access.log );
OR
CREATE VIRTUAL TABLE dbname.tablename USING fts3( );

13. DELETE语句:

DELETE FROM tablename
WHERE {CONDITION};

14. DETACH DATABASE语句:

DETACH DATABASE 'AliasName';

15. DROP声明:

INDEX
DROP INDEX database_name.indexname;
TABLE 
DROP TABLE database_name.tablename;
VIEW 
DROP INDEX database_name.viewname;
TRIGGER 
DROP INDEX database_name.triggername;

16. EXPLAIN声明:

EXPLAIN INSERT statements;
or  
EXPLAIN QUERY PLAN SELECT statements;

17.插入INTO语句:

INSERT INTO tablename( col1, col2)
VALUES ( val1, val2);

18. PRAGMA声明:

PRAGMA pragmaname;

19. REINDEX声明:

REINDEX collationname;
REINDEX dbname.indexname;

20. ROLLBACK语句:

ROLLBACK;
OR
ROLLBACK TO SAVEPOINT savepointname;

21. SAVEPOINT语句:

SAVEPOINT savepointname;

22. RELEASE SAVEPOINT语句:

RELEASE savepointname;

23. SELECT语句:

SELECT col1, col2
FROM tablename;

24. UPDATE语句:

UPDATE tablename
SET col1 = val1, col2 = val2
[ WHERE  CONDITION ];

25. VACUUM语句:

VACUUM;