打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何在MySQL&Oracle下创建自动递增字段_数据库认证_中国网管联盟youPC.cn
]
Tags:
如何在MySQL&Oracle下创建自动递增字段
在MySQL下创建自动递增字段:
bitsCN_net中国网管博客
create table article   //先创建一个表。
www.bitsCN.net网管博客等你来搏
(
id int primary key auto_increment,  //设置该字段为自动递增字段。
title varchar()
);
bitsCN_net中国网管博客
insert into article values (null,'a');     //向数据库中插入数据。
www.bitsCN.net网管博客等你来搏
select * from article;   结果如下:
Id www.bitsCN.net网管博客等你来搏
Title
a
insert into article values (null,’b’);
insert into article values (null,'c');
insert into article  (title)  values ('d'); bitsCN_com关注网管是我们的使命
select * from article;   结果如下:
feedom.net国内最早的网管网站
Id bitsCN_com关注网管是我们的使命
Title feedom.net国内最早的网管网站
bitsCN_com
a
feedom.net国内最早的网管网站
bitsCN_com关注网管是我们的使命
b
bitsCN_com关注网管是我们的使命
c
d bitsCN_net中国网管博客
但是oracle没有这样的功能,但是通过触发器(trigger)和序列(sequence)可以实现。
bitsCN_com
假设关键字段为id,建一个序列,代码为:
create sequence seq_test_ids
minvalue
maxvalue
start with
increment by
nocache
bitsCN_com
order;
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
建解发器代码为:
create or replace trigger tri_test_id
before insert on test_table
for each row
declare
nextid number;
begin
IF :new.id IS NULLor :new.id=0 THEN
select seq_test_id.nextval
into nextid
from sys.dual;
:new.id:=nextid;
end if;
end tri_test_id;
OK,上面的代码就可以实现自动递增的功能了。 bitsCN_com关注网管是我们的使命
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
利用分析函数在Oracle中分组排序取首条或末条记录行
Mysql中用int字段存日期
MYSQL INSERT INTO SELECT 不插入重复数据
MySQL中值得我们注意的SQL语句
数据库相关知识
MySql-Day-01
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服