sxd@sxd-ThinkPad-T400:~/Databases$ sqlite3 test.db SQLite version 3.7.9 2011-11-01 00:52:41 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite>
4. 查看数据库
sqlite> .database seq name file --- --------------- ---------------------------------------------------------- 0 main /home/sxd/Databases/test.db sqlite>
5.创建数据表
sqlite> create table student(name varchar(10), age smallint);
sqlite> .mode insert sqlite> select * from dept; INSERT INTO table VALUES(10,'ACCOUNTING','NEW YORK'); INSERT INTO table VALUES(20,'RESEARCH','DALLAS'); INSERT INTO table VALUES(30,'SALES','CHICAGO'); INSERT INTO table VALUES(40,'OPERATIONS','BOSTON');
sqlite> .mode line sqlite> select * from dept; DEPTNO = 10 DNAME = ACCOUNTING LOC = NEW YORK
DEPTNO = 20 DNAME = RESEARCH LOC = DALLAS
DEPTNO = 30 DNAME = SALES LOC = CHICAGO
DEPTNO = 40 DNAME = OPERATIONS LOC = BOSTON
sqlite> .mode tabs sqlite> select * from dept; 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON
create table emp_bak select * from EMP;不能在sqlite中使用
插入记录 insert into table_name values (field1, field2, field3...); 查询 select * from table_name;查看table_name表中所有记录; select * from table_name where field1='xxxxx'; 查询符合指定条件的记录;
select ..... from table_name[,table_name2,...] where ..... group by.... having .... order by ...
select ..... from table_name inner join | left outer join | right outer join table_name2 on ... where ..... group by.... having .... order by ...
子查询: select * from EMP m where SAL> (select avg(SAL) from EMP where DEPTNO=m.DEPTNO);
支持case when then 语法 update EMP set SAL= ( case when DEPTNO=10 and JOB='MANAGER' then SAL*1.1 when DEPTNO=20 and JOB='CLERK' then SAL*1.2 when DEPTNO=30 then SAL*1.1 when DEPTNO=40 then SAL*1.2 else SAL END );
select ENAME, case DEPTNO when 10 then '后勤部' when 20 then '财务部' when 30 then '内务部门' else '其他部门' end as dept from EMP;
支持关联子查询 in后面的语法中可以有limit(mysql不可以) select * from emp e where e.EMPNO in ( select empno from EMP where deptno=e.DEPTNO order by SAL desc limit 0,2 );
支持表和表之间的数据合并等操作 union 去重复 union all 不去掉重复 select deptno from emp union select deptno from dept;
select deptno from emp union all select deptno from dept;
在列名前加distinct也是去重复 sqlite> select distinct deptno from emp;
删除 delete from table_name where ...
删除表 drop table_name; 删除表; drop index_name; 删除索引;
修改 update table_name set xxx=value[, xxx=value,...] where ...
建立索引
如果资料表有相当多的资料,我们便会建立索引来加快速度。好比说:
create index film_title_index on film(title); 意思是针对film资料表的name字段,建立一个名叫film_name_index的索引。这个指令的语法为
CREATE [ UNIQUE ] NONCLUSTERED INDEX index_name ON { table | view } ( column [ ASC | DESC ] [ ,...n ] ) create index index_name on table_name(field_to_be_indexed); 一旦建立了索引,sqlite3会在针对该字段作查询时,自动使用该索引。这一切的操作都是在幕后自动发生的,无须使用者特别指令。
其他sqlite的特别用法
sqlite可以在shell底下直接执行命令: sqlite3 film.db "select * from emp;"
输出 HTML 表格: sqlite3 -html film.db "select * from film;" 将数据库「倒出来」:
sqlite> begin; sqlite> insert into aaaa values('aaa','333'); sqlite> select * from aaaa; 2|sdfds sdfsd|9 2012-12-12|13:13:13 aaa|333 sqlite> rollback; sqlite> select * from aaaa; 2|sdfds sdfsd|9 2012-12-12|13:13:13
创建和删除视图 CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition DROP VIEW view_name
create view e as select avg(SAL) avgsal,DEPTNO from EMP group by DEPTNO;
select ENAME,EMP.DEPTNO,SAL,avgsal from EMP inner join e on EMP.DEPTNO=e.DEPTNO where SAL>avgsal;
练习员工表:
PRAGMA foreign_keys=OFF; BEGIN TRANSACTION; CREATE TABLE DEPT ( DEPTNO int(2) not null, DNAME varchar(14), LOC varchar(13) ); INSERT INTO "DEPT" VALUES(10,'ACCOUNTING','NEW YORK'); INSERT INTO "DEPT" VALUES(20,'RESEARCH','DALLAS'); INSERT INTO "DEPT" VALUES(30,'SALES','CHICAGO'); INSERT INTO "DEPT" VALUES(40,'OPERATIONS','BOSTON'); CREATE TABLE EMP ( EMPNO int(4) not null, ENAME varchar(10), JOB varchar(9), MGR int(4), HIREDATE date, SAL int(7 ), COMM int(7 ), DEPTNO int(2) ); INSERT INTO "EMP" VALUES(7369,'SMITH','CLERK',7902,'17-12-1980',800,NULL,20); INSERT INTO "EMP" VALUES(7499,'ALLEN','SALESMAN',7698,'20-02-1981',1600,300,30); INSERT INTO "EMP" VALUES(7521,'WARD','SALESMAN',7698,'22-02-1981',1250,500,30); INSERT INTO "EMP" VALUES(7566,'JONES','MANAGER',7839,'02-04-1981',2975,NULL,20); INSERT INTO "EMP" VALUES(7654,'MARTIN','SALESMAN',7698,'28-09-1981',1250,1400,30); INSERT INTO "EMP" VALUES(7698,'BLAKE','MANAGER',7839,'01-05-1981',2850,NULL,30); INSERT INTO "EMP" VALUES(7782,'CLARK','MANAGER',7839,'09-06-1981',2450,NULL,10); INSERT INTO "EMP" VALUES(7788,'SCOTT','ANALYST',7566,'19-04-1987',3000,NULL,20); INSERT INTO "EMP" VALUES(7839,'KING','PRESIDENT',NULL,'17-11-1981',5000,NULL,10); INSERT INTO "EMP" VALUES(7844,'TURNER','SALESMAN',7698,'08-09-1981',1500,0,30); INSERT INTO "EMP" VALUES(7876,'ADAMS','CLERK',7788,'23-05-1987',1100,NULL,20); INSERT INTO "EMP" VALUES(7900,'JAMES','CLERK',7698,'03-12-1981',950,NULL,30); INSERT INTO "EMP" VALUES(7902,'FORD','ANALYST',7566,'03-12-1981',3000,NULL,20); INSERT INTO "EMP" VALUES(7934,'MILLER','CLERK',7782,'23-01-1982',1300,NULL,10); CREATE TABLE SALGRADE ( GRADE int, LOSAL int, HISAL int ); INSERT INTO "SALGRADE" VALUES(1,700,1200); INSERT INTO "SALGRADE" VALUES(2,1201,1400); INSERT INTO "SALGRADE" VALUES(3,1401,2000); INSERT INTO "SALGRADE" VALUES(4,2001,3000); INSERT INTO "SALGRADE" VALUES(5,3001,9999); COMMIT;