oracle创建自增主键

本文介绍了如何在Oracle中创建自增主键,通过触发器和序列来实现。首先讲解了触发器的基本概念和创建权限,然后详细阐述了创建触发器的语法,特别是`BEFORE INSERT`子句的使用。接着讨论了相关名称(correlation names)和伪记录(pseudorecords),并给出了如何利用`SELECT INTO`语句将序列的当前值插入到新行的主键字段中的例子。最后,创建了一个名为`author_id_sequence`的序列,并修正了触发器的错误,使其能够在`author`表的行级触发器中正确工作。
摘要由CSDN通过智能技术生成

在网上查了oracle自增主键的列子,但是里面语句涉及的只是自己并不了解,所以本人结合着文档学习了一下oracle自增主键所涉及的一些知识。

首先是触发器:

Use the CREATE TRIGGER statement to create a database trigger, which is:

  • A stored PL/SQL block associated with a table, a schema, or the database or

  • An anonymous PL/SQL block or a call to a procedure implemented in PL/SQL or Java

Oracle Database automatically executes a trigger when specified conditions occur.

 

咱们要用的是与一个表相关的存储pl/sql语句块。

Prerequisites

To create a trigger in your own schema on a table in your own schema or on your own schema (SCHEMA), you must have the CREATE TRIGGER system privilege.

先决条件,在一个表上创建出发器必须有create trigger系统权限

If the trigger issues SQL statements or calls procedures or functions, then the owner of the trigger must have the privileges necessary to perform these operations. These privileges must be granted directly to the owner rather than acquired through roles.

如果触发器发表了一个sql语句或者程序或者函数,触发器的所有者必须有执行这些操作的权限。这些权限一定要直接授权给该所有者而不是通过角色获得。

大概了解了触发器,咱们看看语法

 

 

一下语法说明:

create_trigger

OR REPLACE

Re-creates the trigger if it exists, and recompiles it.

Users who were granted privileges on the trigger before it was redefined can still access the procedure without being regranted the privileges.

就是如果该触发器存在,重新创建,重新编译它

咱们的sql语句先写个初步:create or replace  trigger trigger_sequence

BEFORE

Causes the database to fire the trigger before running the triggering event. For row triggers, the trigger fires before each affected row is changed.

就是说执行触发器事件之前引起数据库调用该触发器。对于行触发器,触发器在每个受影响的行改变前调用。

触发器事件就是引起该触发器出发的操作,而行触发器就是如果触发器事件引起的每个行都会引起触发器出发

对于咱们是insert 一行之前要先把序列的值赋给该行的主键,也就是选中before

sql语句这回写成

:create or replace  trigger trigger_sequence before insert on author

 

WHEN (condition)

Specifies a SQL condition that the database evaluates for each row that the triggering statement affects. If the value of

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值