【问题标题】:ORA-00936 : missing expression error with ALTER table table_name modify (column_name DEFAULT on null column_name.nextval)ORA-00936: ALTER 表 table_name 修改时缺少表达式错误 (column_name DEFAULT on null column_name.nextval)
【发布时间】:2021-08-13 20:06:55
【问题描述】:

我使用以下方法向表中添加了一个新列:

ALTER table table_name add column_name number(8);

然后更新如下:

UPDATE table_name set column_name = column_name.nextval;

然后我尝试将列修改为非 NULL 列,如下所示:

ALTER table table_name modify (column_name DEFAULT on null column_name.nextval)

在最后一次查询之后,我得到了以下错误:

ORA-00936:缺少表达式

注意 - 此处添加的新列是一个序列。

你能告诉我我在这里缺少什么吗?

谢谢。

【问题讨论】:

  • 对于 alte table,您必须指定 what 来处理该列。 The syntax is:{ add_column_clause | modify_column_clauses | drop_column_clause | add_period_clause | drop_period_clause }... | rename_column_clause

标签: oracle11g


【解决方案1】:

应该是:

SQL> create table test (name varchar2(10));

Table created.

SQL> alter table test add id number;

Table altered.

SQL> alter table test modify id not null;           --> this

Table altered.

SQL> create sequence seq;

Sequence created.

SQL> alter table test modify id default on null seq.nextval;  --> this

Table altered.

SQL> desc test
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 NAME                                               VARCHAR2(10)
 ID                                        NOT NULL NUMBER

SQL>

请注意,这种语法在某些以前的 Oracle 数据库版本(例如 11g)中不起作用。您应该提及您使用的版本。

【讨论】:

  • 感谢您的回复,很抱歉错过了版本。我的错。 11g。
【解决方案2】:

我了解您不必在 alter 语句中指定“ON NULL”。尝试关注。

create sequence test_seq;
create table test_table (col_1 number);
alter table test_table modify col_1 default test_seq.nextval;

如果将null插入test_table.col_1,最后的Alter语句将自动将列值设置为序列的下一个值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多