Oracle的对象——表,同义词,序列,视图,索引和簇

一:表 数据库

 

a.建立表语法格式 安全

create table table_name
(
 字段1 类型1,
 字段2 类型2,
 字段3 类型3(也能够给字段设置默认值)
 ...
); oracle

 

Sql代码   收藏代码
  1. create table student  
  2. (  
  3.  stuno int,  
  4.  stuname varchar(10) not null,  
  5.  stuBirth date default to_date('1980-1-1','YYYY-MM-DD')  
  6. );  

 

查询一下表的结构:desc table_name 分布式

Sql代码   收藏代码
  1. SQL> desc student;  
  2. Name     Type         Nullable Default                          Comments   
  3. -------- ------------ -------- -------------------------------- --------   
  4. STUNO    INTEGER      Y                                                    
  5. STUNAME  VARCHAR2(10)                                                      
  6. STUBIRTH DATE         Y        to_date('1980-1-1','YYYY-MM-DD')     

 

b.给已存在的表增长新列:alter table tablen_ame add(列名1 列类型, 列名2 列类型...);  函数

Sql代码   收藏代码
  1. alter table student add(t3 varchar(10),t4 varchar2(10), t5 number(18,2));  

 

查询结果以下: 性能

Sql代码   收藏代码
  1. SQL> desc student;  
  2. Name     Type         Nullable Default                          Comments   
  3. -------- ------------ -------- -------------------------------- --------   
  4. STUNO    INTEGER      Y                                                    
  5. STUNAME  VARCHAR2(10)                                                      
  6. STUBIRTH DATE         Y        to_date('1980-1-1','YYYY-MM-DD')            
  7. T3       VARCHAR2(10) Y                                                    
  8. T4       VARCHAR2(10) Y                                                    
  9. T5       NUMBER(18,2) Y  

 

新增了T3,T4和T5三列。 spa

 

c.给已存在的表删除一个列:alter table table_name drop column column_name; 对象

Sql代码   收藏代码
  1. alter table student drop column t3;  

 

查询结果将发现T3列已经删除。 索引

 

d.给已存在的表中的列重命名:alter table table_name rename column ole_col_name to new_col_name; rem

Sql代码   收藏代码
  1. alter table student rename column t4 to hello;  

 

查询结果将发现T4列已经更名为hello。

 

e.给已存在的表中的列更改类型
若是表中无数据:alter table 表名 modify 列名 新类型;

Sql代码   收藏代码
  1. alter table student modify(hello number);  

 

查询结果将发现hello列的类型由varchar2类型变成了number类型。

 

若是表中有数据:
alter table 表名 add 新列名 新列类型;
update 表名 set  新列名=旧列名;
alter table 表名 drop column 旧;
alter table 表名 rename column 旧列名 to 新列名;(这句有玄机,好好理解)

 

这种方法会使列名发生变化,并且字段顺序增长 有可能发生行迁移,对应用程序会产生影响,使用起来要慎重。

 

f.设置某字段无用:alter table table_name set unused column column_name;

Sql代码   收藏代码
  1. alter table student set unused column t5;  

 

这个时候你不管查询表中的数据仍是查看表的结构,你都看不到t5这个列了。可是此列仍然存在表中,只不过被Oracle给“关”起来了。无用的字段是没法恢复的,由于这是DDL语句,要想恢复,只能恢复库了。

 

删除无用字段(会把字段删除掉):alter table student drop unused column;

Sql代码   收藏代码
  1. alter table student drop unused column;  

 

这时候这个无用字段被Oracle给“枪毙”了。不再存在了。

 

g.给表增长约束:alter table table_name add constraint constraint_name primary key(column_name);

给一个表中的某个字段增长主键或者外键约束。

Sql代码   收藏代码
  1. alter table student add constraint pk_stuno primary key(stuno);  

 

删除约束:alter table table_name drop constraint constraint_name;

Sql代码   收藏代码
  1. alter table student drop constraint pk_stuno;  

 

表的数据字典为user_tables,

约束的数据字典为user_constraints,

 

另外一样也能够给一个表中的多个列同时设置为主键,即复合主键,这里就不作介绍了。

 

二:同义词

 

同义词是简化的SQL语句,隐藏对象的名称和全部者,为分布式数据库的远程对象提供了位置透明性,提供对对象的公共访问。其语法以下:
create synonym 命令用于建立同义词
drop synonym 命令用于删除同义词
user_synonyms 包含同义词的数据字段

 

建立一个同义词,就用上面的student表。若是你的当前用户没有建立同义词的权限,你要首先用SYS用户登陆受权。我用的是scott用户,我就须要SYS用户给scott用户受权建立同义词。

Sql代码   收藏代码
  1. grant create synonym to scott;  

 

而后切换回scott用户,建立student表的同义词:

Sql代码   收藏代码
  1. create synonym ghk_student for student;  

 

这个时候,同义词ghk_student和表student效力是同样的。可是我隐藏了student表(还有他的用户scott,我只须要对同义词进行操做便可)。

 

为了说明问题,向ghk_student表中插入试验数据:

Sql代码   收藏代码
  1. insert into ghk_student values(12, 'huangkai', to_date('19830326','YYYYMMDD'), 322);  

 

这个时候查询student表和ghk_student同义词的结果是同样的。

 

删除同义词:drop synonym synonym_name;

Sql代码   收藏代码
  1. drop synonym ghk_student;  

 

三:序列

 

序列是数据库提供一种对象,可以提供自动的连续的惟一的值。其建立于法格式以下:

Sql代码   收藏代码
  1. create sequence sequence_name  
  2.     [increment by n] 指定增加间隔数  
  3.     [start with n] 初始值  
  4.     [{maxvalue n | nomaxvalue}] 最大值,再增加转为起始值  
  5.     [{minvalue n | nominvalue}] 最小值  
  6.     [{cycle | nocycle}] 是否循环,在最大值和初始值间轮回  
  7.     [{cache n | nocache}]; 自动在缓冲区生成,速度快  

 

序列两个重要属性:nextval,currval,实际上是两个函数
nextval产生下一个值,初始化序列值;
currval获取当前序列值

下面咱们来作实验,建立一个序列:

Sql代码   收藏代码
  1. create sequence numseq increment by 1 start with 1 maxvalue 999;  

 

建个临时表:

Sql代码   收藏代码
  1. create table temp_test(sno int);  

 

而后向表中插入序列,能够反复插入:

Sql代码   收藏代码
  1. insert into temp_test(sno) values(numseq.nextval);  
  2. insert into temp_test(sno) values(numseq.nextval);  
  3. insert into temp_test(sno) values(numseq.nextval);  
  4. insert into temp_test(sno) values(numseq.nextval);  
  5. insert into temp_test(sno) values(numseq.nextval);  

 

查询temp_test表中的内容,能够看到sno是连续的1,2,3,4,5。这里我就不截图了。

 

查询序列当前值:select numseq.currval from dual;

查询序列下一个值:select numseq.nextval from dual;

 

再次插入序列,看看结果如何?答曰:temp_test中的sno已经出现了跳点,再也不是6,7,8……等连续的了。

当你在select numseq.nextval from dual时候序列已经增长,再次插入的时候就是增长后的值了,因此和前面不连续了。可是连续插入的话,插入的值仍是连续的。

 

删除序列语法:drop sequence sequence_name。

序列的数据字典:user_sequence。

 

四:视图

 

视图用来显示一个或多个表中的数据。视图不真正存储数据,只是一些查询语句,成为“虚表”或“已存储的查询”。

视图优势:提供另一种级别的表安全性查询;隐藏数据的复杂性;简化用户的SQL命令;将应用程序与基表定义的修改隔离开来;从另外一个角度提供数据。

 

scott用户没有建立视图的权限,能够用SYS用户授予其权限,受权语句:

Sql代码   收藏代码
  1. grant create any view to scott;  

 

而后切换回scott用户,建立一个视图:

Sql代码   收藏代码
  1. create or replace view v_student as select stuno,stuname from student;  

 

一个视图创建完毕。

 

这个视图就是截取表中的stuno字段和stuname字段,咱们查询视图的内容和查询表中的内容是同样的(除了列数不一样)。向表中插入数据在查询视图的时候也能查出来,向视图中插入数据在查询表的时候也能查出来(没有的列以默认或空做为显示,前提是视图中没有的列在表中是容许为空的,不然视图插不进去数据)。

 

删除视图:drop view view_name;

 

从新编译视图:alter view view_name compile;

为何要从新编译呢?
咱们建立视图,在后台数据库建立了视图对象,把语句进行编译,把编译后的对象存在视图里面,时间长了之后,可能源表发生变化,致使无效,因此须要按期从新编译视图。

 

视图的数据字典:user_views。

 

五:索引

 

索引,又称为“快表”,提供快速检索表中数据的机制,Oracle有索引段用于存储索引。表更新时,索引亦会连带更新。索引会加快SQL语句的执行,减小磁盘I/O,create index语句用于建立索引,在物理上和逻辑上独立于表中的数据,oracle自动维护索引。

 

a.惟一值索引:定义在索引列中的值是不重复的;
oracle自动为主键和惟一键建立惟一索引;
create unique index 语句用于建立惟一索引。

 

给上面的student表建立惟一值索引:

Sql代码   收藏代码
  1. create unique index idx_stuno on student(stuno);  

 

原来student表中的stuno不是主键,也不是惟一的,这回为其创建惟一值索引。向表中插入数据时候若stuno重复会报错。

 

b.组合索引:将表中某几列字段的值合在一块儿:

Sql代码   收藏代码
  1. create index idx_stunoandstuname on student(stuno, stuname);  

 若是我要查询stuno和stuname,Oracle会从索引段中查询,而不是从整个表中查询,提升效率。

 

c.反向键索引:会把索引表中的值按位反转,适合向表中填入数据而不是更新输入的场合:

Sql代码   收藏代码
  1. create reverse index idx_xx on table(column);  

 

d.位图索引:建立重复率比较大的数据列:

Sql代码   收藏代码
  1. create bitmap index idx_job on emp(job);  

 

索引数据字典:user_indexes。

——(索引须要继续完善)——

 

六:簇

 

簇的含义是汇集的意思,其实和表差很少,create cluster语句用于建立簇,应首先建立簇,而后建立组成簇的表。
优势:减小磁盘I/O,节省磁盘空间;
缺点:插入操做的性能下降;

 

有主外键关系表能够考虑建立簇。

 

例子:

Sql代码   收藏代码
  1. create cluster class_cluster  
  2. (  
  3.     classno varchar2(10)  
  4. );  
  5.   
  6. create table stuClass(classno varchar2(10), classname varchar2(20))  
  7. cluster class_cluster(classno);  
  8.   
  9. create table stuInfo(studentno varchar2(10), stuname varchar(20),  
  10. classno varchar2(10)) cluster class_cluster(classno);  

 

说明:首先建立一个簇class_cluster,簇包括一个字段classno。再建立两个表stuClass和stuInfo,这两个表都含有classno字段,类型也和簇同样,语法格式如上。那么向表中操做数据和普通表同样,若是插入数据到表中,那么classno是存储在簇当中的。

 

另外,删除簇时应先删除含有簇的表。

 

簇的数据字典:user_clasters。

相关文章
相关标签/搜索