How to add a not null constraint on column containing null values

痴心易碎 提交于 2019-12-03 22:25:05

You can add an unvalidated constraint - it will not look at existing rows, but it will be checked for any new or updated rows.

ALTER TABLE mytable MODIFY mycolumn NOT NULL NOVALIDATE;

Just be aware that you won't be able to update an existing row unless it satisfies the constraint.

Also, be aware of the downside that the optimizer will not be able to take advantage of this constraint in making its plans - it has to assume that some rows may still have a null.

Hammad: I face the problem and solve like following:

Alter table thr_empl_info modify THR_EM_DESIGNATION_ID not null

ALTER TABLE table_name SET column_name = '0' WHERE column_name IS NULL;

ALTER TABLE table_name MODIFY COLUMN(column_name NUMBER CONSTRAINT constraint_identifier NOT NULL);

This is of course assuming that your column is a number but it's the same thing really, you would just change the '0' to a default value that isn't null.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!