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

数据库
db2主键在设置时,有时候会提示错误,无法创建,下面就叫一个解决不允许设置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索引创建原则

责任编辑:段燃 来源: 互联网
相关推荐

2011-03-01 14:12:12

FreebsdProftpd

2010-05-20 13:03:52

IIS父路径

2010-06-01 16:12:00

2023-08-22 20:43:09

HashMap单线程null

2010-11-11 16:53:28

SQL Server视

2022-05-08 18:18:40

JDKValueHashMap

2009-09-22 15:54:42

CCIE笔试

2010-08-10 17:10:46

2010-08-20 10:18:32

DB2自动增长主键

2010-08-13 14:46:08

DB2 -964

2010-08-03 13:23:47

DB2自动增长主键

2020-08-20 11:12:14

iOS 13.6苹果降级

2014-06-30 14:53:49

Android定制google

2010-11-02 09:45:07

DB2 logfils

2010-09-06 17:24:48

连接DB2数据库

2010-08-19 13:14:17

DB2自动增长主键

2010-11-02 15:28:28

DB2 sequenc

2011-04-22 10:15:56

Novell专利

2009-06-18 10:47:44

java接口定义变量

2022-01-27 07:02:52

JavaHashMap单线程
点赞
收藏

51CTO技术栈公众号