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?的更多相关文章
- 【问题】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- ...
- MySQL入门手册
本文内容摘自MySQL5.6官方文档,主要选取了在实践过程中所用到的部分文字解释,力求只摘录重点,快速学会使用MySQL,本文所贴代码地方就是我亲自练习过的代码,凡本文没有练习过的代码都没有贴在此处, ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
- libevent源码分析(一)
分析libevent的源代码,我的想法的是先分析各种结构体,struct event_base.struct event,然后是event_base_new函数.event_new函数.event_a ...
- mysql命令行工具
mysql包相关命令行工具 [root@manage ~]# rpm -qa|grep mysql mysql-server-5.1.73-5.el6_7.1.x86_64 mysql-5.1.73- ...
- 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 ...
- platanus
nohup platanus assemble -o Larrrea -f ../unknown_NoIndex_L000_R1.fastq ../unknown_NoIndex_L000_R2.f ...
- windows 下使用 zip安装包安装MySQL 5.7
以下内容参考官方文档:http://dev.mysql.com/doc/refman/5.7/en/windows-start-command-line.html 解压缩zip到D:\mysql-5. ...
- iOS处理XMl提供GDataXMLNode下载的链接
GDataXMLNode .好东西,处理xml 在iOS 中使用.可以编辑和读取Xml文档.支持Xpath.这个很好. GDataXMLNode.h GDataXMLNode.m 文件很不好找啊. / ...
随机推荐
- 【转】ubuntu64,ndk-r9 编译 ffmpeg 2.1.1的config文件
#!/bin/bash NDK_ROOT=/home/wjh/fox/android-ndk-r9c/ PREBUILT=${NDK_ROOT}toolchains/arm-linux-android ...
- JNI 回调小记
javah在eclipse中设置参数:location(javah.exe的位置)working dir(${project_loc}/src) -classpath .;./classes -d $ ...
- scala抽象类抽象字段
package com.test.scala.test /** * 抽象类学习,定义abstact关键字 */ abstract class AbstractClass { val id:Int;// ...
- 使用MeanJS Yeoman Generator
1.首先全局安装该生成器 sudo npm install -g generator-meanjs 2.为项目创建一个路径 mkdir xmen && cd xmen 3.创建app ...
- iOS中 视频直播功能-流媒体的使用(详解)韩俊强的CSDN博客
上一篇博客:(流媒体实现视频播放和下载功能):http://blog.csdn.net/qq_31810357/article/details/50574914 最近视频直播功能比较火,处于需求,研究 ...
- PHP将XML数据转换为数组
<?php $s=join(,file('httpapi.elong.comxmlv2.0hotelcn0132701501.xml')); $result = xml_to_array($s) ...
- 十步完全理解 SQL(转载)
英文出处:Lukas Eder. 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序语言.甚至是函数语言(尽管有些人认为 ...
- GHOST出错
error 15:file not found grub问题VFS:Cannot open root device "sda" or unknow-block 可能是磁盘驱动程序问 ...
- [团队项目]后续安排 Github
6.后续安排 第16周 周二晚7点之前将本代码上传到GITHUB. 周三上课时运行你们的系统给我观赏一下. 根据博客,运行演示,github代码情况评定第二个冲刺的分数. 至此,软件工程学期平时分截止 ...
- Balance
Balance Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11947 Accepted: 7464 Description ...