Oracle sequence and table creation to modify table field attributes

Detailed Oracle database sequence

Sequences in the database

 

Introduction to sequence parameters:

CREATE SEQUENCE sequence //创建序列名称
[INCREMENT BY n] //递增的序列值是 n 如果 n 是正数就递增,如果是负数就递减 默认是 1
[START WITH n] //开始的值,递增默认是 minvalue 递减是 maxvalue
[{MAXVALUE n | NOMAXVALUE}] //最大值  
[{MINVALUE n | NOMINVALUE}] //最小值
[{CYCLE | NOCYCLE}] //循环/不循环
[{CACHE n | NOCACHE}];//分配并存入到内存中

Example of table creation:

-- create table
create table user.t_table_name_des
(
id number(36) not null,
fund_code varchar2(10) not null,
fund_name varchar2(64),
recommended_topic varchar2(1000) not null,
link_url varchar2(255) not null,
icon_url varchar2(255),
push_status number(1) default 0 not null,
client_ids CLOB,
create_date date not null,
update_date date not null,
constraint pk_table_name_des primary key (id)
);

-- add comments
comment on table user.t_table_name_des is '表名称';
comment on column user.t_table_name_des.id is '主键';
comment on column user.t_table_name_des.fund_code is '字段描述';
comment on column ...

-- create sequence
create sequence user.seq_table_name_des
minvalue 1
nomaxvalue 
start with 1
increment by 1 
cache 20 
order;

Remarks:

Define varchar(255) instead of varchar(256)  

In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.

与CHAR不同,VARCHAR值存储为1字节或2字节长度的前缀加上数据。长度前缀表示值中的字节数。如果值需要不超过255个字节,则列使用一个长度字节,如果值需要超过255个字节,则使用两个长度字节。

The difference between CLOB and BLOB

Both BLOB and CLOB are large field types, BLOB is stored in binary, and CLOB can directly store text. In fact, the two are interchangeable, or you can directly use the LOB field to replace the two. But in order to better manage the ORACLE database, usually information such as pictures, files, music, etc. are stored in BLOB fields, and the files are converted to binary first and then stored. As for articles or longer texts, CLOB is used for storage, which provides great convenience for future queries, updates, storage and other operations.

Table field attribute modification:

--添加字段
ALTER TABLE
    T_TABLE_NAME ADD(
        MODULE_TYPE NUMBER(4) default 1  NOT NULL,
        TOP_RANKING CHAR(2) default '0' NOT NULL
    );


--修改字段描述
COMMENT ON COLUMN T_TABLE_NAME.MODULE_TYPE is '字段描述';  
COMMENT ON COLUMN T_TABLE_NAME.TOP_RANKING is '字段描述'; 


--修改默认值
ALTER TABLE T_TABLE_NAME modify SORT_DIRECTION default '0'


--修改字段名
alter table T_TABLE_NAME rename column code to factor_code;


--修改字段类型属性
alter table T_TABLE_NAME modify(lab_name varchar2(64));

Oracle manually executes the SQL report ORA-01400: Cannot insert NULL into xxx.ID Problem:

SQL AUTO INCREMENT field

Introduction to Oracle INSERT statement

Oracle trigger usage examples detailed

ORA-01400: cannot insert NULL

  • Create the sequence corresponding to the table, and use the sequence name .NEXTVAL to assign a value to the ID parameter when adding
--从DUAL表中查看对应序列最新的值
SELECT SEQ_TABLE_NAME.NEXTVAL FROM SYS.DUAL;

INSERT
INTO T_TABLE_NAME
  (
    ID,
    FUND_CODE,
    FUND_NAME,
    SUBJECT,
    LINK_URL,
    ICON_URL,
    ONLINE_DATE,
    OFFLINE_DATE,
    IS_EFFECTIVE,
    CREATE_DATE,
    UPDATE_DATE
  )
  VALUES
  (
    SEQ_TABLE_NAME.NEXTVAL, 
    '888888',
    '名称',
    'XXX',
    'XXX',
    'XXX',
    to_date('06-1月 -21','DD-MON-RR'),
    to_date('11-1月 -21','DD-MON-RR'),
    0,
    to_date('11-1月 -21','DD-MON-RR'),
    to_date('11-1月 -21','DD-MON-RR')
  );
  • Establish the sequence corresponding to the table and create a trigger (the trigger will assign ID to the newly added data)

      At this time, add data, ID parameter can not be given

// 触发器SQL 待补充


INSERT
INTO T_TABLE_NAME
  (
    FUND_CODE,
    FUND_NAME,
    SUBJECT,
    LINK_URL,
    ICON_URL,
    ONLINE_DATE,
    OFFLINE_DATE,
    IS_EFFECTIVE,
    CREATE_DATE,
    UPDATE_DATE
  )
  VALUES
  (
    '888888',
    '名称',
    'XXX',
    'XXX',
    'XXX',
    to_date('06-1月 -21','DD-MON-RR'),
    to_date('11-1月 -21','DD-MON-RR'),
    0,
    to_date('11-1月 -21','DD-MON-RR'),
    to_date('11-1月 -21','DD-MON-RR')
  );

Corresponding Entity:

@Data
@Entity
@Table(name = "T_TABLE_NAME")
public class TTableName extends XxxBaseEntity {
    private static final long serialVersionUID = -588668888958562391L;

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_TABLE_NAME")
    @SequenceGenerator(name = "SEQ_TABLE_NAME", sequenceName = "SEQ_TABLE_NAME", allocationSize = 1)
    private Long id;

    /**
     * XXX
     */
    @Column(name = "FUND_CODE")
    private String fundCode;
    /**
     * XXX
     */
    @Column(name = "FUND_NAME")
    private String fundName;
    /**
     * XXX
     */
    @Transient //不存储到数据库,做临时变量
    private List<String> clientIds;
 
}

 

 

Guess you like

Origin blog.csdn.net/xiangwang2016/article/details/112470229