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

后端 未结 3 1075
孤街浪徒
孤街浪徒 2021-01-03 20:34

I have a table with a column that contains a few null values. I want to add a NOT NULL constraint on that column without updating the existing nulls to a non-nu

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 21:29

    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.

提交回复
热议问题