Home > Database > Mysql Tutorial > Oracle创建主键自增表(sql语句实现)及触发器应用

Oracle创建主键自增表(sql语句实现)及触发器应用

WBOY
Release: 2016-06-07 17:56:11
Original
1132 people have browsed it

主键自增在插入数据的时候是很实用的,可以获取并操作返回的插入记录的ID,接下来介绍Oracle如何创建主键自增表,感兴趣的你可以了解下,就当是巩固知识,希望此文对你有所帮助

1、创建表
代码如下:
createtableTest_Increase(
useridnumber(10)NOTNULLprimarykey,/*主键,自动增加*/
usernamevarchar2(20)
);

2、创建自动增长序列
代码如下:
CREATESEQUENCETestIncrease_Sequence
INCREMENTBY1--每次加几个
STARTWITH1--从1开始计数
NOMAXVALUE--不设置最大值,设置最大值:maxvalue9999
NOCYCLE--一直累加,不循环
CACHE10;

3、创建触发器
代码如下:
CREATETRIGGERTest_IncreaseBEFORE
insertONTest_IncreaseFOREACHROW/*对每一行都检测是否触发*/
begin
selectTestIncrease_Sequence.nextvalinto:New.useridfromdual;
end;
//*退出sqlplus行编辑*/

4、提交
代码如下:
commit;

5、测试
insertinto
代码如下:
Test_Increase(Username)values('test');
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template