在本教程中,您将学习如何使用oracle rename
语句重命名数据库中的表。
要重命名表,可以使用以下oracle rename
表语句,如下所示:
rename table_name to new_name;
在rename
表语句中:
请注意,一旦执行了
rename
语句,就不能回滚了。
当重命名表时,oracle自动将旧表上的索引,约束和授权转移到新表上。 另外,它使依赖重命名表(原表)的所有对象失效,如视图,存储过程,函数和同义词。
我们来创建一个名为promotions
表,用来作演示。
create table promotions(
promotion_id number generated by default as identity,
promotion_name varchar2(255),
start_date date not null,
end_date date not null,
primary key(promotion_id),
check (end_date > start_date)
);
以下pl/sql
函数通过查询promotions
表中的数据来返回促销的数量:
create or replace function count_promotions
return number
is
v_count number;
begin
select
count( * )
into
v_count
from
promotions;
return v_count;
end;
要将promotions
表重命名为campaigns
表,可以使用以下语句:
rename promotions to campaigns;
如上面所述,oracle将promotions
表中的所有索引,约束和授权转移到campaigns
表中。 以下语句显示了从promotions
表传输的新campaigns
表的限制条件:
select
object_type,object_name,status
from
all_objects
where
status = 'invalid' and owner='ot' ;
因为count_promotions
函数引用了promotions
表,所以当重命名promotions
表时,count_promotions
函数变得无效。
要查找当前模式中的无效对象,可以从all_objects
视图中查询数据,如下所示:
select
owner,
object_type,
object_name
from
all_objects
where
status = 'invalid'
order by
object_type,
object_name;
执行上面查询语句,得到以下结果 -
如上查询结果所示,该语句返回count_promotions
函数作为无效的对象。
在本教程中,您已学习如何使用oracle rename
表语句重命名数据库中的表。