在Oracle SQL数据库中插入/更新记录?

时间:2018-11-16 13:53:53

标签: oracle oracle-sqldeveloper

我最近开始使用Oracle。在此之前,我曾使用SQL Server 2008/2012。我注意到Oracle和SQL Server之间存在一些差异。我在SQL Server中创建了具有基本列的表,例如“名字”,“姓氏”,“电子邮件”等。这是我的表结构示例:

Name          Data Type  Size   Not Null
RECORDID      NUMBER            true    //This is primary key (auto increment)  
FIRST         VARCHAR2   50     true        
LAST          VARCHAR2   50     true        
EMAIL         VARCHAR2   320    true        
PHONE         CHAR       10     true        
FILEPATH      VARCHAR2   1000   false       
TYPE          CHAR       1      true        
SUBJECT       VARCHAR2   100    true        
DESCRIPTION   VARCHAR    4000   true    // This should be varchar(max)  
ACTIONDATE    DATE              true    

我已经在Oracle SQL Developer中使用Identity Column选项将RecordID设置为Column Sequence。这与SQL Server不同,我正在寻找相同的行为。该列应为添加到表中的每个新行自动递增。

在Oracle中设置标识列的正确方法是吗?这是我的插入语句的示例:

<cfquery name="insertRec" datasource="test">
    INSERT INTO Table1 (
        RecordID, First, Last, Email, Phone, 
        FilePath, Type, Subject, Description, ActionDate 
    )VALUES(
        RecID_SEQ1.NEXTVAL,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="trim(#form.first#)" maxlength="50">,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="trim(#form.last#)" maxlength="50">,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="trim(#form.email#)" maxlength="50">,
        <cfqueryparam cfsqltype="cf_sql_char" value="trim(#form.phone#)" maxlength="10">,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="trim(#form.file#)" maxlength="1000">,
        <cfqueryparam cfsqltype="cf_sql_char" value="trim(#form.type#)" maxlength="1">,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="trim(#form.subject#)" maxlength="100">,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="trim(#form.appdescr#)" maxlength="4000">,
        CURRENT_TIMESTAMP
    )
</cfquery>

在上面的交易中如何设置自动增量ID?我也想返回与SQL Server中相同的作用域标识。有没有办法在Oracle中实现这一目标?如果有人对如何实现上述建议或如何改进我的代码有任何建议,请告诉我。我刚开始使用Oracle,这对我来说是新的。

CREATE TABLE "MYDB"."MYTABLE" 
   (    "RECORDID" NUMBER, 
    "FIRST" VARCHAR2(20 BYTE), 
    "LAST" VARCHAR2(20 BYTE), 
    "EMAIL" VARCHAR2(20 BYTE), 
    "PHONE" CHAR(10 BYTE), 
    "FILEPATH" VARCHAR2(20 BYTE), 
    "TYPE" CHAR(1 BYTE), 
    "SUBJECT" VARCHAR2(20 BYTE), 
    "DESCRIPTION" VARCHAR2(20 BYTE), 
    "ACTIONDATE" DATE, 
    "PRIORITY" CHAR(1 BYTE)
   ) SEGMENT CREATION DEFERRED 
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  TABLESPACE "MYDB" ;
--------------------------------------------------------
--  DDL for Index MYTABLE_PK
--------------------------------------------------------

  CREATE UNIQUE INDEX "MYDB"."MYTABLE_PK" ON "MYDB"."MYTABLE" ("RECORDID") 
  PCTFREE 10 INITRANS 2 MAXTRANS 255 NOCOMPRESS LOGGING
  TABLESPACE "MYDB" ;
--------------------------------------------------------
--  DDL for Trigger MYTABLE_TRG
--------------------------------------------------------

  CREATE OR REPLACE TRIGGER "MYDB"."MYTABLE_TRG" 
BEFORE INSERT ON MYTABLE
FOR EACH ROW 
BEGIN
  <<COLUMN_SEQUENCES>>
  BEGIN
    NULL;
  END COLUMN_SEQUENCES;
END;
/
ALTER TRIGGER "MYDB"."MYTABLE_TRG" ENABLE;
--------------------------------------------------------
--  DDL for Trigger MYTABLE_TRG1
--------------------------------------------------------

  CREATE OR REPLACE TRIGGER "MYDB"."MYTABLE_TRG1" 
BEFORE INSERT ON MYTABLE
FOR EACH ROW 
BEGIN
  <<COLUMN_SEQUENCES>>
  BEGIN
    IF INSERTING AND :NEW.RECORDID IS NULL THEN
      SELECT ADPR_SEQ1.NEXTVAL INTO :NEW.RECORDID FROM SYS.DUAL;
    END IF;
  END COLUMN_SEQUENCES;
END;
/

ALTER TRIGGER "MYDB"."MYTABLE_TRG1" ENABLE;

2 个答案:

答案 0 :(得分:2)

要进一步解释jeffsmith所说的内容,您不要有一个Identity列。实际上,您甚至没有主键约束,只有唯一的列。身份列如下所示:

create table mytable (
    recordid number generated always by default on null as identity ...

然后,在创建新行时,无需在INSERT语句中指定该列。

如果您要使用此触发序列模式插入列,则可以通过执行以下操作来获取新的ID:

insert into mytable ( first, ... ) 
values ( 'john', ... )
returning recordid into :myrecordid

这将用新行的记录ID填充myrecordid

答案 1 :(得分:1)

这就是您想要的-它在数据库版本12c及更高版本中可用。

enter image description here

生成的代码看起来像这样-

CREATE TABLE TABLE4 
(
  COLUMN1 NUMBER GENERATED ALWAYS AS IDENTITY INCREMENT BY 1 START WITH 1 MINVALUE 1 NOT NULL 
, COLUMN2 VARCHAR2(20) 
, CONSTRAINT TABLE4_PK PRIMARY KEY 
  (
    COLUMN1 
  )
  ENABLE 
);

然后进行插入,并取回生成的ID-

declare
 my_new_record integer;
begin
 insert into table4(column2) values ('Hi') returning column1 into my_new_record;
 dbms_output.put_line('your new record ID is: ' || my_new_record);
end;
/

enter image description here

如果您不在12c上,则仍然可以执行所有操作-您只需创建和维护TRIGGER和SEQUENCE。