oracle中:要修改为null的列无法修改为null

如图,求大神解答!

打开PL/SQL,写如下代码
declare
visnull varchar2(4);
begin
select nullable into visnull from user_tab_columns
where table_name = upper(‘tblStockInspect’)
and column_name = upper(‘FDepartID’);
if visnull = ‘N’ then
alter table tblStockInspect modify FDepartID int null;
end if; 
end; 

运行,又出现错误提示如下

ORA-06550: 第 8 行, 第 7 列: 

PLS-00103: 出现符号 “ALTER”在需要下列之一时:
( begin case declare exit
for goto if loop mod null pragma raise return select update
while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
仔细一看,原来alter不允许在PL/SQL下直接运行,只好更改如下
declare
visnull varchar2(4);
begin
select nullable into visnull from user_tab_columns
where table_name = upper(‘tblStockInspect’)
and column_name = upper(‘FDepartID’);
if visnull = ‘N’ then
execute immediate ‘alter table tblStockInspect modify FDepartID int null‘;
end if; 
end;  
运行通过

oracle中null值的问题

    先建一个用于测试的临时表:T1.表的内容如下:

    with t1 as(select 1 num1 from dual union select null num1 from dual union select 2 num1 from dual) select * from t1;


    如果我想找出num1不等于1的记录。该怎么去写sql呢?我尝试这样去写:select * from t1 where num1<>1;会得出什么结果呢?看下图:


    再一次的,我把sql这样写:select * from t1 where num1<>1 or num1 is null;再来看下结果:


    总结以上结果:NULL是不可以用来做比较的,无论什么值跟NULL作比较都会返回一个FALSE值。所以当记录中有NULL值的话且要处理的话要用is null来处理。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-12-16
你上面做了两次操作,第一次设置为not null,第二步又设置为null,所以现在ID是可以为空的,然后你现在又想要把它设置成可以为null就不行了。你的报错不全面,看看我拿到的error message:
01451. 00000 - "column to be modified to NULL cannot be modified to NULL"
*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.
所以你现在可以再试试,先把它设为not null,再设为null就不会有错了。追问

对对对,你这个报错就一针见血了!我是设了PK的缘故!吃完饭我再去试试!多谢!

本回答被提问者采纳
第2个回答  2015-10-26
id列你之前不是定义为不允许为null吗。。。追问

也就是说,建表时候如果定义了not null的话,以后就不能改了?

相似回答