在现有 table 上创建序列 - ORA-00940: 无效的 ALTER 命令
Creating a sequence on an existing table - ORA-00940: invalid ALTER command
我创建了一个序列,我想要 table 来使用它。序列的创建工作正常。但是,当我尝试更改 table 以使用序列时,出现此错误(在 personInformationSequenceAlterTest
中):
ORA-00940: invalid ALTER command
请注意我需要使用 Java (Eclipse IDE)。
String personInformationSequenceTest =
"CREATE SEQUENCE seq_person "
+ "start with 1 "
+ "increment by 1 "
+ "NOCACHE "
+ "NOCYCLE ";
String personInformationSequenceAlterTest =
"alter table personInformationTest "
+ "alter column personId "
+ "set default nextval('seq_person')";
String personInformationSequenceOwnedTest =
"alter sequence seq_person owned by personInformationTest.personId";
您的 alter 语句有语法问题。
试试这个(假设该列的数据类型是 int。相应地更改):
alter table personInformationTest modify (personId int default seq_person.nextval);
这仅适用于 Oracle 12c 及更高版本。
对于 11g 或更低版本,您可以使用触发器。如果您不想使用触发器,您可以在插入中明确使用 seq_person.nextval
。
insert into personInformationTest (personId, . . .)
values (seq_person.nextval, . . .)
通过更改检查
String personInformationSequenceAlterTest =
"alter table personInformationTest "
+ "alter column personId "
+ "set default nextval('seq_person')";
到
String personInformationSequenceAlterTest =
"alter table personInformationTest "
+ "modify column personId "
+ "set default nextval('seq_person')";
在 Oracle 和 MySql 中,我们使用 "Modify" 来更改现有列。在 SQL Server/MS Access 中,使用 "Alter"。
我创建了一个序列,我想要 table 来使用它。序列的创建工作正常。但是,当我尝试更改 table 以使用序列时,出现此错误(在 personInformationSequenceAlterTest
中):
ORA-00940: invalid ALTER command
请注意我需要使用 Java (Eclipse IDE)。
String personInformationSequenceTest =
"CREATE SEQUENCE seq_person "
+ "start with 1 "
+ "increment by 1 "
+ "NOCACHE "
+ "NOCYCLE ";
String personInformationSequenceAlterTest =
"alter table personInformationTest "
+ "alter column personId "
+ "set default nextval('seq_person')";
String personInformationSequenceOwnedTest =
"alter sequence seq_person owned by personInformationTest.personId";
您的 alter 语句有语法问题。
试试这个(假设该列的数据类型是 int。相应地更改):
alter table personInformationTest modify (personId int default seq_person.nextval);
这仅适用于 Oracle 12c 及更高版本。
对于 11g 或更低版本,您可以使用触发器。如果您不想使用触发器,您可以在插入中明确使用 seq_person.nextval
。
insert into personInformationTest (personId, . . .)
values (seq_person.nextval, . . .)
通过更改检查
String personInformationSequenceAlterTest = "alter table personInformationTest " + "alter column personId " + "set default nextval('seq_person')";
到
String personInformationSequenceAlterTest = "alter table personInformationTest " + "modify column personId " + "set default nextval('seq_person')";
在 Oracle 和 MySql 中,我们使用 "Modify" 来更改现有列。在 SQL Server/MS Access 中,使用 "Alter"。