可能重复:
Autoincrement in Oracle
我在oracle中有一个表,我想让userid自动增加.. 一旦我通过使用序列使其自动递增并尝试使用过程插入到表中我将sample.seq_userid放入其中 我如何插入用户ID?我必须在我的程序中声明它吗?
PROCEDURE insertExample
(
name_in IN sample.name%TYPE,
age_in IN sample.age%TYPE
)
IS
BEGIN
INSERT INTO sample
(name, age)
VALUES
(name_in, age_in);
END insertExample;
这是我的更新
PROCEDURE updateExample
(
userid_in IN sample.userid%TYPE,
name_in IN sample.name%TYPE,
age_in IN sample.age%TYPE
)
IS
BEGIN
UPDATE sample
SET name = name_in,
age = age_in
WHERE userid = userid_in;
END updateExample;
答案 0 :(得分:3)
你需要一个序列:
create sequence seq_user_id start with 1 increment by 1;
和表格上的触发器
CREATE TRIGGER user_id_trg
BEFORE insert
ON sample
FOR EACH ROW
BEGIN
SELECT seq_user_id.NEXTVAL INTO :new.user_id FROM dual;
END;
/