Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion CN/modules/ROOT/pages/master/oracle_compatibility/sequence.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,36 @@ ivorysql=# SELECT seq.NEXTVAL FROM DUAL;
(1 row)
ivorysql=# DROP SEQUENCE seq;
DROP SEQUENCE
```
```

== 向标识列导入数据

在 Oracle 模式下,标识列使用关联序列来生成值。根据导入时提供 ID 的方式选择定义子句。

[cols="2,1,1,1",options="header"]
|===
|标识列子句 |省略值或使用 `DEFAULT` |显式指定数字 |字面量 `NULL`
|`GENERATED ALWAYS AS IDENTITY` |生成 |普通 `INSERT` 拒绝 |拒绝
|`GENERATED BY DEFAULT AS IDENTITY` |生成 |保存给定值 |拒绝
|`GENERATED BY DEFAULT ON NULL AS IDENTITY` |生成 |保存给定值 |生成
|===

以下示例导入已有 ID `42`,并将序列的起点设在导入范围之上。两条新记录分别取得 `1000` 和 `1001`。

[source,sql]
----
CREATE TABLE identity_import_demo (
item_id bigint GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 1000)
PRIMARY KEY,
label varchar2(30)
);

INSERT INTO identity_import_demo VALUES (42, 'legacy');
INSERT INTO identity_import_demo (label) VALUES ('generated');
INSERT INTO identity_import_demo VALUES (NULL, 'literal null');
SELECT item_id, label FROM identity_import_demo ORDER BY item_id;

DROP TABLE identity_import_demo;
----

插入显式 ID 时,标识列的序列计数保持原值。允许插入自动生成的 ID 前,应根据导入数据设置安全的序列起点,或重新设置序列值。主键或唯一约束负责保证唯一性。
32 changes: 32 additions & 0 deletions EN/modules/ROOT/pages/master/oracle_compatibility/sequence.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,35 @@ ivorysql=# SELECT seq.NEXTVAL FROM DUAL;
ivorysql=# DROP SEQUENCE seq;
DROP SEQUENCE
```

== Import rows into an identity column

In Oracle mode, an identity column obtains generated values from an associated sequence. Choose the clause according to how the import supplies IDs.

[cols="2,1,1,1",options="header"]
|===
|Identity clause |Omitted value or `DEFAULT` |Explicit number |Literal `NULL`
|`GENERATED ALWAYS AS IDENTITY` |Generated |Rejected by a plain `INSERT` |Rejected
|`GENERATED BY DEFAULT AS IDENTITY` |Generated |Stored as supplied |Rejected
|`GENERATED BY DEFAULT ON NULL AS IDENTITY` |Generated |Stored as supplied |Generated
|===

The example imports an existing ID of `42` and starts the sequence above the imported range. The two new rows receive `1000` and `1001`.

[source,sql]
----
CREATE TABLE identity_import_demo (
item_id bigint GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH 1000)
PRIMARY KEY,
label varchar2(30)
);

INSERT INTO identity_import_demo VALUES (42, 'legacy');
INSERT INTO identity_import_demo (label) VALUES ('generated');
INSERT INTO identity_import_demo VALUES (NULL, 'literal null');
SELECT item_id, label FROM identity_import_demo ORDER BY item_id;

DROP TABLE identity_import_demo;
----

Inserting explicit IDs leaves the identity sequence's counter untouched. Set the start value or reseed the sequence to a safe value for the imported data before allowing generated inserts. A primary key or unique constraint enforces uniqueness.
Loading