博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?
阅读量:5023 次
发布时间:2019-06-12

本文共 2420 字,大约阅读时间需要 8 分钟。

 

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

, 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. 

 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:

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

Then you could issue these statements

-- 1. This will insert "MyDefault" --    into tbl.col    第一个插入记录,没有指定col的值INSERT INTO tbl (A, B)   VALUES (NULL, NULL);-- 2. This will insert "MyDefault" --    into tbl.colINSERT INTO tbl (A, B, col)     第二个插入记录,指定col为Default  VALUES (NULL, NULL, DEFAULT);-- 3. This will insert "MyDefault"--    into tbl.colINSERT 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:

-- 5. This will update "MyDefault"--    into tbl.colUPDATE tbl SET col = DEFAULT;-- 6. This will update NULL --    into tbl.colUPDATE 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.

 

转载于:https://www.cnblogs.com/chucklu/p/5913051.html

你可能感兴趣的文章
IOS 开发调用打电话,发短信
查看>>
CI 框架中的日志处理 以及 404异常处理
查看>>
keepalived介绍
查看>>
css3 标签 background-size
查看>>
python itertools
查看>>
Linux内核调试技术——jprobe使用与实现
查看>>
样式、格式布局
查看>>
ubuntu设计文件权限
查看>>
Vue双向绑定原理详解
查看>>
Android基础总结(5)——数据存储,持久化技术
查看>>
关于DataSet事务处理以及SqlDataAdapter四种用法
查看>>
bootstrap
查看>>
http://lorempixel.com/ 可以快速产生假图
查看>>
工程经验总结之吹水"管理大境界"
查看>>
为什么JS动态生成的input标签在后台有时候没法获取到
查看>>
20189210 移动开发平台第六周作业
查看>>
java之hibernate之基于外键的双向一对一关联映射
查看>>
rxjs一句话描述一个操作符(1)
查看>>
第一次独立上手多线程高并发的项目的心路历程
查看>>
ServiceStack 介绍
查看>>