Oracle table 如何知道它与哪个序列关联?
How does an Oracle table know which sequence it is associated with?
我想知道 Oracle table(或列)如何知道哪个序列被使用或应用于特定的 table(或列)。
据我所知,序列 没有 系统地与 Oracle 中的特定 table 相关联。根据 the documentation:
Without sequences, sequential values can only be produced programmatically. A new primary key value can be obtained by selecting the most recently produced value and incrementing it. This method requires a lock during the transaction and causes multiple users to wait for the next value of the primary key; this waiting is known as serialization. If developers have such constructs in applications, then you should encourage the developers to replace them with access to sequences. Sequences eliminate serialization and improve the concurrency of an application.
因此应用程序的开发人员需要将每个序列与它们一起使用的 table(s) 相关联。通常这是通过使用以某种方式指示其用途的名称来完成的。因此,用于填充 emp
table 的主键的序列可能称为 emp_sequence
.
Oracle 12c 您可以创建带有标识子句的列。
create table MY_TABLE(
ID number generated always as identity
)
/
这将创建一个与该列关联的序列。您可以查询视图 ALL_TAB_IDENTITY_COLS 以找出系统生成的序列名称:
select owner, sequence_name
from ALL_TAB_IDENTITY_COLS
where owner='MY_SCHEMA'
and table_name='MY_TABLE'
and column_name='MY_COLUMN'
/
我想知道 Oracle table(或列)如何知道哪个序列被使用或应用于特定的 table(或列)。
据我所知,序列 没有 系统地与 Oracle 中的特定 table 相关联。根据 the documentation:
Without sequences, sequential values can only be produced programmatically. A new primary key value can be obtained by selecting the most recently produced value and incrementing it. This method requires a lock during the transaction and causes multiple users to wait for the next value of the primary key; this waiting is known as serialization. If developers have such constructs in applications, then you should encourage the developers to replace them with access to sequences. Sequences eliminate serialization and improve the concurrency of an application.
因此应用程序的开发人员需要将每个序列与它们一起使用的 table(s) 相关联。通常这是通过使用以某种方式指示其用途的名称来完成的。因此,用于填充 emp
table 的主键的序列可能称为 emp_sequence
.
Oracle 12c 您可以创建带有标识子句的列。
create table MY_TABLE(
ID number generated always as identity
)
/
这将创建一个与该列关联的序列。您可以查询视图 ALL_TAB_IDENTITY_COLS 以找出系统生成的序列名称:
select owner, sequence_name
from ALL_TAB_IDENTITY_COLS
where owner='MY_SCHEMA'
and table_name='MY_TABLE'
and column_name='MY_COLUMN'
/