在 Oracle 中打开 OFF 等效的 IDENTITY INSERT

Turn ON OFF IDENTITY INSERT equivalent in Oracle

我的 DWH 设计中有以下 ProductCategory 维度,以免丢失数据:

ProductSK ProductID ProductName BI_StartDate BI_EndDate
-1        -1        Undefined   99991231     99991231

ProductSK 是标识列。

我习惯在SQL服务器中使用Turn ON/OFF Identity Insert,如何在Oracle中做同样的事情?

这是我的维度 DDL :

CREATE TABLE ProductCategory (
    ProductSK NUMBER GENERATED ALWAYS AS IDENTITY,
    ProductID NUMBER NOT NULL,
    ProductName VARCHAR2(100) NOT NULL,
    BI_StartDate NUMBER NOT NULL,
    BI_EndDate NUMBER NOT NULL,

);

SQL服务器中的等价物:

SET IDENTITY_INSERT sometableWithIdentity ON;
SET IDENTITY_INSERT sometableWithIdentity OFF;

在SQL服务器

set identity on Allows explicit values to be inserted into the identity column of a table.

基本上,您可以打开和关闭插入标识列的可能性,标识列被定义为基于间隔的数字序列。

在 Oracle 中,您可以选择使用 IDENTITY GENERATED BY DEFAULT

GENERATED BY DEFAULT: Oracle generates a value for the identity column if you provide no value. If you provide a value, Oracle will insert that value into the identity column. For this option, Oracle will issue an error if you insert a NULL value into the identity column.

例子

SQL> create table x ( c1 number generated by default as identity start with 1 increment by 1 , c2 number ) ;

Table created.

SQL> insert into x ( c2 ) values ( 1 ) ;

1 row created.

SQL> insert into x ( c1, c2 ) values ( 2, 2 ) ;

1 row created.

SQL> select * from x ;

        C1         C2
---------- ----------
         1          1
         2          2

此选项允许您插入或不插入(这是一种打开/关闭),在某种意义上非常类似于 SQL 服务器转 on/off。