您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 汕尾分类信息网,免费分类信息发布

sqlite基本sql语句使用

2025/12/26 9:27:27发布34次查看
sqlite基本sql语句使用 一,sqlite常见的数据类型 sqlite是无类型的。 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中,无论这列声明的数据类型是什么(只有自动递增integer primary key才有用)。对于sqlite来说对字段不指定类型是完全有
sqlite基本sql语句使用
一,sqlite常见的数据类型
sqlite是无类型的。 这意味着你可以保存任何类型的数据到你所想要保存的任何表的任何列中,无论这列声明的数据类型是什么(只有自动递增integer primary key才有用)。对于sqlite来说对字段不指定类型是完全有效的。 即使sqlite允许忽略数据类型,但是仍然建议在你的create table语句中指定数据类型。 因为数据类型对于你和其他的程序员交
流,或者你准备换掉你的数据库引擎是非常有用的。sqlite只支持常见的5种存储类,
   null
   integer  --整型
   real      --浮点数
   text      --文本
   blob      --大二进制对象
以下定义的数据类型都会转到相应的存储类中。
create  table  tab(            --注意其中的注释方式 
  a  varchar(10),          --长度不固定且其最大长度为n的字符串
  b  nvarchar(15),
c  text,                  --二进制对象
d  integer,              --带符号的整型,具体取决于存入数字的范围大小
e  float,            
f  boolean,   
g  clob,                 --使用char来保存数据
      h  blob,                 --使用二进制对象保存数据,如保存位图
      i  timestamp,
      j  numberic(10,5),
      k  varying character(24),
      l  national varying character(16),    //
      j  real                  --浮点数字,存储为8-byte ieee浮点数
);
二,        基本的数据操作
1,建立表
create  table  admin(
username text,
age integer);
2,插入数据
    insert into 表名(字段列表) values(值列表);
    例如:insert  into admin values(‘song’,25);
3,查询
    select 字段名 from 表名;
    select * from admin;
    select  distinct  field  from  table_name;(distinct去掉重复项,将列中各字段值单个列出)
4,删除数据
    delete from 表名 where 条件子句。
    delete from admin form where username=’song’;
5,修改
    update 表名 set 字段名=值 where 条件子句。
    update admin set username=’zhang’,age=24 where username=’song’ and age=25;
6,按条件分组
   select * from 表名 where 条件子句 group by 分组子句 having …order by排子句
    例如:
       select * from admin;
       select * from admin order by id desc(降序) | asc(升序);
       select username from admin group by username having count(*)>1;
7,多条件查询语句
    select 字段名 from 表名 where  子句1 按 子句二
    select * from admin where username=’song’ and age=24;
    select * from table_name where field in (‘val1’ , ’val2’ , ‘val3’ );
    select * from table_name where field between  val1 and val2;
    select * from admin limit 5;        --限制输出数据记录数量
8,多条件排序
   select 字段名 from 表名 order by 字段1 (desc),字段2(desc);
   select * from admin order by t1 ,t2 desc;
9,索引
    例如 建立复合索引:create index  idxt1  on admin(username,age);
         各自建立索引:create index  idxusername on  admin(username);
                       create index  idxage on admin(age);
10,外键foreign key(unique | primary key | not null)的用法()
     create table a(
a1 integer  primary key | unique | not null,
a2 text,
a3 integer );
     create table b()(
b1 integer ,
b2 text,
b3 integer,
foreign key(b3) references a(a1));
11,分页
     select * from account limit 5 offset 3; 
     或者  select * from account limit 5,3;
12,模糊查询
      select 字段 from 表 where 某字段 like 条件
     (1)%:表示任意0个或多个字符
     (2)_ :表示任意单个字符,匹配单个任意字符,常用来限制表达式的字符长度语句。
     (3)[ ]:表示括号内所列字符中的一个(类似正则表达式)
           select * from admin where username like ‘[张李王]三’;
           表示搜索的是“张三”,“李三”或“王三”
       [4]:[^]表示不在括号所列之类的单个字符。
       [5]:查询内容包含通配符时,用“[ ]”括起来。
13,删除表 | 索引
     drop table [ if exists] admin;
     drop index index_name
14,查询记录数目
     select count(*) from table_name;
汕尾分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product