当前位置: 移动技术网 > IT编程>数据库>SQLLite > SQLite教程(三):数据表和视图简介

SQLite教程(三):数据表和视图简介

2017年11月30日  | 移动技术网IT编程  | 我要评论
一、创建数据表:     该命令的语法规则和使用方式与大多数关系型数据库基本相同,因此我们还是以示例的方式来演示sqlite中创建表的各种规

一、创建数据表:

    该命令的语法规则和使用方式与大多数关系型数据库基本相同,因此我们还是以示例的方式来演示sqlite中创建表的各种规则。但是对于一些sqlite特有的规则,我们会给予额外的说明。注:以下所有示例均是在sqlite自带命令行工具中完成的。

    1). 最简单的数据表:
 


    sqlite> create table testtable (first_col integer);
 

    这里需要说明的是,对于自定义数据表表名,如testtable,不能以sqlite_开头,因为以该前缀定义的表名都用于sqlite内部。

    2). 创建带有缺省值的数据表:
 


    sqlite> create table testtable (first_col integer default 0, second_col varchar default 'hello');

    3). 在指定数据库创建表:
 

    sqlite> attach database 'd:/mydb.db' as mydb;
    sqlite> create table mydb.testtable (first_col integer);
 

    这里先通过attach database命令将一个已经存在的数据库文件attach到当前的连接中,之后再通过指定数据库名的方式在目标数据库中创建数据表,如mydb.testtable。关于该规则还需要给出一些额外的说明,如果我们在创建数据表时没有指定数据库名,那么将会在当前连接的main数据库中创建该表,在一个连接中只能有一个main数据库。如果需要创建临时表,就无需指定数据库名,见如下示例:
    --创建两个表,一个临时表和普通表。
 

    sqlite> create temp table temptable(first_col integer);
    sqlite> create table testtable (first_col integer);   
 

    --将当前连接中的缓存数据导出到本地文件,同时退出当前连接。
 

    sqlite> .backup d:/mydb.db
    sqlite> .exit
 

    --重新建立sqlite的连接,并将刚刚导出的数据库作为主库重新导入。
 

    sqlite> .restore d:/mydb.db
 

    --查看该数据库中的表信息,通过结果可以看出临时表并没有被持久化到数据库文件中。
 

    sqlite> .tables
    testtable   

    4). "if not exists"从句:
    如果当前创建的数据表名已经存在,即与已经存在的表名、视图名和索引名冲突,那么本次创建操作将失败并报错。然而如果在创建表时加上"if not exists"从句,那么本次创建操作将不会有任何影响,即不会有错误抛出,除非当前的表名和某一索引名冲突。
 

    sqlite> create table testtable (first_col integer);
    error: table testtable already exists
    sqlite> create table if not exists testtable (first_col integer);

    5). create table ... as select:
    通过该方式创建的数据表将与select查询返回的结果集具有相同的schema信息,但是不包含缺省值和主键等约束信息。然而新创建的表将会包含结果集返回的所有数据。
 

    sqlite> create table testtable2 as select * from testtable;   
    sqlite> .schema testtable2
    create table testtable2(first_col int);
 

    .schema命令是sqlite3命令行工具的内置命令,用于显示当前数据表的create table语句。   

    6). 主键约束:
 


    --直接在字段的定义上指定主键。
    sqlite> create table testtable (first_col integer primary key asc);
    --在所有字段已经定义完毕后,再定义表的数约束,这里定义的是基于first_col和second_col的联合主键。
    sqlite> create table testtable2 (
       ...>     first_col integer,
       ...>     second_col integer,
       ...>     primary key (first_col,second_col)
       ...> );

    和其他关系型数据库一样,主键必须是唯一的。

    7). 唯一性约束:
 


    --直接在字段的定义上指定唯一性约束。
    sqlite> create table testtable (first_col integer unique);
    --在所有字段已经定义完毕后,在定义表的唯一性约束,这里定义的是基于两个列的唯一性约束。
    sqlite> create table testtable2 (
       ...>     first_col integer,
       ...>     second_col integer,
       ...>     unique (first_col,second_col)
       ...> );   
    在sqlite中,null值被视为和其他任何值都是不同的,这样包括和其他的null值,如下例:
    sqlite> delete from testtable;
    sqlite> select count(*) from testtable;
    count(*)
    ----------
    0
    sqlite> insert into testtable values(null);
    sqlite> insert into testtable values(null);
    sqlite> select count(*) from testtable;
    count(*)
    ----------
    2  
 

    由此可见,两次插入的null值均插入成功。

    8). 为空(not null)约束:
 


    sqlite> create table testtable(first_col integer not null);
    sqlite> insert into testtable values(null);
    error: testtable.first_col may not be null
 

    从输出结果可以看出,first_col已经被定义了非空约束,因此不能在插入null值了。   

    9). 检查性约束:
 


    sqlite> create table testtable (first_col integer check (first_col < 5));
    sqlite> insert into testtable values(4);
    sqlite> insert into testtable values(20); -- 20违反了字段first_col的检查性约束(first_col < 5)
    error: constraint failed
    --和之前的其它约束一样,检查性约束也是可以基于表中的多个列来定义的。   
    sqlite> create table testtable2 (
       ...>     first_col integer,
       ...>     second_col integer,
       ...>     check (first_col > 0 and second_col < 0)
       ...> );

二、表的修改:

    sqlite对alter table命令支持的非常有限,仅仅是修改表名和添加新字段。其它的功能,如重命名字段、删除字段和添加删除约束等均为提供支持。

    1). 修改表名:

    需要先说明的是,sqlite中表名的修改只能在同一个数据库中,不能将其移动到attached数据库中。再有就是一旦表名被修改后,该表已存在的索引将不会受到影响,然而依赖该表的视图和触发器将不得不重新修改其定义。
 


    sqlite> create table testtable (first_col integer);
    sqlite> alter table testtable rename to testtable2;
    sqlite> .tables
    testtable2   
 

    通过.tables命令的输出可以看出,表testtable已经被修改为testtable2。   

    2). 新增字段:
 


    sqlite> create table testtable (first_col integer);
    sqlite> alter table testtable add column second_col integer;
    sqlite> .schema testtable
    create table "testtable" (first_col integer, second_col integer);
 

    通过.schema命令的输出可以看出,表testtable的定义中已经包含了新增字段。   
    关于alter table最后需要说明的是,在sqlite中该命令的执行时间是不会受到当前表行数的影响,也就是说,修改有一千万行数据的表和修改只有一条数据的表所需的时间几乎是相等的。
   
三、表的删除:

    在sqlite中如果某个表被删除了,那么与之相关的索引和触发器也会被随之删除。在很多其他的关系型数据库中是不可以这样的,如果必须要删除相关对象,只能在删除表语句中加入with cascade从句。见如下示例:
 


    sqlite> create table testtable (first_col integer);
    sqlite> drop table testtable;
    sqlite> drop table testtable;
    error: no such table: testtable
    sqlite> drop table if exists testtable; 
 

    从上面的示例中可以看出,如果删除的表不存在,sqlite将会报错并输出错误信息。如果希望在执行时不抛出异常,我们可以添加if exists从句,该从句的语义和create table中的完全相同。
   
四、创建视图:

    我们这里只是给出简单的sql命令示例,具体的含义和技术细节可以参照上面的创建数据表部分,如临时视图、"if not exists"从句等。
    1). 最简单的视图:
 


    sqlite> create view testview as select * from testtable where first_col > 100;   
   

    2). 创建临时视图:
 

    sqlite> create temp view tempview as select * from testtable where first_col > 100;
   

    3). "if not exists"从句:
 

    sqlite> create view testview as select * from testtable where first_col > 100;
    error: table testview already exists
    sqlite> create view if not exists testview as select * from testtable where first_col > 100;
   

五、删除视图:

    该操作的语法和删除表基本相同,因此这里只是给出示例:
 


    sqlite> drop view testview;
    sqlite> drop view testview;
    error: no such view: testview
    sqlite> drop view if exists testview;      

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网