在student 表添加一列:
alter table student add cla_id number;
把它设置为非空的:
alter table student modify cla_id not null;
设置为非空弹出错误窗口提示:
[img]http://dl.iteye.com/upload/attachment/0083/1677/a2db3682-fac3-33e2-a61f-741b8c1633d9.jpg[/img]
在student 表添加一列:
alter table student add cla_id number;
此时,cla_id这列都是空值。
把它设置为非空的:
alter table student modify cla_id not null;
由于上面你添加的列为空,而此时设置为非空,肯定报错了。
alter table的语法如下:
[code="java"]
alter table tablename add (column datatype [default value][null/not null],….);
[/code]
所以,你应该设置默认值:
alter table student add cla_id number default 0;
把那一列删掉,使用
alter table student add cla_id number not null default 0;
然后再更新cla_id
意思是说 你的cla_id有些行有空值 但你现在想not null 肯定搞不定
一
1、先把所有行改为非null
2、再执行
二 默认值
alter table student add cla_id number default 0;
not null 和 default 只能二者选一 default 表示null时的值 所以不可能为null
请问表中是否存在数据。如果存在数据。添加列后,会自己设置为空。你要先把添加的列设置为数字,然后再执行:alter table student modify cla_id not null;