我在 derby Netbeans 中创建了一个表,现在我意识到我需要将一个列自动递增 1,这是一个主键。我该怎么做?我尝试了以下代码,但徒劳无功。
ALTER TABLE ISSUERECIPT ALTER IRCODE SET INCREMENT BY 1;
我需要再次创建表还是可以通过其他方式?
我找到了一个替代解决方案,我从数据库中删除了该列(感谢 vels4j)再次从 netbeans derby UI 添加了该列,如下所示:
ALTER TABLE ISSUERECIPT ADD IRCODE INTEGER NOT NULL primary key GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
我想可以为你做这些事
检查这个
ALTER TABLE ISSUERECIPT
ALTER IRCODE INTEGER NOT NULL
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);
如果你的桌子是空的,试试这个
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);
另请参阅: Derby ALTER TABLE 语法
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;
ALTER TABLE 语句无法将 IDENTITY 列添加到表中
如果您的表是空的或不在生产中。删除表并再次创建,例如:
DROP TABLE CUSTOMER;
CREATE TABLE CUSTOMER
(CUSTOMER_ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY
1),
FIRSTNAME VARCHAR(100) NOT NULL,VARCHAR(100),
PREFERRED_ID INTEGER,
CONSTRAINT primary_key PRIMARY KEY (CUSTOMER_ID)
);
试试这个 :
alter table ISSUERECIPT modify column IRCODE int(4) auto_increment
再次重新创建表,请参见下面的示例:
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)
) ;