oracle 主键自增

前端之家收集整理的这篇文章主要介绍了oracle 主键自增前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
注意:new后面的和主键对应

CREATE TABLE item (
iid INT,
title VARCHAR2(50),
CONSTRAINT pk_iid PRIMARY KEY (iid)
);
CREATE TABLE news (
nid INT,
title VARCHAR2(50),
pubdate DATE,
content LONG,
iid INT,
CONSTRAINT pk_nid PRIMARY KEY (nid),
CONSTRAINT fk_iid FOREIGN KEY (iid) REFERENCES item (iid)
ON DELETE SET NULL
);
-- 测试数据


--create sequence customer_id_seq increment by 1 start with 1 ;


create or replace
trigger trg_item before insert on item for each row
begin
select CUSTOMER_ID_SEQ.nextval into :new. iid from dual;
end;
/
create or replace
trigger trg_news before insert on news for each row
begin
select CUSTOMER_ID_SEQ.nextval into :new.nid from dual;
end;

/




INSERT INTO item (title) VALUES ('aaa'); INSERT INTO item (title) VALUES ('体育新闻'); INSERT INTO item (title) VALUES ('娱乐新闻'); INSERT INTO item (title) VALUES ('财经新闻'); INSERT INTO item (title) VALUES ('科技新闻');

猜你在找的Oracle相关文章