文档库 最新最全的文档下载
当前位置:文档库 › ORACLE中的权限设定,建外键,序列,触发器等 .

ORACLE中的权限设定,建外键,序列,触发器等 .

首先建立了两张表:
create table car_type_t (
typeid numeric(20) not null,
typename varchar(50) null,
constraint PK_CAR_TYPE_T primary key (typeid)
)

create table vehicle_t (
vehicleid numeric(10) not null,
typeid numeric(20) null,
fueltypeid numeric(10) null,
vehiclestatusid numeric(10) null,
vehiclenumber numeric(10) null,
vehiclelength float(10) null,
vehiclewidth float(10) null,
vehiclehigh float(10) null,
vehiclecolor varchar(10) null,
solddat date null,
manufactcomp varchar(100) null,
engineid numeric(20) null,
vehiclephycid numeric(20) null,
width float(5) null,
capaweight float(5) null,
constraint PK_VEHICLE_T primary key (vehicleid)
)
创建两张表之间的关联关系:

alter table vehicle_t
add constraint FK_VEHICLE_RELATIONS_CARTYPE foreign key (typeid)
references car_type_t (typeid)

然后遇到了一些问题:
1.首先是我想让ORACLE中表ID自动增加,
具体实现:
首先建立了一个序列
create sequence cartype_s
increment by 1
start with 10000
nomaxvalue
nocycle;

然后再对这个序列建立了触发器
create or replace trigger car_type_t_trg
before insert or update on car_type_t
for each row
begin
select cartype_s.nextval into :new.typeid from dual;
end;
/在建立出发器的时候遇到了一些问题!就是会报错,"权限不足",于是仔细研究发现:是我建立的该角色没有建触发器的权限:具体建立方法:grant create trigger to vm_user

这样键好后,insert into car_type_t values(232,'test'); 无论第一个字段是多少,最后查询car_type_t是,字段typeid是不断从10000增加的.

2.如何在ORACLE中查看表间关系,及外键关联关系:

select * from all_constraints
where constraint_type = 'R'
and owner = 'CARMEN'


select * from all_cons_columns
where owner = 'CARMEN'
and constraint_name = 'PK_CAR_TYPE_T'
此两行语句就可以查出表中的FK对应的PK,和PK对应的表名称.

3.删除表的时候需要先删除外键
alter table VEHICLE_T
drop constraint FK_VEHICLE_RELATIONS_CARTYPE

drop table car_type_t才可以做到

4.查询触发器
Select * from All_Triggers
where owner='CARMEN'

5.删除序列
drop sequence cartype_s

相关文档