简体   繁体   English

如何将PK添加到DB2中的现有表?

[英]How to add a PK to an existing table in DB2?

I am facing one problem. 我面临一个问题。 I have a table already create in DB2. 我已经在DB2中创建了一个表。

CREATE TABLE "DDL12"
(
 "D4_1" decimal(10,0),
 "D4_2" decimal(10,0),
);

I am trying to create a PK on this table as :- 我正在尝试在此表上创建一个PK:

ALTER TABLE "DDL12" ADD CONSTRAINT "Key4" PRIMARY KEY ("D4_1");

But while running the command, I am getting the error saying D4_1 is NULLABLE. 但是在运行命令时,出现错误消息说D4_1为NULLABLE。

Now, how can I create a PK on this table? 现在,如何在此表上创建PK?

Thanks 谢谢

Yes, this is due the fact, that your database "could have" rows having NULL value in that non PK column right now. 是的,这是由于事实,您的数据库现在在该非PK列中“可能”具有NULL值的行。

So first set the column to NOT NULL (+ make sure having a unique value in all rows) and then set the primary key with the command above. 因此,首先将列设置为NOT NULL(+确保在所有行中都有唯一的值),然后使用上面的命令设置主键。

You can change a column to not NULL like this: 您可以像这样将列更改为非NULL:

ALTER TABLE "DDL12"
 MODIFY "D4_1"   decimal(10,0)     NOT NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM