ITPub博客

首页 > 数据库 > 数据库开发技术 > DB2 列为null,无法设置该列为主键

DB2 列为null,无法设置该列为主键

原创 数据库开发技术 作者:fjzcau 时间:2015-03-28 20:41:10 0 删除 编辑
--创建表d,设置列dept 为 not null
db2inst1@sles11:~/sqllib> db2 "create table d (
     dept char(3) not null ,
     deptname char(20) not null,
     constraint c_dept_name  unique (deptname)
) in mb_data1"

--添加主键成功
db2inst1@sles11:~/sqllib> db2 "alter table d add constraint p_d_dept  primary key (dept)"

--删除表
db2inst1@sles11:~/sqllib> db2 drop table d                                              

--创建表d,列dept 允许为 null
db2inst1@sles11:~/sqllib> db2 "create table d (                                         
dept char(3) ,
deptname char(20) not null,
constraint c_dept_name  unique (deptname)
) in mb_data1"
DB20000I  The SQL command completed successfully.

--设置主键报错
db2inst1@sles11:~/sqllib> db2 "alter table d add constraint p_d_dept  primary key (dept)"
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  The column named "DEPT" cannot be a column of a primary key or
unique key constraint because it can contain null values.  SQLSTATE=42831

--添加not null 约束
db2inst1@sles11:~/sqllib> db2 "alter table d alter column dept set not null"            
DB20000I  The SQL command completed successfully.

--无法添加主键 为什么呢?
db2inst1@sles11:~/sqllib> db2 "alter table d add constraint p_d_dept  primary key (dept)"
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.D". 
SQLSTATE=57016

-----------------------------------------------------------------------------------------
--解决方案:重组表
db2 reorg table db2inst1.d

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22661144/viewspace-1477211/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: DB2 HADR搭建过程
请登录后发表评论 登录
全部评论

注册时间:2011-04-03

  • 博文量
    208
  • 访问量
    547159