plsql怎么给主键自增
答案:2 悬赏:70 手机版
解决时间 2021-03-17 04:56
- 提问者网友:且恨且铭记
- 2021-03-17 02:06
plsql怎么给主键自增
最佳答案
- 五星知识达人网友:煞尾
- 2021-03-17 02:19
oracle没有ORACLE自增字段这样的功能,但是通过触发器(trigger)和序列(sequence)可以实现。
create table t_client (id number(4) primary key,
pid number(4) not null,
name varchar2(30) not null,
client_id varchar2(10),
client_level char(3),
bank_acct_no varchar2(30),
contact_tel varchar2(30),
address varchar2(30),
zip_code varchar2(10),
is_leaf char(1) default 'y' check (is_leaf in('y','n')),
is_client char(1) default 'n' check (is_client in('y','n')
));
假设关键字段为id,建一个序列,代码为:
create sequence seq_t_client_id
minvalue 1
maxvalue 99999999
start with 1
increment by 1
nocache
order;
建解发器代码为:
CREATE OR REPLACe TRIGGER trg_t_client_id
BEFORE INSERT ON t_client
FOR EACH ROW
BEGIN
SELECT seq_t_client_id.nextval INTO :new.id FROM dual;
END;
/
create table t_client (id number(4) primary key,
pid number(4) not null,
name varchar2(30) not null,
client_id varchar2(10),
client_level char(3),
bank_acct_no varchar2(30),
contact_tel varchar2(30),
address varchar2(30),
zip_code varchar2(10),
is_leaf char(1) default 'y' check (is_leaf in('y','n')),
is_client char(1) default 'n' check (is_client in('y','n')
));
假设关键字段为id,建一个序列,代码为:
create sequence seq_t_client_id
minvalue 1
maxvalue 99999999
start with 1
increment by 1
nocache
order;
建解发器代码为:
CREATE OR REPLACe TRIGGER trg_t_client_id
BEFORE INSERT ON t_client
FOR EACH ROW
BEGIN
SELECT seq_t_client_id.nextval INTO :new.id FROM dual;
END;
/
全部回答
- 1楼网友:等灯
- 2021-03-17 02:30
oracle没有oracle自增字段这样的功能,但是通过触发器(trigger)和序列(sequence)可以实现。
create table t_client (id number(4) primary key,
pid number(4) not null,
name varchar2(30) not null,
client_id varchar2(10),
client_level char(3),
bank_acct_no varchar2(30),
contact_tel varchar2(30),
address varchar2(30),
zip_code varchar2(10),
is_leaf char(1) default 'y' check (is_leaf in('y','n')),
is_client char(1) default 'n' check (is_client in('y','n')
));
假设关键字段为id,建一个序列,代码为:
create sequence seq_t_client_id
minvalue 1
maxvalue 99999999
start with 1
increment by 1
nocache
order;
建解发器代码为:
create or replace trigger trg_t_client_id
before insert on t_client
for each row
begin
select seq_t_client_id.nextval into :new.id from dual;
end;
/
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯