database Oracle 将列移到第一个位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1824654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:31:43  来源:igfitidea点击:

Oracle move column to the first position

databaseoracle

提问by opHASnoNAME

is there any way to move an column in an oracle table from last to first position? Someone has dropped the ID column, and recreated it. So now it is at the end, wich is an problem because some of our PHP Scripts are using the first column as identifier (one Abstract Model with more than 100 other Models using this base object..)

有没有办法将oracle表中的列从最后一个位置移动到第一个位置?有人删除了 ID 列,然后重新创建了它。所以现在它在最后,这是一个问题,因为我们的一些 PHP 脚本使用第一列作为标识符(一个抽象模型和 100 多个使用此基础对象的其他模型..)

See also

也可以看看

回答by nickf

The Oracle FAQsays:

甲骨文常见问题解答说:

Oracle only allows columns to be added to the end of an existing table.

Oracle 只允许将列添加到现有表的末尾。

You'd have to recreate your table.

你必须重新创建你的表。

RENAME tab1 TO tab1_old;

CREATE TABLE tab1 AS SELECT id, <the rest of your columns> FROM tab1_old;

回答by Vincent Malgrat

the simplest way to modify the logical order of the columns of a table is to rename your table and create a view with the "right" column positions:

修改表列的逻辑顺序的最简单方法是重命名表并创建具有“正确”列位置的视图:

ALTER TABLE your_table RENAME TO your_table_t;

CREATE VIEW your_table AS SELECT <columns in the right order> FROM your_table_t;

-- grants on the view (the same as the table)
GRANT ** TO ** ON your_table;

Your application will behave as if the columns were in the "right" position. You don't have to touch at the physical structure.

您的应用程序的行为就像列位于“正确”位置一样。您不必接触物理结构。

回答by Sulaha

In Oracle 12c it is now easier to rearrange columns logically. It can be achived by making column invisible/visible.If you change a invisible column to visible , the column will appear last in the odering.

在 Oracle 12c 中,现在可以更轻松地按逻辑重新排列列。可以通过使列不可见/可见来实现。如果将不可见列更改为可见,该列将出现在排序的最后。

Consider Using Invisible Columns

考虑使用不可见列

Create wxyz table:

创建 wxyz 表:

CREATE TABLE t ( w INT,
y VARCHAR2, z VARCHAR2, x VARCHAR2

创建表 t ( w INT,
y VARCHAR2, z VARCHAR2, x VARCHAR2

);

);

rearrange the x column to the middle:

将 x 列重新排列到中间:

ALTER TABLE wxyz MODIFY (y INVISIBLE, z INVISIBLE); ALTER TABLE wxyz MODIFY (y VISIBLE, z VISIBLE);

ALTER TABLE wxyz 修改(y 不可见,z 不可见);ALTER TABLE wxyz 修改(y 可见,z 可见);

DESCRIBE wxyz;

描述 wxyz;

Name

姓名

w

x

X

y

z

z

回答by cagcowboy

Recreating the table (via rename/temporary table so you don't lose your data) is the only way I know of.

重新创建表(通过重命名/临时表,这样您就不会丢失数据)是我所知道的唯一方法。

I don't believe it's possible to simply change the column order.

我不相信简单地更改列顺序是可能的。

回答by cagcowboy

If there isn't a ton of data/columns, you could simply rename the columns in the order you want. Then just delete * from 'your table name here'. This was a good solution for me since I hadn't inserted many records yet.

如果没有大量数据/列,您可以简单地按照您想要的顺序重命名列。然后只需从“您的表名”中删除 * 即可。这对我来说是一个很好的解决方案,因为我还没有插入很多记录。

回答by Samrat Mondal

Not possible to move column in oracle. It will be created in the last position. If anyone wanted so either view needs to be created or new table need to be created

无法在 oracle 中移动列。它将在最后一个位置创建。如果有人想要,那么需要创建视图或需要创建新表

回答by David Aldridge

You might like to access your table via a view, so you can painlessly rearrange the logical order if it's important to the application technology.

您可能希望通过视图访问您的表,因此如果它对应用程序技术很重要,您可以轻松地重新排列逻辑顺序。

回答by user2161700

I use this all the time:

我经常用这个:

ALTER TABLE MOVE COLUMN column_name TO ordinal_position;