不允许设置db2主键问题的解决

设置db2主键问题出错的话,应该如何处理呢?下面就教您一个不允许设置db2主键问题的解决方法,希望对您学习设置db2主键方面有所帮助。

将empno设置为主键

db2 => alter table emp add primary key (empno)

DB21034E The command was processed as an SQL statement because it was not a

valid Command Line Processor command. During SQL processing it returned:

SQL0542N “EMPNO” cannot be a column of a primary key or unique key because it

can contain null values. SQLSTATE=42831

fenced  出现问题的原因是,因为empno列在创建的时候,默认支持值为空。而在DB2中,针对这种情况,是不允许创建主键的。这点也与oracle和mysql不同。

fenced  因此,我们要添加主键,需要先修改empno,添加上约束,让此列为非空:

db2 => alter table emp alter column empno set not null

将列修改了之后,尝试添加主键,再次出错:

db2 => alter table emp add primary key (empno)

DB21034E The command was processed as an SQL statement because it was not a

valid Command Line Processor command. During SQL processing it returned:

SQL0668N Operation not allowed for reason code “7” on table “DB2INST1.EMP”.

SQLSTATE=57016

解决办法:

查看错误信息代码SQL0668N,去查看具体的code“7”的含义。

db2 => ? SQL0668N

SQL0668N Operation not allowed for reason code “<reason-code>” on table

     “<table-name>”.

Explanation:

Access to table “<table-name>” is restricted. The cause is based on the

following reason codes “<reason-code>”:

1fenced

fenced  The table is in the Set Integrity Pending No Access state.

。。。。。内容省略。。。。。。。。

7fencedThe table is in the reorg pending state. This can occur after

fenced  an ALTER TABLE statement containing a REORG-recommended

fenced  operation.

User response:

1fencedExecute the SET INTEGRITY statement with the IMMEDIATE CHECKED

fenced  option on table “<table-name>” to bring the table out of the

。。。。。内容省略。。。。。。。。

7fencedReorganize the table using the REORG TABLE command.

fenced  For a table in the reorg pending state, note that the following

fenced  clauses are not allowed when reorganizing the table:

fenced  

fenced  * The INPLACE REORG TABLE clause

fenced  * The ON DATA PARTITION clause for a partitioned table when

fenced     table has nonpartitioned indexes defined on the table

通过查看上述描述中的红色部分的文字,emp表的empno列是添加了not null约束而导致表出于了“reorg pending state”这种状态。我们可以通过以下的方法来验证:

db2 => load query table emp

Tablestate:

Reorg Pending

解决办法,参照6.1中的“User response”描述,使用REORG TABLE命令:

db2 => reorg table emp

DB20000I The REORG command completed successfully.

修改完成后,再次添加主键:

db2 => alter table emp add primary key(empno)

DB20000I The SQL command completed successfully.

查看下表的状态,若为normal则能够正常得进行操作:

db2 => load query table emp

Tablestate:

Normal

总结:对表添加主键时出现的错误,是由于之前对表的列进行了修改,添加了非空约束,导致整个表出于“Reorg Pending”状态。

针对表出现这种状况时,使用reorg table命令,将表进行reorganize即可。
 

 

 

【编辑推荐】

DB2临时表定义的方法

linux下安装DB2的步骤

DB2性能优化的十大注意事项

SQL SERVER连接DB2数据库

DB2索引创建原则

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/279358.html<

(0)
运维的头像运维
上一篇2025-05-12 00:49
下一篇 2025-05-12 00:50

相关推荐

  • hosteonsVPS测评,实测体验,hosteonsVPS怎么样?

    Hosteons VPS 在 2026 年的实测表现显示,其依托全球 BGP 线路优化与 NVMe 全闪存架构,在亚洲至北美跨洋延迟控制上表现优异,是追求高性价比与稳定性的中小型企业首选,但需注意其部分机房在晚高峰期的波动风险,核心性能与网络架构深度解析在 2026 年云计算基础设施全面向 AI 算力与边缘计算……

    2026-05-02
    0
  • BaCloud独立服务器测评不限流量实测表现,BaCloud独立服务器不限流量怎么样

    2026 年实测结论:BaCloud 独立服务器在不限流量场景下表现优异,特别适合高并发视频流媒体与大数据传输业务,其性价比与稳定性在同类竞品中处于第一梯队,但需关注其节点覆盖密度,在 2026 年云计算市场进入存量博弈与精细化运营并存的阶段,企业用户对于“不限流量”的诉求已从单纯的带宽大小转向实际吞吐能力与计……

    2026-05-02
    0
  • 香港旅游好去处,香港自由行攻略,香港签证怎么办理

    2026 年香港作为全球顶级金融与科创枢纽,其核心优势在于“一国两制”下的资金自由流动、低税率环境及与国际市场无缝对接的法治体系,是跨境企业布局亚太的首选地,2026 香港宏观环境:政策红利与产业格局进入 2026 年,香港在巩固国际金融中心地位的同时,正加速向“国际创新科技中心”转型,根据香港特区政府统计处及……

    2026-05-02
    0
  • RamNode 是什么?RamNode 价格贵吗

    RamNode 在 2026 年依然是全球高性价比独立服务器首选,尤其适合预算有限但追求极致 I/O 性能与 99.9% 在线率的中小型跨境电商及游戏开发者,在 2026 年的云计算版图中,RamNode 凭借其独特的“内存优先”架构与极致的成本控制策略,继续稳固其在 VPS 市场的头部地位,对于寻求RamNo……

    2026-05-02
    0
  • RamNodeVPS测评,实测体验,RamNodeVPS怎么样,RamNodeVPS好用吗

    RamNode VPS 在 2026 年依然是高性价比的入门级选择,特别适合预算有限且对网络延迟敏感的小微开发者,但其在高并发场景下的稳定性略逊于一线云厂商,在 2026 年云计算市场格局重塑的背景下,RamNode 作为老牌 VPS 服务商,其“低价高配”的策略依然具有极强的市场穿透力,针对RamNode V……

    2026-05-02
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注