OracleFaq(如何在ORACLE中更改表的列名和順序)

如需轉載,請註明出處! 用過ORACLE的人都知道,要想在ORACLE中更改表的列名和順序可是一件很煩瑣的事,下面給大傢提供一種簡單的方法。 SQL> select object_id from all_objects where owner=SCOTT and object_name=T1; OBJECT_ID ———- 6067 SQL> select obj#,col#,name from sys.col$ where obj#=6067; OBJ# COL# ———- ———- NAME ———————————————————— 6067 1 ID 6067 2 NAME SQL> update sys.col$ set name=NEW_ID ,col#=3 where obj#=6067 and name=ID; 1 row updated. SQL> update sys.col$ set name=MY_NAME,col#=1 where obj#=6067 and name=NAME; 1 row updated. SQL> update sys.col$ set col#=2 where obj#=6067 and col#=3; 1 row updated. SQL> commit; Commit complete. SQL> select * from scott.t1; ID NAME ———- ——————– 3 cheng 2 yong 1 xin 2 gototop 1 topcio 2 yongxin 1 cyx 7 rows selected. SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup ORACLE instance started. Total System Global Area 128159368 bytes Fixed Size 732808 bytes Variable Size 117440512 bytes Database Buffers 8388608 bytes Redo Buffers 1597440 bytes Database mounted. Database opened. SQL> select * from scott.t1; MY_NAME NEW_ID ——————– ———- cheng 3 yong 2 xin 1 gototop 2 topcio 1 yongxin 2 cyx 1 7 rows selected. 到此為止我們已經把SCOTT.T1表中的列名和順序都給該瞭,如果你隻想改變順序而不想改變列名,隻需再進行UPDATE就可以瞭,之所以不能一次完成是因為SYS.COL$中COL#,NAME都是UNIQUE的。 這種方法雖然存在一定風險,但對於特別大的表效果是很明顯的,而用一般的方法將需要更多的存儲空間、回滾段和時間開銷。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *