Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向Oracle中的现有表添加自动增量_Oracle_Oracle12c_Auto Increment - Fatal编程技术网

如何向Oracle中的现有表添加自动增量

如何向Oracle中的现有表添加自动增量,oracle,oracle12c,auto-increment,Oracle,Oracle12c,Auto Increment,是否有任何方法可以将autoincrement添加到Oracle 12c中已经存在的表中的主键。可以使用ALTER TABLE function或smth,我的意思是没有触发器和序列。据我所知,您可以不将现有主键列“修改”为“真实”标识列 如果要这样做,您必须删除当前主键列,然后更改表并添加新的标识列 解决方法是使用序列(或触发器),但是-你说过你不想这样做。无论如何,如果您决定使用它: SQL> create table test 2 (id number constrai

是否有任何方法可以将autoincrement添加到Oracle 12c中已经存在的表中的主键。可以使用ALTER TABLE function或smth,我的意思是没有触发器和序列。

据我所知,您可以将现有主键列“修改”为“真实”标识列

如果要这样做,您必须删除当前主键列,然后更改表并添加新的标识列


解决方法是使用序列(或触发器),但是-你说过你不想这样做。无论如何,如果您决定使用它:

SQL> create table test
  2   (id   number constraint pk_test primary key,
  3    name varchar2(10));

Table created.

SQL> insert into test values (1, 'LF');

1 row created.

SQL> create sequence seq_test start with 2;

Sequence created.

SQL> alter table test modify id default seq_test.nextval;

Table altered.

SQL> insert into test (name) values ('BF');

1 row created.

SQL> select * from test;

        ID NAME
---------- ----------
         1 LF
         2 BF

SQL>
或者,通过删除当前主键列(请注意,如果涉及外键,则不容易操作):

SQL> alter table test drop column id;

Table altered.

SQL> alter table test add id number generated always as identity;

Table altered.

SQL> select * From test;

NAME               ID
---------- ----------
LF                  1
BF                  2

SQL> insert into test (name) values ('test');

1 row created.

SQL> select * From test;

NAME               ID
---------- ----------
LF                  1
BF                  2
test                3

SQL>