oracle查询锁表及解锁,修改表字段名与复制表结构和数据的方法

在Oracle中查询锁表及解锁:

锁表查询的代码有以下的形式:

select count(*) from v$locked_object;
select * from v$locked_object;

查看哪个表被锁:

select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;

查看是哪个session引起的:

select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time;

杀掉对应进程:

执行命令:alter system kill session'32,241';其中32为sid,241为serial#.

在Oracle中修改表字段名:

假设字段有数据,则改为nvarchar2(20)可以直接执行:

alter table tb modify (name nvarchar2(20));

若不能修改则用下面

alter table sft_bfwo rename column bfwoid to wid;
alter table sft_bfwo add bfwoid char(16);
update sft_bfwo set bfwoid=trim(wid);
alter table sft_bfwo drop column wid;

在Oracle中复制表结构和表数据:

复制表结构及其数据:

create table table_name_new as select * from table_name_old

只复制表结构:

create table table_name_new as select * from table_name_old where 1=2;

或者:

create table table_name_new like table_name_old

只复制表数据:

如果两个表结构一样:

insert into table_name_new select * from table_name_old

如果两个表结构不一样:

insert into table_name_new(column1,column2...) select column1,column2... from table_name_old

相关推荐