欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Oracle如何创建表的自增主键ID—SEQ序列和Mybatis.xml的selectKey代码

程序员文章站 2022-07-03 20:11:06
Oracle创建Student学生表-- ------------------------------ Table structure for STUDENT_INFO-- ----------------------------DROP TABLE "SCOTT"."STUDENT_INFO";CREATE TABLE "SCOTT"."STUDENT_INFO" ( "STUDENT_ID" NUMBER NOT NULL, "STUDENT_NO" VARCHAR2(10 BYTE)...

Oracle创建Student学生表

-- ----------------------------
-- Table structure for STUDENT_INFO
-- ----------------------------
DROP TABLE "SCOTT"."STUDENT_INFO";
CREATE TABLE "SCOTT"."STUDENT_INFO" (
  "STUDENT_ID" NUMBER NOT NULL,
  "STUDENT_NO" VARCHAR2(10 BYTE),
  "STUDENT_NAME" VARCHAR2(14 BYTE)
)
TABLESPACE "USERS"
LOGGING
NOCOMPRESS
PCTFREE 10
INITRANS 1
STORAGE (
  INITIAL 65536 
  NEXT 1048576 
  MINEXTENTS 1
  MAXEXTENTS 2147483645
  BUFFER_POOL DEFAULT
)
PARALLEL 1
NOCACHE
DISABLE ROW MOVEMENT
;
COMMENT ON COLUMN "SCOTT"."STUDENT_INFO"."STUDENT_ID" IS '学生ID';
COMMENT ON COLUMN "SCOTT"."STUDENT_INFO"."STUDENT_NO" IS '学生编号';
COMMENT ON COLUMN "SCOTT"."STUDENT_INFO"."STUDENT_NAME" IS '学生名称';

-- ----------------------------
-- Primary Key structure for table STUDENT_INFO
-- ----------------------------
ALTER TABLE "SCOTT"."STUDENT_INFO" ADD CONSTRAINT "SYS_C0012237" PRIMARY KEY ("STUDENT_ID");

-- ----------------------------
-- Checks structure for table STUDENT_INFO
-- ----------------------------
ALTER TABLE "SCOTT"."STUDENT_INFO" ADD CONSTRAINT "SYS_C0012236" CHECK ("STUDENT_ID" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;
ALTER TABLE "SCOTT"."STUDENT_INFO" ADD CONSTRAINT "SYS_C0012238" CHECK ("STUDENT_ID" IS NOT NULL) NOT DEFERRABLE INITIALLY IMMEDIATE NORELY VALIDATE;

创建SEQ序列

CREATE SEQUENCE SEQ_STUDENT_INFO
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOMINVALUE
CACHE 20

Mybatis.xml

<!-- 新增学生信息 -->
<insert id="insertStudentInfo" parameterType="StudentInfo" useGeneratedKeys="true" keyProperty="studentId">
    <selectKey keyProperty="studentId" resultType="long" order="BEFORE">
        SELECT seq_student_info.NEXTVAL as studentId FROM DUAL
    </selectKey>
    insert into student_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="studentId != null "> student_id,</if>
        <if test="studentNo != null and studentNo != '' "> student_no,</if>
        <if test="studentName != null and studentName != '' "> student_name,</if>
      </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="studentId != null ">#{studentId},</if>
        <if test="studentNo != null and studentNo != '' ">#{studentNo},</if>
        <if test="studentName != null and studentName != '' ">#{studentName},</if>
      </trim>
</insert>

本文地址:https://blog.csdn.net/weixin_44316527/article/details/110665127

相关标签: oracle