DB2 添加主键

您所在的位置:网站首页 圣安东尼奥马刺微博 DB2 添加主键

DB2 添加主键

#DB2 添加主键 | 来源: 网络整理| 查看: 265

1.问题 需要对表进行增加主键时,使用如下语句:

alter table TABLENAME add primary key (PK_FIELD);

发生错误:

-542, SQLSTATE=42831

解释: -542 42831 :可以为空的列不允许作为主键的一部分包含在内 检查表结构 发现 PK_FIELD 确实是允许为空

SELECT * from SYSIBM.SYSCOLUMNS where TBNAME='TABLENAME';

在这里插入图片描述

2.解决方案

1. 将Allow null 改为 not null 执行

alter table TABLENAME ALTER PK_FIELD set not null;

网上也有如下语句,但发现在有数据的情况下无法更正 allow null为not null

alter table TABLENAME add unique(PK_FIELD);

2. 设置主键

alter table TABLENAME add primary key (PK_FIELD);

*如果在执行alert后发现出现错误 SQL0668N Operation not allowed for reason code “7” on table XXX 请重新整理下表

call sysproc.admin_cmd ('reorg table TABLENAME');

综上所述 大致过程如下:

alter table TABLENAME ALTER PK_FIELD set not null; alter table TABLENAME add primary key (PK_FIELD); call sysproc.admin_cmd ('reorg table TABLENAME');


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3