更改列时在 Oracle 中添加默认约束

0 oracle default-value alter-table

我是甲骨文新手。我需要将 SQL Server 命令移植到 Oracle。

我想更改列以添加具有默认值的新约束。

SQL 服务器命令

ALTER TABLE <schema_name>.<table_name> 
ADD CONSTRAINT [<constraint_name>]
DEFAULT (1) FOR [<column_name>]
Run Code Online (Sandbox Code Playgroud)

Oracle命令

ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name> 
DEFAULT (1) FOR <column_name>
Run Code Online (Sandbox Code Playgroud)

运行 Oracle 查询时出现以下错误:

Error report - 
SQL Error: ORA-00904: : invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Run Code Online (Sandbox Code Playgroud)

可能是因为FOR标识符无效?

但是如何为特定列添加具有默认值的约束呢?

我们是否需要更改列并设置默认值?

哪种方法是正确的?

Bal*_*app 5

默认值不是 Oracle 中的约束。您只需将列更改为:

SQL> create table tab1 (col1 number, col2 number);

Table created.

SQL> alter table tab1 modify (col2 default 1);

Table altered.

SQL> select * from user_constraints;

no rows selected

SQL> insert into tab1 (col1) values (0);

1 row created.

SQL> select * from tab1;

      COL1       COL2
---------- ----------
         0          1
Run Code Online (Sandbox Code Playgroud)