work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

表、视图、索引的创建、修改、删除操作等

一、表的创建

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;