oracle 修改表的列顺序

2020-09-01 10:53:19
方法一:

(1)备份目标表数据

create table 临时表 as select * from 目标表;

(2)drop 目标表

drop table 目标表;

(3)再重新按照要求的字段顺序建表;

create table 新表 (col1,..,coln);
(4)用select将数据从临时表导回。
 
方法二:通过修改sys的数据字典来实现。
第1步 创建表
create table stuInfo(
stuName varchar(20) not null,
stuNo char(6) not null,
stuAge number(3,0) not null
);

第2步 查询出表的id,注意:owner和object_name的值要大写

DBA用户使用
select object_id from all_objects where owner='WT' and object_name='STUINFO';--本例结果为73526
表归属用户可以使用
select object_id from obj where object_name='STUINFO';--本例结果为73526
第3步 通过id查出该表所有字段的顺序
select obj#,col#,name from sys.col$ where obj#=73526 order by col#;

第4步 更新字段顺序,需要sys用户或是System用户 as sysdba登录,或是其他拥有sysdba权限的用户,但是这里的操作一定要小心。

(1)更新目标字段的col#
update sys.col$ set col#=1 where obj#=73526 and name='STUNO';
(2)更新受影响字段的col#,自己根据情况自己组织语句
update sys.col$ set col#=2 where obj#=73526 and name!='STUNAME';
(3)提交
commit;
第5步  重启oracle
第6步  检查是否生效
select obj#,col#,name from sys.col$ where obj#=73526 order by col#;
select * from WT.STUINFO;

posted on 2020-09-01 10:45  wtsgtc  阅读(1584)  评论(0)    收藏  举报

导航