Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?

https://blog.jooq.org/2014/11/11/have-you-ever-wondered-about-the-difference-between-not-null-and-default/

 

When writing DDL in SQL, you can specify a couple of constraints on columns, like NOT NULL or DEFAULT constraints. 

Some people might wonder, if the two constraints are actually redundant, i.e. is it still necessary to specify a NOT NULL constraint, if there is already a DEFAULT clause?

The answer is: Yes!

 

Yes, you should still specify that NOT NULL constraint. And no, the two constraints are not redundant. 

The answer I gave here on Stack Overflow wraps it up by example, which I’m going to repeat here on our blog:

 

DEFAULT is the value that will be inserted in the absence缺乏 of an explicit明确的 value in an insert / update statement. Lets assume, your DDL did not have the NOT NULL constraint:

1
2
3
ALTER TABLE tbl
  ADD COLUMN col VARCHAR(20)
    DEFAULT "MyDefault"

Then you could issue these statements

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- 1. This will insert "MyDefault"
--    into tbl.col    第一个插入记录,没有指定col的值
INSERT INTO tbl (A, B)
  VALUES (NULL, NULL);
 
-- 2. This will insert "MyDefault"
--    into tbl.col
INSERT INTO tbl (A, B, col)     第二个插入记录,指定col为Default
  VALUES (NULL, NULL, DEFAULT);
 
-- 3. This will insert "MyDefault"
--    into tbl.col
INSERT INTO tbl (A, B, col)
  DEFAULT VALUES;
 
-- 4. This will insert NULL
--    into tbl.col             第四个插入记录,指定col为null,不使用默认值
INSERT INTO tbl (A, B, col)
  VALUES (NULL, NULL, NULL);

Alternatively, you can also use DEFAULT in UPDATE statements, according to the SQL-1992 standard:

1
2
3
4
5
6
7
-- 5. This will update "MyDefault"
--    into tbl.col
UPDATE tbl SET col = DEFAULT;
 
-- 6. This will update NULL
--    into tbl.col
UPDATE tbl SET col = NULL;

Note, not all databases support all of these SQL standard syntaxes.

Adding the NOT NULL constraint will cause an error with statements 4, 6, while 1-3, 5 are still valid statements.

So to answer your question:

No, NOT NULL and DEFAULT are not redundant

 

That’s already quite interesting, so the DEFAULT constraint really only interacts with DML statements and how they specify the various columns that they’re updating. The NOT NULL constraint is a much more universal guarantee, that constraints a column’s content also “outside” of the manipulating DML statements.

For instance, if you have a set of data and then you add a DEFAULT constraint, this will not affect your existing data, only new data being inserted.

If, however, you have a set of data and then you add a NOT NULL constraint, you can actually only do so if the constraint is valid – i.e. when there are no NULL values in your column. Otherwise, an error will be raised.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(359)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-09-27 积分
2015-09-27 Playing with cubes II
2015-09-27 Smallest unused ID
2015-09-27 SHA-1
2015-09-27 git clone
2015-09-27 git rev-list 和 git rev-parse
2015-09-27 git获取远端版本库上的Tag (没有clone[远端的版本库太大了])
点击右上角即可分享
微信分享提示