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;
分享到:
相关推荐
id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20), sex TINYINT ); ``` 在这个例子中,`id`字段被定义为主键,并且具有`AUTO_INCREMENT`属性。每当向`student`表中插入新记录时,如果未显式提供`id`值,则...
在数据库设计中,自增主键(Auto Increment Primary Key)是一种常见的策略,用于为每条新插入的记录生成唯一的标识符。然而,自增主键并不总是连续的,这主要与数据库管理系统(如MySQL)如何处理自增值以及自增值...
mysql中索引是非常重要的知识点,相比其他的知识点,索引更难掌握,并且mysql中的索引种类也有很...logrecord_id int(11) NOT NULL auto_increment, user_name varchar(100) default NULL, operation_time datetime de
在MySQL数据库中,主键(Primary Key)是用于唯一标识表中每一行数据的重要属性。一个表只能有一个主键,并且主键的值必须是唯一的,不允许为空。当尝试为表定义多个主键或者主键设置不当时,系统可能会抛出...
id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), content TEXT ); ``` 这里的`id`列就是自动递增的,每次插入新的`blog_posts`记录时,`id`值都会自动增加。`PRIMARY KEY`约束确保了这个字段的唯一性。...
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, p_id VARCHAR(50), p_type VARCHAR(50), p_name VARCHAR(50), p_price INT(10), p_quantity INT(10), p_time VARCHAR(50) ); ``` 导出 SQL 文件 在 MySQL 中...
在MySQL中,AUTO_INCREMENT是用来指定一个字段为自动增长的字段,通常用于设置主键(primary key)。当向表中插入新记录时,无需手动指定这个字段的值,MySQL会自动为该字段生成一个唯一的递增值。 在Python中,...
id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, email VARCHAR(100) UNIQUE, register_date DATE ); ``` 2. **商品表(products)**:记录商品...
通常的设置自增字段的方法:创建表格时添加: 代码如下:create table table1(id int auto_increment primary key,…)创建表格后添加: 代码如下:alter table table1 add id int auto_increment primary key 自增字段...
学生(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, ...
id int primary key auto_increment, loginname varchar(32), password varchar(32) ); ## 商品类型表 create table goodstype( id int primary key auto_increment, name varchar(32) ); ## 商品表 ...
好久没有更新了,写点吧算是翻译吧,纯原创没空啊XD ... `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `filename` VARCHAR(255) NOT NULL, `title` VARCHAR(100) NOT NULL ); 文件的目录结构如下: public_html/
学生(Student):id(int, primary key, auto increment),Name(姓名,varchar),Birthday(出生日期,date),性别(Sex,varchar) b. 学生证(StudentIDCard):id(int, primary key, auto increment),StuNo(学号,long...
id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL UNIQUE ); ``` 接着,我们需要建立城市级别的数据。每个省下面有多个城市,所以城市信息应该关联到省份。为此,我们可以创建`city`表,包含`id`、...
1、已知有一个学生信息表(StudentInfo),其包含以下字段:id(int, primary key, auto increment),StuNo(学号,long),Name(姓名,varchar),Birthday(出生日期,date),dorm(宿舍地址,varchar),home(家庭地址,...
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 ...
`id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '用户id(主键)', `username` VARCHAR(50) COMMENT '用户姓名', `age` CHAR(3) COMMENT '用户年龄' ); -- 订单表(orders) CREATE TABLE `orders`( `id` INT AUTO_...
在PostgreSQL数据库中,"AUTO_INCREMENT"特性是通过序列(Sequences)来实现的,它在新记录插入到表中时会自动生成一个唯一的数字。序列是一种特殊的数据类型,可以生成一系列连续的整数,用于为表中的字段赋值。在...