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:

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

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

Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?的更多相关文章

  1. 【问题】Difference between ">/dev/null 2>&1" and "2>&1 >/dev/null"

    https://www.unix.com/shell-programming-and-scripting/125947-difference-between-dev-null-2-1-2-1-dev- ...

  2. MySQL入门手册

    本文内容摘自MySQL5.6官方文档,主要选取了在实践过程中所用到的部分文字解释,力求只摘录重点,快速学会使用MySQL,本文所贴代码地方就是我亲自练习过的代码,凡本文没有练习过的代码都没有贴在此处, ...

  3. 编写更少量的代码:使用apache commons工具类库

    Commons-configuration   Commons-FileUpload   Commons DbUtils   Commons BeanUtils  Commons CLI  Commo ...

  4. libevent源码分析(一)

    分析libevent的源代码,我的想法的是先分析各种结构体,struct event_base.struct event,然后是event_base_new函数.event_new函数.event_a ...

  5. mysql命令行工具

    mysql包相关命令行工具 [root@manage ~]# rpm -qa|grep mysql mysql-server-5.1.73-5.el6_7.1.x86_64 mysql-5.1.73- ...

  6. but this usually doesn’t gain you anything.

    High Performance My SQL, Third Edition Date and Time Types My SQL has many types for various kinds o ...

  7. platanus

    nohup  platanus assemble -o Larrrea -f ../unknown_NoIndex_L000_R1.fastq ../unknown_NoIndex_L000_R2.f ...

  8. windows 下使用 zip安装包安装MySQL 5.7

    以下内容参考官方文档:http://dev.mysql.com/doc/refman/5.7/en/windows-start-command-line.html 解压缩zip到D:\mysql-5. ...

  9. iOS处理XMl提供GDataXMLNode下载的链接

    GDataXMLNode .好东西,处理xml 在iOS 中使用.可以编辑和读取Xml文档.支持Xpath.这个很好. GDataXMLNode.h GDataXMLNode.m 文件很不好找啊. / ...

随机推荐

  1. 【linux】xrander/cvt自定义分辨率

    今天在虚拟机上装了一个LUbuntux64(12.10)玩,安装的时候,由于主板默认是没有开虚拟化支持,报错,改后相当的顺利.但是进入系统后,屏幕显示分辨率为800X600的,全屏的话,在大显示器上显 ...

  2. php 下载保存文件保存到本地的两种方法

    第一种: 1 <? ?> 或 <?php //下载文件保存到本地//www.jbxue.comfunction downfile($fileurl){ob_start(); $fil ...

  3. Sqlserver常用的时间函数---GETDATE、GETUTCDATE、DATENAME

    GETDATE 按 datetime 值的 Microsoft® SQL Server™ 标准内部格式返回当前系统日期和时间.语法GETDATE ( )返回类型datetime注释日期函数可用在 SE ...

  4. React+Node.js+Express+mongoskin+MongoDB

    首发:个人博客,更新&纠错&回复 采用React + Node.js + Express + mongoskin + MongoDB技术开发的一个示例,演示地址在这里,项目源码在这里. ...

  5. 鸟哥的linux私房菜学习记录之bash

    当你对计算机输入一个指令时,bash会将指令传送给核心kernel,核心再去调用相关的程序,启动硬件. 如果直接让用户操作操作系统,可能会造成系统的崩溃,所以操作系统通过应用程序来让用户操作系统即壳程 ...

  6. source insight资源

    http://www.cnblogs.com/Red_angelX/p/3713935.html https://github.com/redxu/sihook

  7. 【python cookbook】【数据结构与算法】4.找到最大或最小的N个元素

    问题:想在某个集合中找出最大或最小的N个元素 解决方案:heapq模块中的nlargest()和nsmallest()两个函数正是我们需要的. >>> import heapq &g ...

  8. ES6,ES2105核心功能一览,js新特性详解

    ES6,ES2105核心功能一览,js新特性详解 过去几年 JavaScript 发生了很大的变化.ES6(ECMAScript 6.ES2105)是 JavaScript 语言的新标准,2015 年 ...

  9. Java的多线程+Socket 后台

    打包好可执行的jar文件之后, 用ftp上传到服务器, 用nohup命令, 让这个服务器在后台运行, 并将输出重定向到log文件中, 命令是: #nohup java -jar MTSServer.j ...

  10. 使用SQLServer Profiler侦测死锁(转)

    准备工作: 为了侦测死锁,我们需要先模拟死锁.本例将使用两个不同的会话创建两个事务. 步骤: 1. 打开SQLServer Profiler 2. 选择[新建跟踪],连到实例. 3. 然后选择[空白] ...