简体   繁体   English

在 Oracle 中打开 OFF 等效的 IDENTITY INSERT

[英]Turn ON OFF IDENTITY INSERT equivalent in Oracle

I have the following ProductCategory dimension in my DWH design to not lose data:我的 DWH 设计中有以下 ProductCategory 维度,以免丢失数据:

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

The ProductSK is an identity column. ProductSK 是标识列。

I am used to use Turn ON/OFF Identity Insert in SQL Server, how can I do the same in Oracle?我习惯在 SQL Server 中使用 Turn ON/OFF Identity Insert,我怎样才能在 Oracle 中做同样的事情?

This is my dimension DDL:这是我的维度 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,

);

The equivalent in SQL Server: SQL Server 中的等价物:

SET IDENTITY_INSERT sometableWithIdentity ON;
SET IDENTITY_INSERT sometableWithIdentity OFF;

In SQL Server在 SQL Server 中

set identity on Allows explicit values to be inserted into the identity column of a table. set identity on允许将显式值插入到表的标识列中。

Basically you turn on and off the possibility to insert into an identity column which is defined as a sequence of numbers based on an interval.基本上,您可以打开和关闭插入标识列的可能性,该标识列被定义为基于间隔的数字序列。

In Oracle, you have the option to use IDENTITY GENERATED BY DEFAULT在 Oracle 中,您可以选择使用IDENTITY GENERATED BY DEFAULT的身份

GENERATED BY DEFAULT : Oracle generates a value for the identity column if you provide no value. GENERATED BY DEFAULT :如果您不提供任何值,Oracle 会为标识列生成一个值。 If you provide a value, Oracle will insert that value into the identity column.如果您提供一个值,Oracle 会将该值插入到标识列中。 For this option, Oracle will issue an error if you insert a NULL value into the identity column.对于此选项,如果您将 NULL 值插入标识列,Oracle 将发出错误消息。

Example例子

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

This option allows you to either insert or not ( which is kind of turning on / off ) in a sense very similar to the SQL Server turn on/off.这个选项允许您插入或不插入(这是一种打开/关闭),在某种意义上与 SQL Server 打开/关闭非常相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM