【问题标题】:How to add column with default options using ENUM如何使用 ENUM 添加具有默认选项的列
【发布时间】:2020-10-06 09:19:12
【问题描述】:

我想在表格中添加一个称为类别的列,但我尝试了几个选项,但都不起作用

 alter table attachments
                add
                (
                  category varchar2(20) not null check (category in ('default','agreement','security','information') DEFAULT 'default')
                );

我也尝试过这样的事情

 ALTER TABLE attachments
 ADD  category ENUM ('default','agreement','security','information') DEFAULT 'default'

我在这里查看了几篇帖子,但找不到任何解决方案。我在哪里犯错了?这里有什么问题? 我收到消息

Error report -
ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
 

【问题讨论】:

  • ENUM 是 MySQL 语法,在 Oracle 中无效。
  • 是的,我只看到文档。谢谢

标签: sql oracle alter-table check-constraints


【解决方案1】:

你可以这样表述:

alter table attachments
add category varchar2(20) 
    default 'default'
    check (category in ('default','agreement','security','information'))
    not null 
;

【讨论】:

  • @xerror 您还应该命名约束(这样您就可以知道失败的原因,而不是尝试解码系统生成的约束名称)。类似... DEFAULT 'default' CONSTRAINT attachments__category__chk CHECK ( ... ) CONSTRAINT attachments__category__nn NOT NULL
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多