ORA-01442: column to be modified to NOT NULL is already NOT NULL
2012-06-26 11:20
537 查看
前两天自己想改变下表中一个字段的约束,没有想到就出现这个问题!当时一直在很奇怪,为什么desc看到的明明是可以为空,但是就是更改失败呢!后来才知道是因为约束失效导致的!今天特意又重新模拟了下!
1,模拟环境
create table test(name varchar2(20) not null novalidate);
-- 修改字段约束
SQL> alter table test modify name not null;
alter table test modify name not null
ORA-01442: column to be modified to NOT NULL is already NOT NULL
出现了ora-01442,这是因为在建表时,使用了约束失效(novalidate)的限制。
2,查询约束的当前状态信息
SQL> select constraint_name,constraint_type,status,validated
2 from user_constraints
3 where constraint_name in (select constraint_name from user_cons_columns where table_name='TEST' and column_name='NAME');
CONSTRAINT_NAME CONSTRAINT_TYPE STATUS VALIDATED
------------------------------ --------------- -------- -------------
SYS_C005247 C ENABLED NOT VALIDATED
约束的状态为ENABLED但是VALIDATED为无效,所以这个表的约束失效,导致可以插入空值!
3,解决问题
--将约束改为有效即可
SQL> alter table test enable validate constraint SYS_C005247;
Table altered
SQL> alter table test modify name null;
Table altered
SQL> desc test;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
NAME VARCHAR2(20) Y
ID INTEGER Y
SQL> alter table test modify name not null;
Table altered
SQL> desc test;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
NAME VARCHAR2(20)
ID INTEGER Y
所以当遇到修改约束不成功的时候,应该先看看约束是否失效,如果失效就必须将约束改为有效后才能修改约束条件!
1,模拟环境
create table test(name varchar2(20) not null novalidate);
-- 修改字段约束
SQL> alter table test modify name not null;
alter table test modify name not null
ORA-01442: column to be modified to NOT NULL is already NOT NULL
出现了ora-01442,这是因为在建表时,使用了约束失效(novalidate)的限制。
2,查询约束的当前状态信息
SQL> select constraint_name,constraint_type,status,validated
2 from user_constraints
3 where constraint_name in (select constraint_name from user_cons_columns where table_name='TEST' and column_name='NAME');
CONSTRAINT_NAME CONSTRAINT_TYPE STATUS VALIDATED
------------------------------ --------------- -------- -------------
SYS_C005247 C ENABLED NOT VALIDATED
约束的状态为ENABLED但是VALIDATED为无效,所以这个表的约束失效,导致可以插入空值!
3,解决问题
--将约束改为有效即可
SQL> alter table test enable validate constraint SYS_C005247;
Table altered
SQL> alter table test modify name null;
Table altered
SQL> desc test;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
NAME VARCHAR2(20) Y
ID INTEGER Y
SQL> alter table test modify name not null;
Table altered
SQL> desc test;
Name Type Nullable Default Comments
---- ------------ -------- ------- --------
NAME VARCHAR2(20)
ID INTEGER Y
所以当遇到修改约束不成功的时候,应该先看看约束是否失效,如果失效就必须将约束改为有效后才能修改约束条件!
相关文章推荐
- ORA-01442: column to be modified to NOT NULL is already NOT NULL
- ORA-54033要修改的列由某个虚拟表达式使用column to be modified is used in a virtual column expression
- ORA-01439: column to be modified must be empty to change datatype
- java.sql.SQLException: ORA-01799: a column may not be outer-joined to a subquery
- ORA-01439: column to be modified must be empty to change datatype
- ORA-01440: column to be modified must be empty to decrease precision or scale
- ora-01440:column to be modified must be empty to decrease precision or scale
- Optional int parameter 'folderId' is not present but cannot be translated into a null value due to b
- ORA-01439: column to be modified must be empty to change datatype
- "Firefox is already running but is not responding" error message - How to fix it
- myproject/gen already exists but is not a source folder. Convert to a source folder or rename it.
- AlertDialog显示错误 Unable to add window token null is not for an application
- Unable to add window -- token null is not for an application错误的解决方法 android开发
- 错误:Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is yo
- spark-shell on yarn 出错(arn application already ended,might be killed or not able to launch applic)解决
- PopupWindow出现Unable to add window -- token null is not valid; is your activity running?
- ORA-31623: a job is not attached to this session via the specified handle
- Unable to add window -- token null is not for an application错误的解决方法 android开发
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic