`
fantasyday
  • 浏览: 32886 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

Auto increment primary key

阅读更多
1. Let's say we have a table called "test" with two columns, id and testdata. (This is just a dumb quick example, so I won't bother to specify any constraints on id.)

create table test (id number, testdata varchar2(255));

2. Next we'll create a sequence to use for the id numbers in our test table.

create sequence test_seq
start with 1
increment by 1
nomaxvalue;

You could change "start with 1" to any number you want to begin with (e.g. if you already have 213 entries in a table and you want to begin using this for your 214th entry, replace with "start with 214"). The "increment by 1" clause is the default, so you could omit it. You could also replace it with "increment by n" if you want it to skip n-1 numbers between id numbers. The "nomaxvalue" tells it to keep incrementing forever as opposed to resetting at some point. i (I'm sure Oracle has some limitation on how big it can get, but I don't know what that limit is).

3. Now we're ready to create the trigger that will automatically insert the next number from the sequence into the id column.

create trigger test_trigger
before insert on test
for each row
begin
select test_seq.nextval into :new.id from dual;
end;
/

Greg Malewski writes:

You've demonstrated an implementation using triggers. This is not necessary, since instead it can be included as part of the INSERT statement. Using your example, my INSERT statement would be:

   
insert into test values(test_seq.nextval, 'voila!');


Here are a couple of questions the above might raise. This is pretty intuitive stuff, but I'm aiming it at the Oracle newbie since no expert would be reading this page anyway.

How do you tell what sequences and triggers are already out there?

select sequence_name from user_sequences;
select trigger_name from user_triggers;

How do you get rid of a sequence or trigger you created?

drop sequence test_seq;
drop trigger test_trigger;

Again, replace test_seq and test_trigger with the specific names you used. You can also keep the trigger but disable it so it won't automatically populate the id column with every insert (and enable it again later if you want):

alter trigger test_trigger disable;
alter trigger test_trigger enable;
分享到:
评论

相关推荐

    MIS学生信息管理系统

    id int (5)auto_increment primary key, username varchar(20), userpassword varchar(20), studentmaggerrole varchar(10) ) //班级信息表 create table class ( id int (5)auto_increment primary key, deptid ...

    mysql中key 、primary key 、unique key 与index区别

    mysql中索引是非常重要的知识点,相比其他的知识点,索引更难掌握,并且mysql中的索引种类也有很...logrecord_id int(11) NOT NULL auto_increment, user_name varchar(100) default NULL, operation_time datetime de

    mysql自增ID起始值修改方法

    通常的设置自增字段的方法:创建表格时添加: 代码如下:create table table1(id int auto_increment primary key,…)创建表格后添加: 代码如下:alter table table1 add id int auto_increment primary key 自增字段...

    MyBatis的关联映射(含源码及结果图)

    学生(Student):id(int, primary key, auto increment),Name(姓名,varchar),Birthday(出生日期,date),性别(Sex,varchar) b. 学生证(StudentIDCard):id(int, primary key, auto increment),StuNo(学号,long...

    韩顺平的满汉楼源代码

    表名:chart2010 语句:id int primary key auto_increment, num int 表名:menu 语句: id int primary key auto_increment, name varchar(20), cost varchar(10) 表名:uid 语句: id int primary key auto_increment, ...

    jquery + struts2 + spring + hibernate 电子商务 - 购物车模块

    id int primary key auto_increment, loginname varchar(32), password varchar(32) ); ## 商品类型表 create table goodstype( id int primary key auto_increment, name varchar(32) ); ## 商品表 ...

    MyBatis的关联映射

    学生(Student):id(int, primary key, auto increment),Name(姓名,varchar),Birthday(出生日期,date),性别(Sex,varchar) b. 学生证(StudentIDCard):id(int, primary key, auto increment),StuNo(学号,long...

    MyBatis数据库操作与核心配置

    1、已知有一个学生信息表(StudentInfo),其包含以下字段:id(int, primary key, auto increment),StuNo(学号,long),Name(姓名,varchar),Birthday(出生日期,date),dorm(宿舍地址,varchar),home(家庭地址,...

    qor-cms:qor-cms使用qor开发一个cms系统

    CREATE TABLE IF NOT EXISTS category ( id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(200) NOT NULL, description varchar(300));CREATE TABLE IF NOT EXISTS article ( id bigint(20) NOT ...

    MySQL 高级多表查询

    `id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '用户id(主键)', `username` VARCHAR(50) COMMENT '用户姓名', `age` CHAR(3) COMMENT '用户年龄' ); -- 订单表(orders) CREATE TABLE `orders`( `id` INT AUTO_...

    2023年全新MySQL大厂面试经验

    id int auto_increment primary key, first_name varchar(16), last_name VARCHAR(16), id_card VARCHAR(18), information text ); -- 更改表结构 alter table user_index -- 创建一个first_name和last_name的...

    愿望清单

    愿望清单项目创建数据库: 愿望清单创建用户: 创建用户'phpuser'@'localhost'由'phpuserpw'标识创建餐桌许愿者: CREATE TABLE wishers(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,名称CHAR(50)CHARACTER SET...

    python mysql自增字段AUTO_INCREMENT值的修改方式

    在之前得文章中我们说过,如果使用delete对数据库中得表进行删除,那么只是把记录删除掉,并且id的值...create table t4(id int auto_increment primary key, num int) engine=innodb default charset=utf8; 那么接下

    基于jsp的人事管理系统

    create table user(id int primary key auto_increment,username varchar(20),password varchar(20)); insert into user(name,password) values(admin,admin); create table personnel(personnelid int primary ...

    jdbc写的模拟银行转账系统

    bankId int primary key auto_increment, bankname varchar(13), bankareaId int, foreign key(bankareaId)references bankarea(bankareaId) ); insert into bank values(1,'中国农业银行',1),(2,'交通银行',2...

    mysql 全国地区表地区数据库表

    PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3784 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of b_region -- ---------------------------- INSERT INTO `b_region` VALUES ('1'...

    struts2_spring2.5_ibatis2.3_mysql架构

    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, userName varchar(50), password varchar(50) ) insert into users(userName,password) value('hj','123'); insert into users(userName,password) value('wtt','123...

    初级java笔试题-online-bookstore:网上书店

    AUTO_INCREMENT PRIMARY KEY, category_name VARCHAR(255) NULL DEFAULT NULL); - 创建书表 tbl_book 如果不存在则创建表productDetails 。 tbl_book ( id BIGINT(20)NOT NULL AUTO_INCREMENT PRIMARY KEY,

    HairSalon:Epicodus第11周个展项目

    美发沙龙 沙龙的AC#Web应用程序,... 创建表客户端(clientID int(11)NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255),stylistID int(11)DEFAULT'0'); 导航到目录 运行“ dotnet run”命令以在命

    合租记账管理系统(java+mysql)

    Id INT AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(30) NOT NULL, Pay FLOAT NOT NULL, day DATE NOT NULL ); 功能: 1.花费金额记录(时间系统自动记录); 2.月底结算。 适用对象: 与他人合租者.

Global site tag (gtag.js) - Google Analytics