【问题标题】:How to insert a column to an existing table?如何在现有表中插入一列?
【发布时间】:2012-07-07 18:09:22
【问题描述】:

我的表名userdetails

我想在location 列之后插入一个新列mob

我尝试使用代码

alter table userdetails 
add mob varchar2(10) after location;

但显示错误

ORA-01735: 无效的 ALTER TABLE 选项

请帮帮我。

我正在使用 oracle10g。

【问题讨论】:

  • 你可以使用这个解决方案:stackoverflow.com/questions/5391280/…
  • 是的,但是从谷歌搜索我得到了这个代码,显示错误
  • 您不能在给定列之后创建新列 - 新列总是添加到表的末尾。另外:在关系数据库中,列的顺序无论如何都是绝对不相关的。

标签: database oracle oracle10g


【解决方案1】:

试着去掉“之后”

alter table userdetails add ( mob varchar2(10) )

【讨论】:

  • 不,我想在列位置之后添加 mob 列。这将在最后创建列
  • Oracle 只允许将列添加到现有表的末尾。你可以在这里看到非标准方法orafaq.com/faq/…
  • 谢谢你的链接,在 MySQL 中只有语法是有效的,谢谢
【解决方案2】:

没有“定位后”。语法无效。

您可能会走以下路线: 只需将 mob varchar 添加到 userdetails。 它将被添加到表格的末尾。 你仍然可以查询它。 ALTER TABLE userdetails ADD (mob varchar2(10))

要得到你想要的表结构:

// 1) rename the table
rename userdetails to userdetails_old;

// 2) recreate the table with your wanted structure
// Note that the selection order decides about the table structure.
create table userdetails
as
select a as a
     , b as b
     , location as location
     , mob  as mob
     , c as c
from userdetails_old;

// 3) check what you did
desc userdetails;

// 4) before dropping your old table
drop table userdetails_old;

【讨论】:

  • 没问题。但是接受的答案并不能回答您的问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 2019-01-01
相关资源
最近更新 更多