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

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

 

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.

 

转载地址:http://rgfuo.baihongyu.com/

你可能感兴趣的文章
Flex布局
查看>>
Material Design之 AppbarLayout 开发实践总结
查看>>
Flutter之MaterialApp使用详解
查看>>
DataBinding最全使用说明
查看>>
原生Js交互之DSBridge
查看>>
Matlab编程之——卷积神经网络CNN代码解析
查看>>
三篇文章了解 TiDB 技术内幕 —— 说计算
查看>>
copy strong weak assign的区别
查看>>
OpenCV 入门
查看>>
css 3D transform变换
查看>>
ele表格合并行之后的selection选中
查看>>
正则表达式分解剖析(一文悟透正则表达式)
查看>>
解决UILable标点符号居中的问题
查看>>
HTML5新特性教程
查看>>
ImageOptim-无损图片压缩Mac版
查看>>
12 Go语言map底层浅析
查看>>
vue-resumer 项目中 element-ui 遇到的 textarea autosize 问题
查看>>
PHP扩展库PEAR被攻击,近半年下载者或被影响
查看>>
传统运维团队转型应该注意哪些问题?
查看>>
JavaScript函数(二)
查看>>