Auto increment primary key

本文介绍如何在Oracle数据库中使用序列和触发器自动填充表的ID字段,并提供了创建序列、触发器的方法及如何直接在INSERT语句中利用序列值的示例。
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;
### SQL 中 `order_id INT AUTO_INCREMENT PRIMARY KEY` 的用法与示例 在 SQL 中,`AUTO_INCREMENT` 是一种机制,用于为表中的主键字段自动生成唯一的递增值。以下是对 `order_id INT AUTO_INCREMENT PRIMARY KEY` 语法和功能的详细说明。 #### 1. 语法解释 `order_id INT AUTO_INCREMENT PRIMARY KEY` 的含义如下: - `order_id`: 表示列名。 - `INT`: 定义列的数据类型为整数。 - `AUTO_INCREMENT`: 指定该列为自动递增列,每次插入新记录时,系统会自动为其生成一个唯一的递增值[^3]。 - `PRIMARY KEY`: 将该列定义为主键,确保每行数据的唯一性[^4]。 #### 2. 创建包含 `AUTO_INCREMENT` 主键的表 以下是一个完整的示例,展示如何创建一个包含 `AUTO_INCREMENT` 主键的表: ```sql CREATE TABLE orders ( order_id INT AUTO_INCREMENT PRIMARY KEY, user_id INT NOT NULL, product_name VARCHAR(50) NOT NULL, quantity INT NOT NULL, order_date DATE NOT NULL ); ``` - `order_id` 被定义为 `AUTO_INCREMENT` 和 `PRIMARY KEY`,确保其值唯一且自动递增[^3]。 - 其他字段如 `user_id`, `product_name`, `quantity`, 和 `order_date` 则存储订单相关的具体信息。 #### 3. 插入数据 在插入数据时,无需为 `order_id` 提供值,系统会自动为其生成唯一的递增值。例如: ```sql INSERT INTO orders (user_id, product_name, quantity, order_date) VALUES (1, 'Laptop', 2, '2023-10-01'); ``` 执行上述语句后,`order_id` 会被自动赋值为 1(如果这是第一条记录)。再次插入时,`order_id` 将递增为 2,依此类推。 #### 4. 注意事项 - **唯一性**:`AUTO_INCREMENT` 列的值必须是唯一的,不能重复。 - **起始值**:默认情况下,`AUTO_INCREMENT` 的起始值为 1,可以通过 `ALTER TABLE` 修改起始值[^3]。例如: ```sql ALTER TABLE orders AUTO_INCREMENT = 100; ``` - **锁定机制**:在 InnoDB 存储引擎中,`AUTO_INCREMENT` 的处理涉及锁定机制,以确保并发插入时不会产生冲突[^5]。 #### 5. 示例验证 假设我们已经创建了 `orders` 表,并插入了几条数据: ```sql INSERT INTO orders (user_id, product_name, quantity, order_date) VALUES (2, 'Phone', 1, '2023-10-02'); INSERT INTO orders (user_id, product_name, quantity, order_date) VALUES (3, 'Tablet', 3, '2023-10-03'); ``` 查询表中的数据: ```sql SELECT * FROM orders; ``` 结果可能如下: | order_id | user_id | product_name | quantity | order_date | |----------|---------|--------------|----------|------------| | 1 | 1 | Laptop | 2 | 2023-10-01 | | 2 | 2 | Phone | 1 | 2023-10-02 | | 3 | 3 | Tablet | 3 | 2023-10-03 | #### 6. 常见问题 - 如果删除了某条记录,`AUTO_INCREMENT` 的值不会重用。例如,删除 `order_id = 2` 的记录后,插入新记录时,`order_id` 会从上次的最大值继续递增。 - 在多用户并发环境下,`AUTO_INCREMENT` 的实现依赖于数据库的锁定机制,确保数据的一致性和完整性[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值