oracel数据库基础使用教程

创建表空间

create tablespace user1_tablespace1

datafile ’e:\oracle\product\10.2.0\oradata\orcl\user1.dbf’

size 100m 默认设置表空间

autoextend on next 32m maxsize unlimited

logging

extent management local

segment space management auto 当空间使用完毕后自动扩展空间

创建用户

create user user1

identifed by xxxx

default tablespace tablespace1 指定默认表空间

temporary tablespace tablespace1 指定临时表空间

权限和角色

connect :临时用户

resource:更为可靠和正式的用户

dba :管理员角色,拥有管理数据库的最高权限。

分配权限或角色

grant privileges or role to user1

撤销权限和角色

revoke privileges or role from user1

把connect.resource角色授予用户user1

grant connect,resource to user1;

撤销用户user1的resource权限

revoke resource from user1

允许用户查看emp表中的记录

grant select on emp to user1

允许用户跟新emp表中的记录

grant updata on emp to user1

创建数据库表

添加列

alter table stuinfo add (stuxxxx varchar2

create table stuinfo (stuno char(6) not null,stuname varchar2(20) not null,stuage number(3,0) not null, stuid numeric(18,0),stuseat number(2,0));18代表18位数字,0代表小数位为0(25));

修改列

alter table stuinfo modifily (stuxxx varchar2(25));

删除列

alter table stuinfo drop column stutel_no;删除stutel_no列

alter table stuinfo drop (stutel_no,stuseat);删除stutel_no,stuseat列

约束

1.创建表的时候直接添加约束

constraint constraint_name check(condition)

constraint 关键词

constraint_name :约束名称

condition :用来唯一标识表中的一个列,一个表中的主键约束只能有一个,但是可以在一个主键约束中包含多个列,也称为联合主键

create table xxx (bookid int,xxx char,constraint ck_price check (price >=0 and price <=100));

alter table 表名 add constraint 约束名 约束类型 具体的约束说明

约束名的取名规则推荐采用:约束类型——约束字段

主键(primary key)约束 :如 pk_stuno

唯一(unique)约束:如uq_stuno

检查(check)约束:ck_stuage

外键(foreign key)约束:如fk_stuno

主键约束(primary keyconstraint)

添加主键约束(stuno作为主键)

alter table stuinfo add constraint pk_stuno primary key(stuno)

添加唯一约束(身份证号唯一,因为每个人的身份证号码全国唯一)

alter table stuinfo add constraint uq_stuid unique(stuid)

添加检查check约束,要求年龄只能在15-40岁之间

alter table stuinfo add constraint ck_stuage check(stuage between 15 and 40);

添加外键约束(主表stu个或多个列生成的键,以及映射到指定数据存储位置的指针。主表stuinfo 和从表stumarks建立关系,关联字段为stuno)

alter table stumarks add constraint fk_stuno foreign key(stuno) references stuinfo(stuno);

索引种类及其语法

1.b-tree索引:b—tree info和从表stumarks建立关系,关联字段为stuno)

alter table stumarks add constraint fk_stuno foreign key (stuno) references stuinfo(stuno);

2.索引:从表或视图中一”类型语句中起作用。

4.反向键索引:reverse key index ,如果主键值按一定顺序增加,那么索引树的层数也在增长很快,为了避免这种情况,使用了反向键索引这种方式,他对b-tree索引码中的字节进行了反转,这样就似的索引树分配更均匀,有效的减少了索引叶的竞争。

 建立索引

1. create index命令语法index,是oracle 中默认的和最常用的一种索引,可以使用它来检索有索引列排序的数据,也就是通常所见的唯一索引,聚簇索引。

2.位图索引:bitmap index,使用位图来管理与数据行的对应关系,主要是在联机数据分析方面,它适合数据差异较小的列。需要注意的是,该索引针对那些数据不经常改动的字段。

3.基于函数的索引:function-based index ,这种索引使用数据列基于函数返回值,在oracle 8i以后可以得到支持,他会在“select * from table_name where      

  function(col)=value:

create index创建索引

create [unique] index [user.]index

on [user.]table (column [asc | desc] [,column

[asc | desc] ] … )

[cluster [scheam.]cluster]

[initrans n]

[maxtrans n]

[pctfree n]

[storage storage]

[tablespace tablespace]

[no sort]

advanced

其中:

   schema oracle模式,缺省即为当前帐户

   index 索引名

   table 创建索引的基表名

   column 基表中的列名,一个索引最多有16列,long列、long raw

              列不能建索引列

   desc、asc 缺省为asc即升序排序

   cluster 指定一个聚簇(hash cluster不能建索引)

   initrans、maxtrans 指定初始和最大事务入口数

   tablespace 表空间名

   storage 存储参数,同create table 中的storage.

   pctfree 索引数据块空闲空间的百分比(不能指定pctused)

   nosort 不(能)排序(存储时就已按升序,所以指出不再排序)

2.建立索引的目的:

建立索引的目的是:

l 提高对表的查询速度;

l 对表有关列的取值进行检查。

但是,对表进行insert,update,delete处理时,由于要表的存放位置记录到索引项中而会降低一些速度。

注意:一个基表不能建太多的索引;

      空值不能被索引

      只有唯一索引才真正提高速度,一般的索引只能提高30%左右。

3.修改表

alter table 《表名》 add column_name |modify column_name |drop column column_name

解释:column_name:列名

      modify:表示修改某列

      这里的add 表示向表中添加一列,modify 表示向表中修改一列,drop 表示向表中删除一列

eg:alter table ic_nr_file add xxx_ks varchar2 (20);

eg:alter table ic_nr_file modify xxx_ks char not null;

4.删除列

alter table bookinfo drop column isbn   ..删除isbn列

在修改列的结构有一些限制:

(1).不能改变列名。

(2).不能将含有空值的列定义修改为not null 约束。

(3).若列中已有数据,减少该列的宽度时,不能比已有的数据还短。

(4).只能修改null/not null 约束,其他类型的约束在修改之前必须先删除,然后在重新添加修改过的约束定义。

(5).当改变列的数据类型时,列中已有的数据类型必须要与新的数据类型兼容。

5.重新命名表

rename oldname to newname;

6.表备份

select * from backup as select * from dba.db1

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐