当前位置:网站首页>Oracle 字段自增
Oracle 字段自增
2022-08-09 18:45:00 【Please Sit Down】
一、创建表
--创建student表
create table student(
id number not null, --学生id
name varchar2(20) not null --学生姓名
);
二、创建序列
create sequence student_id_seq; --创建序列
三、测试(向表中添加数据)
insert into student values (student_id_seq.nextval,'张三');
insert into student values (student_id_seq.nextval,'李四');
select * from student; --查询表
四、使用触发器创建自增字段
create or replace trigger student_auto_incr
before insert on student --before:执行DML等操作之前触发
for each row --行级触发器
begin
select student_id_seq.nextval into :new.id from dual;
end;
/
触发时机:
before:能够防止某些错误操作发生而便于回滚或实现某些业务规则,适用于实现自增字段;
after:在DML等操作发生之后发生,这种方式便于记录该操作或做某些事后处理信息;
instead of:触发器为替代触发器。
五、测试
insert into student(name) values ('张三');
insert into student (name) values ('李四');
select * from student;
边栏推荐
猜你喜欢
【Unity3D】2D动画
uniapp离线推送华为厂商申请流程
《痞子衡嵌入式半月刊》 第 60 期
IDEA快捷代码实时模板
What is the Treasure Project (TPC), a dark horse with wings in 2022!
Paper sharing: "FED BN" uses the LOCAL BATCH NORMALIZATION method to solve the Non-iid problem
基于CC2530 E18-MS1-PCB Zigbee DIY作品(三)
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
小满nestjs(第六章 nestjs cli 常用命令)
IS31FL3737B 通用12×12 LED驱动器 I2C 42mA 40QFN
随机推荐
laravel 中配置文件.env解读
awk use
Start cleaning up the long-term divers in the electronic chart development group again
competed中访问ref为undefined
新出现的去中心化科学能够为科学领域带来什么?
[免费专栏] Android安全之Root检测和绕过(浅析)
单调栈
奥特曼卡牌隐藏的百亿市场
[免费专栏] Android安全之数据存储与数据安全【大集合】
IDEA tools commonly used configuration
基于CC2530 E18-MS1-PCB Zigbee DIY作品(二)
Bi Sheng Compiler Optimization: Lazy Code Motion
队列题目:用队列实现栈
[免费专栏] Android安全之Android工程模式
ebook下载 | 《 企业高管IT战略指南——企业为何要落地DevOps》
Leetcode 739.每日温度 单调栈
技术分享 | 接口自动化测试如何处理 Header cookie
21天学习挑战赛--第四天打卡(横竖屏切换)
mysql duplicate data group multiple latest records
Intensive reading of the paper: VIT - AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE