如何在Oracle上使用自动增量创建id?在Oracle中似乎没有自动增量的概念,直到并包括版本11g。如何在Oracle 11g中创建行为类似自动增量的列?
4 回答
慕仙森
TA贡献1827条经验 获得超7个赞
SYS_GUID
SYS_GUID
RAW(16)
CREATE SEQUENCE name_of_sequence START WITH 1 INCREMENT BY 1 CACHE 100;
INSERT
INSERT INTO name_of_table( primary_key_column, <<other columns>> ) VALUES( name_of_sequence.nextval, <<other values>> );
CREATE OR REPLACE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROWBEGIN SELECT name_of_sequence.nextval INTO :new.primary_key_column FROM dual;END;
CREATE OR REPLACE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROWBEGIN :new.primary_key_column := name_of_sequence.nextval;END;
SYS_GUID
CREATE TABLE table_name ( primary_key_column raw(16) default sys_guid() primary key, <<other columns>>)
噜噜哒
TA贡献1784条经验 获得超7个赞
CREATE TABLE MAPS( MAP_ID INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL, MAP_NAME VARCHAR(24) NOT NULL, UNIQUE (MAP_ID, MAP_NAME));
-- create tableCREATE TABLE MAPS( MAP_ID INTEGER NOT NULL , MAP_NAME VARCHAR(24) NOT NULL, UNIQUE (MAP_ID, MAP_NAME));-- create sequenceCREATE SEQUENCE MAPS_SEQ;-- create tigger using the sequenceCREATE OR REPLACE TRIGGER MAPS_TRG BEFORE INSERT ON MAPS FOR EACH ROWWHEN (new.MAP_ID IS NULL)BEGIN SELECT MAPS_SEQ.NEXTVAL INTO :new.MAP_ID FROM dual;END;/
汪汪一只猫
TA贡献1898条经验 获得超8个赞
数字
。简单增加数值,例如1,2,3,. 吉德
。全局Univeral标识符,作为 RAW
数据类型。 GUID(字符串)
。和上面一样,但是作为一个字符串,在某些语言中可能更容易处理。
x
FOO
-- numerical identity, e.g. 1,2,3...create table FOO ( x number primary key);create sequence FOO_seq;create or replace trigger FOO_trg before insert on FOOfor each rowbegin select FOO_seq.nextval into :new.x from dual;end;/-- GUID identity, e.g. 7CFF0C304187716EE040488AA1F9749A-- use the commented out lines if you prefer RAW over VARCHAR2.create table FOO ( x varchar(32) primary key -- string version -- x raw(32) primary key -- raw version);create or replace trigger FOO_trg before insert on FOOfor each rowbegin select cast(sys_guid() as varchar2(32)) into :new.x from dual; -- string version -- select sys_guid() into :new.x from dual; -- raw versionend;/
create table mytable(id number default mysequence.nextval);create table mytable(id number generated as identity);
添加回答
举报
0/150
提交
取消