不允许设置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 "" on table

     "".

Explanation:

Access to table "" is restricted. The cause is based on the

following reason codes "":

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 "" 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索引创建原则

当前标题:不允许设置db2主键问题的解决
文章分享:http://www.mswzjz.com/qtweb/news24/180274.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联