Oracle在插入时不自动生成主键



新的Oracle这里。我有一个使用以下SQL创建的表:

create table Widgets (
    id              integer constraint pkWidgets primary key using index,
    ruleId          integer not null,
    customerId      integer constraint fkWidgets_Customers references Customers
);

我现在试图用以下方式将记录插入此表中。

INSERT INTO Widgets (
  ruleId,
  customerId
) VALUES (
  88471239,
  null
);

并获得以下错误:

INSERT INTO Widgets not successful
An error occurred when executing the SQL command:
INSERT INTO Widgets (
  ruleId,
  customerId...
ORA-01400: cannot insert NULL into ("MYSCHEMA"."WIDGETS"."ID") [SQL State=23000, DB Errorcode=1400]
1 statement failed.
Execution time: 0.13s

这里发生了什么? Oracle不应该自动生成我的主键(id字段)值吗?如果没有,我该如何告诉( centect sql )为此值插入什么?

仅将某些内容声明为主键不会导致产生值。在Oracle 12c中,您可以使用:

id  integer generated always as identity primary key

在早期版本中,您将使用触发器进行此操作。

我能够使用 ids.nextval,而我的插入功能很棒:

INSERT INTO Widgets (
  id,
  ruleId,
  customerId
) VALUES (
  ids.nextval,
  88471239,
  null
);

最新更新