简体   繁体   English

在Oracle 11g中使DB列的值自动递增1

[英]Make value of a DB column auto increment by 1 in Oracle 11g

I have Oracle DB 11g Enterprise Edition and I want to create a table by reading the sql script from following file: 我有Oracle DB 11g企业版,我想通过从以下文件中读取sql脚本来创建表:

CREATE SEQUENCE VerHist_SeqNum
   START WITH 1 
   INCREMENT BY 1;


CREATE TABLE VerHist
(
  SequenceNumber NUMBER(10,0)  NOT NULL,
  SQLFileName VARCHAR2(100)  NOT NULL,
  STATUS VARCHAR2(10)  NOT NULL,
  AppliedDate DATE  NOT NULL,
  DateCreated DATE 
   DEFAULT (SYSDATE),
  DateUpdated DATE 
   DEFAULT (SYSDATE),
  CONSTRAINT PK_VerHist PRIMARY KEY( SequenceNumber ),
  CONSTRAINT UC_VerHist_SQLFileNa UNIQUE( SQLFileName )
);


CREATE OR REPLACE TRIGGER VerHist_SeqNum_TRG
   BEFORE INSERT 
   ON VerHist
   FOR EACH ROW
   BEGIN
:NEW.SequenceNumber := VerHist_SeqNum.NEXTVAL;
   END;

Through java code I am reading above sql script and then executing following 3 queries: 通过java代码,我正在阅读sql脚本,然后执行以下3个查询:

  1. Create Sequence 创建序列
  2. Create Table 创建表
  3. Create Trigger which auto increment the 'SequenceNumber' when INSERT query is run 创建触发器,在运行INSERT查询时自动递增“SequenceNumber”

I have created this sql script by converting from my MS SQL Server script, where I only have to run a single query instead of 3 in this case. 我已经通过从我的MS SQL Server脚本转换创建了这个sql脚本,在这种情况下我只需运行一个查询而不是3。

My question is, Is it possible to not have Sequence and the Trigger just for the auto-increment purpose and can I simply modify my Create table query to do this, the way it is done in MS SQL using CONSTRAINT. 我的问题是,是否可以不将SequenceTrigger仅用于自动增量目的,我可以简单地修改我的Create table查询来执行此操作,就像使用CONSTRAINT在MS SQL中完成一样。

I want to do this because say if I drop the table and create new one then I would like to have the sequence restart from 1, whereas in current scenario it won't. 我想这样做是因为如果我删除表并创建新表,那么我希望序列从1重新启动,而在当前场景中它不会。

For Oracle 11g , you will have to use the approach you have used. 对于Oracle 11g ,您必须使用您使用的方法。 There is no auto-increment functionality available, to help you do away with the sequence and the trigger. 没有可用的自动增量功能,可帮助您取消序列和触发器。

As you say, you might want to drop the table, and want the sequence to start at 1, for that, you would need to recreate the sequence, so that it starts at desired value. 如您所说,您可能希望删除表,并希望序列从1开始,为此,您需要重新创建序列,以便它以所需的值开始。

Starting Oracle 12c, IDENTITY columns are available, which can help you implement auto-increment feature. 启动Oracle 12c,可以使用IDENTITY列,它可以帮助您实现自动增量功能。 For more details, refer to this 有关更多详细信息,请参阅此处

No, it is Not possible. 不,这是不可能的。

The syntax diagrams for creating a table in Oracle can be found here: http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_7002.htm 可以在此处找到在Oracle中创建表的语法图: http//docs.oracle.com/cd/E11882_01/server.112/e41084/statements_7002.htm

Looking into this specification from Oracle, it would be easy to see that the CREATE TABLE statement doesn't allow creating sequences or triggers as part of creating a table (and in fact, each of these tasks - creating a table, creating a sequence, and creating a trigger - are considered separate DDL commands in Oracle); 从Oracle查看此规范,很容易看出CREATE TABLE语句不允许创建序列或触发器作为创建表的一部分(实际上,每个任务 - 创建表,创建序列,并创建一个触发器 - 在Oracle中被视为单独的DDL命令); Nor does Oracle allow to define such a constraint for a column. Oracle也不允许为列定义这样的约束。

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

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