sqlite 学习笔记(二 )

本次

1、sqlite 只有5种本地类型(integer ,real , text , blob , null);

2、创建表的几种不同的配置:

     a、CREATE TABLE my_table (id integer primary key , firstname text , lastname text , age integer);

     b、CREATE TABLE my_table (id integer primary key , firstname text NOT NULL,lastname text NOT NULL , age integer);

          如果设置了NOT NULL 参数,则再INSERT 数据的时候一定要设置不为空的值,否则会报错。

     c、CREATE TABLE my_table (id integer primary key , firstname text NOT NULL DEFAULT 'mike' , lastname text NOT NULL 'li' , age integer DEFAULT 23);

          可以设置默认值,当INSERT时没有指定字段的值,则使用默认的值。

    另外还有 collate nocase 可以指定排序不区分大小写;unique 指定唯一。

3、删除表 使用 SQL : DROP TABLE my_table;

4、选择查询SQL:

     a、select * from foods;

     b、select * from foods where type_id = 4;

     c、select name from foods where type_id = 4;

     d、select name from foods where type_id = 4 [and|or] name =‘pizza’;

     e、 select * from foods where name >='B' and name <'D'; 使用这种方式可以筛选首字母 大于等于B 小于D的行;

     f、select  * from foods where name is null; 查询name未定义的行;不能使用 name = null , name='null' , name =0 等限制;也可以 使用 is not null;

     g、select * from foods where id like '%3%'; 查询 id 中 包含 3的行;

     h、select * from foods where id between 12 and 20; 包含头尾

     i、select * from foods where id in (12 , 13, 14); 查询id 为 ()范围内的 行,相反的用法有 not in;

     j、select length(name),name,lower(name) from foods where type_id =4; sqlite 中可以使用 length lower 等方法处理筛选出的字段,但是其他数据库中的部分方法是用不了的。

     k、另外 select count(*) from foods ; 可以求出 筛选出的行数;

     l、 select count(*) ,avg(type_id) from foods where id>4 and id<9; avg 求平均数;sum 求和;

     m、select * from foods where name like '%A' order by name; order by 用来排序;再列名后加上 desc 表示反转排序结果。

     n、select sum(id),count(id),type_id from foods group by type_id order by sum(id); group by 分组;

     o、select type_id , max(id) ,max(length(name)) from foods group by type_id;  max ,min求最大最小值;

     p、 select distinct type_id from foods;求不同的type_id;

     s、select distinct type_id from foods limit 2; limit 限制显示的条数 , offset 表示从某一行开始选择。(第一行为 0);

5、删除记录:

    a、delete from foods where id = 3;

    b、delete from foods ;删除foods中的所有记录;

6、更新记录:

    a、update foods set name = 'kfc' where id = 3;  set 后面可以接多个字段,其间用 ‘,’分开;

         不加where 每个行中的字段都会被修改;

         update 修改的行 由where 限制;

    b、update foods set type_id= type_id +1 where id = 3;

    c、update foods set name = case when type_id =1 then 1          when type_id =2 then 2        when type_id =3 then 3       end       where type_id <3;

         使用case end模块可以筛选需要跟新的记录;

7、添加字段:

    a、alter table show add column id integer; 为表show 添加一个新的字段 id;

    b、alter table tablename rename to tb_two;

 

posted @ 2012-07-10 19:34  lipeil  阅读(351)  评论(0编辑  收藏  举报