sqlite> .help .auth ON|OFF Show authorizer callbacks .backup ?DB? FILE Backup DB (default "main") to FILE .bail on|off Stop after hitting an error. Default OFF .binary on|off Turn binary output on or off. Default OFF .changes on|off Show number of rows changed by SQL .check GLOB Fail if output since .testcase does not match .clone NEWDB Clone data into NEWDB from the existing database .databases List names and files of attached databases .dbinfo ?DB? Show status information about the database .dump ?TABLE? ... Dump the database in an SQL text format If TABLE specified, only dump tables matching LIKE pattern TABLE. .echo on|off Turn command echo on or off .eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN .exit Exit this program .explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic .fullschema ?--indent? Show schema and the content of sqlite_stat tables .headers on|off Turn display of headers on or off .help Show this message .import FILE TABLE Import data from FILE into TABLE .imposter INDEX TABLE Create imposter table TABLE on index INDEX .indexes ?TABLE? Show names of all indexes If TABLE specified, only show indexes for tables matching LIKE pattern TABLE. .limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT .log FILE|off Turn logging on or off. FILE can be stderr/stdout .mode MODE ?TABLE? Set output mode where MODE is one of: ascii Columns/rows delimited by 0x1F and 0x1E csv Comma-separated values column Left-aligned columns. (See .width) html HTML <table> code insert SQL insert statements for TABLE line One value per line list Values delimited by .separator strings quote Escape answers as for SQL tabs Tab-separated values tcl TCL list elements .nullvalue STRING Use STRING in place of NULL values .once FILENAME Output for the next SQL command only to FILENAME .open ?--new? ?FILE? Close existing database and reopen FILE The --new starts with an empty file .output ?FILENAME? Send output to FILENAME or stdout .print STRING... Print literal STRING .prompt MAIN CONTINUE Replace the standard prompts .quit Exit this program .read FILENAME Execute SQL in FILENAME .restore ?DB? FILE Restore content of DB (default "main") from FILE .save FILE Write in-memory database into FILE .scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off .schema ?PATTERN? Show the CREATE statements matching PATTERN Add --indent for pretty-printing .separator COL ?ROW? Change the column separator and optionally the row separator for both the output mode and .import .shell CMD ARGS... Run CMD ARGS... in a system shell .show Show the current values for various settings .stats ?on|off? Show stats or turn stats on or off .system CMD ARGS... Run CMD ARGS... in a system shell .tables ?TABLE? List names of tables If TABLE specified, only list tables matching LIKE pattern TABLE. .testcase NAME Begin redirecting output to 'testcase-out.txt' .timeout MS Try opening locked tables for MS milliseconds .timer on|off Turn SQL timer on or off .trace FILE|off Output each SQL statement as it is run .vfsinfo ?AUX? Information about the top-level VFS .vfslist List all available VFSes .vfsname ?AUX? Print the name of the VFS stack .width NUM1 NUM2 ... Set column widths for "column" mode Negative values right-justify
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
.show: .show 用来显示sqlite3中的一些设置:
sqlite> .show echo: off eqp: off explain: auto headers: on mode: column nullvalue: "" output: stdout colseparator: "|" rowseparator: "\n" stats: off width:
SQLite数据类型
SQLite数据库中存储的值是以下存储类之一:
存储类
描述
NULL
表示值为空(null)值。
INTEGER
表示值是一个有符号整数,根据值的大小存储在1,2,3,4,6或8个字节中。
REAL
表示值是一个浮点值,存储为8位IEEE浮点数。
TEXT
表示值是一个文本字符串,使用数据库编码(utf-8,utf-16be或utf-16le)存储
BLOB
表示值是一个数据块,与输入的数据完全相同。
SQLite 常用子句
ORDER BY SQLite 的 子句是用来基于一个或多个列按升序或降序顺序排列数据。
1 2 3 4 5 6 7 8 9
语法: ASC:表示升序,默认就是升序(可以不写) DESX:表示降序
SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC];
1
select * from student order by AGE DESC;
GROUP BY SQLite 的GROUP BY 子句用于与 SELECT 语句一起使用,来对相同的数据进行分组。在 SELECT语句中,GROUP BY 子句放在 WHERE 子句之后,放在 ORDER BY 子句之前。
1
select NAME, count(NAME) from student group by NAME;
Having SQLite 的having 子句使用指定条件来过滤将出现在最终结果中的分组结果。 在 WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建的分组上设置条件。
1
select NAME, count(NAME) from student group by NAME having count(NAME) > 1;
create table bound_test( ID int primary key not NULL, --主键约束 NAME TEXT NOT NULL,--not null 约束 AGE INT CHECK(AGE>0), --CHECK约束,AGE要大于0 WEIGHT INT UNIQUE, --UNIQUE 约束,体重不能相等(虽然不恰当哈) ADDRESS CHAR(50) DEFAULT 'shenzhen' --DEFAULT约束,默认为深圳 );
内连接(inner join)是应用程序中用的普遍的”连接”操作,它一般都是默认连接类型。内连接基于连接谓词将两张表(如 A 和 B)的列组合在一起,产生新的结果表。查询会将 A 表的每一行和 B 表的每一行进行比较,并找出满足连接谓词的组合。当连接谓词被满足,A 和 B 中匹配的行会按列组合(并排组合)成结果集中的一行。连接产生的结果集,可以定义为首先对两张表做笛卡尔积(交叉连接) – 将 A 中的每一行和 B 中的每一行组合,然后返回满足连接谓词的记录。实际上 SQL 产品会尽可能用其他方式去实现连接,笛卡尔积运算是非常没效率的.
CREATE TRIGGER audit_log AFTER INSERT ON COMPANY BEGIN INSERT INTO AUDIT(EMP_ID, ACTION_TYPE ,ENTRY_DATE) VALUES (new.ID, 'AFTER INSERT',datetime('now')); END;
1 2 3 4 5
CREATE TRIGGER befor_ins BEFORE INSERT ON COMPANY BEGIN INSERT INTO AUDIT(EMP_ID, ACTION_TYPE ,ENTRY_DATE) VALUES (new.ID, 'BEFORE INSERT', datetime('now')); END;
CREATE TABLE company( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
CREATE TABLE audit( EMP_ID INT NOT NULL, ACTION_TYPE TEXT NOT NULL, ENTRY_DATE TEXT NOT NULL );
CREATE TRIGGER audit_log AFTER INSERT ON COMPANY BEGIN INSERT INTO AUDIT(EMP_ID, ACTION_TYPE ,ENTRY_DATE) VALUES (new.ID, 'AFTER INSERT',datetime('now')); END;
CREATE TRIGGER befor_ins BEFORE INSERT ON COMPANY BEGIN INSERT INTO AUDIT(EMP_ID, ACTION_TYPE ,ENTRY_DATE) VALUES (new.ID, 'BEFORE INSERT', datetime('now')); END;
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Maxsu', 22, 'Haikou', 40000.00);
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Minsu', 28, 'Guangzhou', 35000.00);
如何列出/查看触发器?
可以使用查询语句从sqlite_master表中来查询列出/查看触发器。
1 2
SELECT name FROM sqlite_master WHERE type = 'trigger';