天天看點

SQLite快速入門二--表、視圖的建立、修改、删除操作

表、視圖、索引的建立、修改、删除操作等

一、表的建立

1、建立表

create if not exists  table student(StuID integer);

2、 建立帶有預設值的資料表:

create table if not exists  schoolTable(schID integer default 0, schName varchar default 'hz');

3、if not exists 使用

 如果已經存在表名、視圖名和索引名,那麼本次建立操作将失敗。加上"IF NOT EXISTS"從句,那麼本次建立操作将不會有任何影響.

create table if not exists studentTest(StuID integer);

4、primary key

create table if not exists  studenttable (stuid integer primary key asc); 建立主鍵

create table if not exists studenttable2 (stuid integer,stuName varchar, primary key(stuid,stuName)); 建立聯合主鍵

4 unique限制

create table if not exists sutTest(stuID integer unique); 建立唯一性限制

5 check限制

create table if not exists sutTest2(stuID integer, ID integer, check(stuID > 0 and ID <0));

二、表的修改

1、修改表名

alter table sutTest rename to stutest;

2、向表中添加新列

alter table  stuTest add column stuName varchar;

三、表的删除

drop table if exists stuTest 如果某個表被删除了,那麼與之相關的索引和觸發器也會被随之删除。 

四、建立視圖

1、建立簡單視圖

create view if not exists View_Corporate as select * from corporate where corid > 1

2、建立臨時視圖

create temp view tempView_Corporate as select * from corporate where corid > 1

五、删除視圖

drop view if exists View_Corporate;

六、索引的建立

1、該索引基于corporate表的corID字段。

create index cor_index on corporate(corID);

2、該索引基于corporate表的corID,corname字段,,并且指定每個字段的排序規則

create index cor_index2 on corporate(corID asc, corName desc);

3、建立唯一索引

create unique index cor_index3 on corporate(corID asc, corName desc);

七、删除索引

drop index if exists cor_index3;

八、重建索引 reindex;

 重建索引用于删除已經存在的索引,同時基于其原有的規則重建該索引。

九、資料分析 analyze;

十、資料清理 vacuum;

本文轉自Work Hard Work Smart部落格園部落格,原文連結:http://www.cnblogs.com/linlf03/archive/2012/02/20/2359009.html,如需轉載請自行聯系原作者

繼續閱讀