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的。
这种方法虽然存在一定风险,但对于特别大的表效果是很明显的,而用一般的方法将需要更多的存储空间、回滚段和时间开销。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools