oracle查询表非空字段,记录一次删除Oracle表字段的非空约束-Oracle

记录一次删除Oracle表字段的非空约束

一个表表结构指定了某个字段(如cno)not null,同时加了check not null约束。

现在需要将not null约束去掉,要让cno字段可以插入空值。

于是执行了下面的语句:

Sql代码

1.alter table tableName drop constraints not_null_cons_cno;

2.alter table tableName modify cno null;

语句执行完毕,还是不能插入空值。

报如下错误:

ORA-01451: column to be modified to NULL cannot be modified to NULL

查询了Oracle的文档,描述如下:

Cause: the column may already allow NULL values, the NOT NULL constraint is part of a primary key or check constraint.

Action: if a primary key or check constraint is enforcing the NOT NULL constraint, then drop that constraint.

然后我发现这个字段与其他的字段组成了一个唯一性的组合索引。

于是,我修改这个索引,从中删掉cno字段。

1.删除主键;

Sql代码

alter table tableName drop constraints pk_tableName;

2.删除唯一索引;

Sql代码

drop index un_idx_table_name;

3.创建主键(不包括cno字段);

Sql代码

alter table tableName

add constraint PK_tableName primary key (DNO, TNO, YEAR, PNO);

4.创建唯一索引(不包括cno字段)。

Sql代码

create unique index ix_tableName on tableName (DNO, TNO, YEAR, PNO);

第3步报错了:

ORA-02437: cannot validate (TBOSDATA.PK_VOU_NUM) – primary key violated

这个错误是说主键或唯一键有重复记录。(我的唯一性约束作用在dno,tno,cno,year,pno几个字段。

查找重复的记录:

Sql代码

select *

from test vnt

where (vnt.dno, vnt.tno, vnt.year, vnt.pno) in

(select t.dno, t.tno, t.year, t.pno

from test t

group by t.dno, t.tno, t.year, t.pno

having count(*) > 1)

order by dno, tno, year, pno;

删除重复记录或修改重复记录,然后重新执行上面的3,4就OK了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值