在Oracle中似乎没有AUTO_INCREMENT的概念,直到并且包括版本11g。
如何在Oracle 11g中创建一个行为像自动递增的列?
在Oracle中似乎没有AUTO_INCREMENT的概念,直到并且包括版本11g。
如何在Oracle 11g中创建一个行为像自动递增的列?
当前回答
这是我如何在一个现有的表和列(名为id)上做到这一点:
UPDATE table SET id=ROWNUM;
DECLARE
maxval NUMBER;
BEGIN
SELECT MAX(id) INTO maxval FROM table;
EXECUTE IMMEDIATE 'DROP SEQUENCE table_seq';
EXECUTE IMMEDIATE 'CREATE SEQUENCE table_seq START WITH '|| TO_CHAR(TO_NUMBER(maxval)+1) ||' INCREMENT BY 1 NOMAXVALUE';
END;
CREATE TRIGGER table_trigger
BEFORE INSERT ON table
FOR EACH ROW
BEGIN
:new.id := table_seq.NEXTVAL;
END;
其他回答
假设您指的是类似SQL Server标识列的列?
在Oracle中,您可以使用SEQUENCE来实现相同的功能。我看看能不能找到一个好的链接,然后贴在这里。
更新:看起来你自己找到了。这里是链接: http://www.techonthenet.com/oracle/sequences.php
这里有三种口味:
数字。简单递增的数值,例如1,2,3,.... GUID。全局通用标识符,作为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 FOO
for each row
begin
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 FOO
for each row
begin
select cast(sys_guid() as varchar2(32)) into :new.x from dual; -- string version
-- select sys_guid() into :new.x from dual; -- raw version
end;
/
更新:
Oracle 12c引入了两个不依赖触发器的变体:
create table mytable(id number default mysequence.nextval);
create table mytable(id number generated as identity);
第一个是传统的序列;第二个在内部管理值。
create trigger t1_trigger
before insert on AUDITLOGS
for each row
begin
select t1_seq.nextval into :new.id from dual;
end;
我只需要改变表名(AUDITLOGS)与你的表名和新的。使用new.column_name标识
FUNCTION GETUNIQUEID_2 RETURN VARCHAR2
AS
v_curr_id NUMBER;
v_inc NUMBER;
v_next_val NUMBER;
pragma autonomous_transaction;
begin
CREATE SEQUENCE sequnce
START WITH YYMMDD0000000001
INCREMENT BY 1
NOCACHE
select sequence.nextval into v_curr_id from dual;
if(substr(v_curr_id,0,6)= to_char(sysdate,'yymmdd')) then
v_next_val := to_number(to_char(SYSDATE+1, 'yymmdd') || '0000000000');
v_inc := v_next_val - v_curr_id;
execute immediate ' alter sequence sequence increment by ' || v_inc ;
select sequence.nextval into v_curr_id from dual;
execute immediate ' alter sequence sequence increment by 1';
else
dbms_output.put_line('exception : file not found');
end if;
RETURN 'ID'||v_curr_id;
END;
它被称为Identity Columns,只能从oracle 12c获得
CREATE TABLE identity_test_tab
(
id NUMBER GENERATED ALWAYS AS IDENTITY,
description VARCHAR2 (30)
);
插入到标识列的示例如下
INSERT INTO identity_test_tab (description) VALUES ('Just DESCRIPTION');
已创建1行。
你不能像下面这样插入
INSERT INTO identity_test_tab (id, description) VALUES (NULL, 'ID=NULL and DESCRIPTION');
第1行错误:ORA-32795:不能插入生成的always 标识列
INSERT INTO identity_test_tab (id, description) VALUES (999, 'ID=999 and DESCRIPTION');
第1行错误:ORA-32795:不能插入生成的always 标识列
有用的链接