"Missing right parenthesis" 在 CREATE TABLE 中默认

"Missing right parenthesis" on DEFAULT in a CREATE TABLE

在 table Aposta 中,每当我插入新行时,我希望 CodEstadoAposta 的值默认为 0,而不是 null,但这一直显示在脚本输出中:

”错误报告- ORA-00907: 缺少右括号 00907.00000 - "missing right parenthesis" *原因:
*行动:

Table 叛教者:

CREATE TABLE Aposta (
CodAposta Number(10) Primary key,
data DATE default sysdate not null,
hora VARCHAR2(5) not null, 
valor Number(10,2) not null,
quotaTotal Number(10,2)  not null,
CodTipoAposta references TipoAposta(CodTipoAposta) not null,
CodEstadoAposta references EstadoAposta(CodEstadoAposta) default '0' not null
);

如果有帮助,table EstadoAposta 和 TipoAposta:

CREATE TABLE EstadoAposta (
CodEstadoAposta Number(1) Primary key
  check (CodEstadoAposta in ('0','1','2')),
desc_EstadoAposta VARCHAR2(20) not null
);

CREATE TABLE TipoAposta (
CodTipoAposta Number(1) Primary key
  check (CodTipoAposta in ('0','1')),
desc_TipoAposta VARCHAR2(20) not null
); 

编辑:

这显示:default syntax error - 默认后它有“1”,但这只是我测试的。

我 运行 通过 Postgresql,针对 Postgres 类型稍作修改,以获得更好的诊断。

test=> CREATE TABLE Aposta (
test(>     CodAposta decimal(10) Primary key,
test(>     data DATE default sysdate not null,
test(>     hora varchar(5) not null, 
test(>     valor decimal(10,2) not null,
test(>     quotaTotal decimal(10,2)  not null,
test(>     CodTipoAposta references TipoAposta(CodTipoAposta) not null,
test(>     CodEstadoAposta references EstadoAposta(CodEstadoAposta) default '0' not null
test(> );
ERROR:  syntax error at or near "references"
LINE 7:     CodTipoAposta references TipoAposta(CodTipoAposta) not n...

每列都需要一个名称、类型和可选约束。 references ... 是一个约束条件。它缺少应与其引用的列类型匹配的类型。

此外,Oracle 似乎是 extremely particular about the order of constraintsdefault 必须出现在类型之后和任何约束之前。

CodTipoAposta number (1) not null references TipoAposta(CodTipoAposta),
CodEstadoAposta number(1) default '0' not null references EstadoAposta(CodEstadoAposta)

参见 db<>fiddle

Oracle 似乎严格遵循 SQL standard grammar for a column definition,其中 defaultreferencesnot null 等列约束分开。大多数 SQL 实现使用更宽松的语法,其中 default 被视为列约束。

我相信你想要的是:

CREATE TABLE Aposta (
    CodAposta Number(10) Primary key,
    data DATE default sysdate not null,
    hora VARCHAR2(5) not null, 
    valor Number(10,2) not null,
    quotaTotal Number(10,2)  not null,
    CodTipoAposta not null references TipoAposta(CodTipoAposta),
    CodEstadoAposta varchar2(1) default '0' references EstadoAposta(CodEstadoAposta) 
);

注意,当有外键引用时,需要为列声明类型。我还将外键引用作为该列的最后一个条件。

所有列都需要数据类型:例如

CREATE TABLE Aposta (
    CodAposta Number(10) PRIMARY KEY
  , data date DEFAULT sysdate NOT NULL
  , hora VARCHAR2(5) NOT NULL
  , valor Number(10, 2) NOT NULL
  , quotaTotal Number(10, 2) NOT NULL
  , CodTipoAposta number(1) NOT NULL --references TipoAposta(CodTipoAposta)
  , CodEstadoAposta number(1) DEFAULT 0 --references EstadoAposta(CodEstadoAposta) 
);

db<>fiddle here

对参考文献进行了评论,因为我没有这些表格,所以使用的数据可能不正确