SQL 在 derby 中更改自动递增 1 的表列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14316187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:08:03  来源:igfitidea点击:

Alter a table column with auto increment by 1 in derby

sqldatabasederby

提问by Nitesh Verma

I have created a table in derby Netbeans and now i realize that i need to make a column as auto incremented by 1 which is a primary key. How can i do so? I tried the following code but was in vain.

我在 derby Netbeans 中创建了一个表,现在我意识到我需要创建一个自动递增 1 的列,这是一个主键。我该怎么做?我尝试了以下代码但徒劳无功。

ALTER TABLE ISSUERECIPT ALTER IRCODE SET INCREMENT BY 1;

Do i need to create the table once again or can it be possible some other way?

我需要再次创建表格还是可以通过其他方式?

采纳答案by Nitesh Verma

I have found an alternate solution, i dropped the column from the database (thanks vels4j) added the column once again from the netbeans derby UI as shown below:

我找到了一个替代解决方案,我从数据库中删除了该列(感谢 vels4j)从 netbeans derby UI 中再次添加了该列,如下所示:

enter image description here

在此处输入图片说明

回答by FaizFizy

To alter the column to be auto-generated, the code is

要更改要自动生成的列,代码是

ALTER TABLE ISSUERECIPT ALTER IRCODE SET INCREMENT BY 1;

BUT the column must already be defined with the IDENTITY attribute (as written in this documentation).

但是必须已经使用 IDENTITY 属性定义了该列(如本文档中所述)。

In most cases (assuming that you too), the primary key column is not set as IDENTITY. Therefore, you may intend to alter the column to IDENTITY, but that is impossible.

在大多数情况下(假设您也是),主键列不会设置为 IDENTITY。因此,您可能打算将列更改为 IDENTITY,但这是不可能的。

The only way is to drop the table and create it again, as written here.

唯一的办法是删除表,并重新创建它,写在这里

回答by arvin_codeHunk

ALTER TABLE ISSUERECIPT  ADD IRCODE INTEGER NOT NULL primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),

I guess could do the things for you

我想可以为你做这些事情

回答by vels4j

Check this

检查这个

ALTER TABLE ISSUERECIPT 
ALTER IRCODE INTEGER NOT NULL 
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);

If your table is empty, Try this

如果你的桌子是空的,试试这个

ALTER TABLE DROP  PRIMARY KEY your_primaryKeyContrainName ; 
ALTER TABLE ISSUERECIPT DROP COLUMN IRCODE ;
ALTER TABLE ISSUERECIPT ADD COLUMN 
IRCODE PRIMARY KEY INTEGER NOT NULL 
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);

See Also :Derby ALTER TABLE Syntax

另请参见:德比 ALTER TABLE 语法

回答by Naveen Bangwal

ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;

ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;

回答by Michael Karaz

Recreate the table again see example below:

再次重新创建表,请参见下面的示例:

CREATE TABLE students
(
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
name VARCHAR(24) NOT NULL,
address VARCHAR(1024),
CONSTRAINT primary_key PRIMARY KEY (id)
) ;

回答by Asmita

Try this :

尝试这个 :

alter table ISSUERECIPT modify column IRCODE int(4) auto_increment