sql问题,求教了

neyo123456 2011-03-24 01:44:34
要往数据库插入数据,写个sql能实现主键递增,例如,insert a(id,name)value(xxx,'san'),或是帮写个触发器来实现,谢谢了
...全文
92 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
changhe325 2011-03-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhuomingwang 的回复:]

SQL code

--1.先创建序列
scott@YPCOST> create sequence orderNo_seq start with 100 increment by 1 maxvalue 999;

序列已创建。

scott@YPCOST> create table test(id number,name varchar2(20));

表已创建。

--2、再加触发器
sco……
[/Quote]up
zty598416146 2011-03-24
  • 打赏
  • 举报
回复
楼主是想自己实现序列的功能?
  • 打赏
  • 举报
回复

--1.先创建序列
scott@YPCOST> create sequence orderNo_seq start with 100 increment by 1 maxvalue 999;

序列已创建。

scott@YPCOST> create table test(id number,name varchar2(20));

表已创建。

--2、再加触发器
scott@YPCOST> ed
已写入 file afiedt.buf

create or replace trigger insert_tri
before insert on test
for each row
declare
begin
select orderNo_seq.nextval into :new.id from dual;
end;
scott@YPCOST> /

触发器已创建

scott@YPCOST> insert into test(name) values('tom');

已创建 1 行。

scott@YPCOST> commit;

提交完成。

scott@YPCOST> select * from test;

ID NAME
-------------------- --------------------
100 tom
wangli20092010 2011-03-24
  • 打赏
  • 举报
回复
主键递增用 IDENTITY
例如:CREATE TABLE dbo.Tmp_t_t01
(
id int NOT NULL IDENTITY (1, 1),
id2 int NULL
) ON [PRIMARY]

17,090

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧