婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > MySQL為何不建議使用默認值為null列

MySQL為何不建議使用默認值為null列

熱門標簽:云南電商智能外呼系統(tǒng)價格 大眾點評星級酒店地圖標注 話務(wù)外呼系統(tǒng)怎么樣 400電話可以辦理嗎 拉卡拉外呼系統(tǒng) 臨清電話機器人 智能外呼系統(tǒng)復(fù)位 外東北地圖標注 高清地圖標注道路

通常能聽到的答案是使用了NULL值的列將會使索引失效,但是如果實際測試過一下,你就知道IS NULL會使用索引.所以上述說法有漏洞.

著急的人拉到最下邊看結(jié)論

Preface

Null is a special constraint of columns.
The columns in table will be added null constrain if you do not define the column with “not null” key words explicitly
when creating the table.Many programmers like to define columns by default
because of the conveniences(reducing the judgement code of nullibility) what consequently
cause some uncertainty of query and poor performance of database.

NULL值是一種對列的特殊約束,我們創(chuàng)建一個新列時,如果沒有明確的使用關(guān)鍵字not null聲明該數(shù)據(jù)列,Mysql會默認的為我們添加上NULL約束.
有些開發(fā)人員在創(chuàng)建數(shù)據(jù)表時,由于懶惰直接使用Mysql的默認推薦設(shè)置.(即允許字段使用NULL值).而這一陋習(xí)很容易在使用NULL的場景中得出不確定的查詢結(jié)果以及引起數(shù)據(jù)庫性能的下降.

Introduce

Null is null means it is not anything at all,we cannot think of null is equal to ‘' and they are totally different.
MySQL provides three operators to handle null value:“IS NULL”,“IS NOT NULL”,"=>" and a function ifnull().
IS NULL: It returns true,if the column value is null.
IS NOT NULL: It returns true,if the columns value is not null.
=>: It's a compare operator similar with “=” but not the same.It returns true even for the two null values.
(eg. null => null is legal)
IFNULL(): Specify two input parameters,if the first is null value then returns the second one.
It's similar with Oracle's NVL() function.

NULL并不意味著什么都沒有,我們要注意 NULL 跟 ''(空值)是兩個完全不一樣的值.MySQL中可以操作NULL值操作符主要有三個.

  • IS NULL
  • IS NOT NULL
  • => 太空船操作符,這個操作符很像=,select NULL=>NULL可以返回true,但是select NULL=NULL返回false.
  • IFNULL 一個函數(shù).怎么使用自己查吧…反正我會了

Example

Null never returns true when comparing with any other values except null with “=>”.
NULL通過任一操作符與其它值比較都會得到NULL,除了=>.

(root@localhost mysql3306.sock)[zlm]>create table test_null(
    -> id int not null,
    -> name varchar(10)
    -> );
Query OK, 0 rows affected (0.02 sec)

(root@localhost mysql3306.sock)[zlm]>insert into test_null values(1,'zlm');
Query OK, 1 row affected (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>insert into test_null values(2,null);
Query OK, 1 row affected (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select * from test_null;
+----+------+
| id | name |
+----+------+
|  1 | zlm  |
|  2 | NULL |
+----+------+
2 rows in set (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select * from test_null where name=null;
Empty set (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select * from test_null where name is null;
+----+------+
| id | name |
+----+------+
|  2 | NULL |
+----+------+
1 row in set (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select * from test_null where name is not null;
+----+------+
| id | name |
+----+------+
|  1 | zlm  |
+----+------+
1 row in set (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select * from test_null where null=null;
Empty set (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select * from test_null where null>null;
Empty set (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select * from test_null where null=>null;
+----+------+
| id | name |
+----+------+
|  1 | zlm  |
|  2 | NULL |
+----+------+
2 rows in set (0.00 sec)

//null=>null always return true,it's equal to "where 1=1".

Null means “a missing and unknown value”.Let's see details below.
NULL代表一個不確定的值,就算是兩個NULL,它倆也不一定相等.(像不像C中未初始化的局部變量)

(root@localhost mysql3306.sock)[zlm]>SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;
+-----------+---------------+------------+----------------+
| 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |
+-----------+---------------+------------+----------------+
|         0 |             1 |          0 |              1 |
+-----------+---------------+------------+----------------+
1 row in set (0.00 sec)

//It's not equal to zero number or vacant string.
//In MySQL,0 means fasle,1 means true.

(root@localhost mysql3306.sock)[zlm]>SELECT 1 = NULL, 1 > NULL, 1  NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 > NULL | 1  NULL | 1 > NULL |
+----------+-----------+----------+----------+
|     NULL |      NULL |     NULL |     NULL |
+----------+-----------+----------+----------+
1 row in set (0.00 sec)

//It cannot be compared with number.
//In MySQL,null means false,too.

It truns null as a result if any expression contains null value.
任何有返回值的表達式中有NULL參與時,都會得到另外一個NULL值.

(root@localhost mysql3306.sock)[zlm]>select ifnull(null,'First is null'),ifnull(null+10,'First is null'),ifnull(concat('abc',null),'First is null');
+------------------------------+---------------------------------+--------------------------------------------+
| ifnull(null,'First is null') | ifnull(null+10,'First is null') | ifnull(concat('abc',null),'First is null') |
+------------------------------+---------------------------------+--------------------------------------------+
| First is null                | First is null                   | First is null                              |
+------------------------------+---------------------------------+--------------------------------------------+
1 row in set (0.00 sec)

//null value needs to be disposed with ifnull() function,what usually causes sql statement more complex.
//As we all know,MySQL does not support funcion index.Therefore,indexes on the column may not be used.That's really worse.

It's diffrent when using count(*) count(null column).
使用count(*) 或者 count(null column)結(jié)果不同,count(null column)=count(*).

(root@localhost mysql3306.sock)[zlm]>select count(*),count(name) from test_null;
+----------+-------------+
| count(*) | count(name) |
+----------+-------------+
|        2 |           1 |
+----------+-------------+
1 row in set (0.00 sec)

//count(*) returns all rows ignore the null while count(name) returns the non-null rows in column "name".
//This will also leads to uncertainty if someone is unaware of the details above.

如果使用者對NULL屬性不熟悉,很容易統(tǒng)計出錯誤的結(jié)果.

When using distinct,group by,order by,all null values are considered as the same value.
雖然select NULL=NULL的結(jié)果為false,但是在我們使用distinct,group by,order by時,NULL又被認為是相同值.

(root@localhost mysql3306.sock)[zlm]>insert into test_null values(3,null);
Query OK, 1 row affected (0.00 sec)

(root@localhost mysql3306.sock)[zlm]>select distinct name from test_null;
+------+
| name |
+------+
| zlm  |
| NULL |
+------+
2 rows in set (0.00 sec)

//Two rows of null value returned one and the result became two.

(root@localhost mysql3306.sock)[zlm]>select name from test_null group by name;
+------+
| name |
+------+
| NULL |
| zlm  |
+------+
2 rows in set (0.00 sec)

//Two rows of null value were put into the same group.
//By default,group by will also sort the result(null row showed first).

(root@localhost mysql3306.sock)[zlm]>select id,name from test_null order by name;
+----+------+
| id | name |
+----+------+
|  2 | NULL |
|  3 | NULL |
|  1 | zlm  |
+----+------+
3 rows in set (0.00 sec)

//Three rows were sorted(two null rows showed first).

MySQL supports to use index on column which contains null value(what's different from oracle).
MySQL中支持在含有NULL值的列上使用索引,但是Oracle不支持.這就是我們平時所說的如果列上含有NULL那么將會使索引失效.
嚴格來說,這句話對與MySQL來說是不準確的.

(root@localhost mysql3306.sock)[sysbench]>show tables;
+--------------------+
| Tables_in_sysbench |
+--------------------+
| sbtest1            |
| sbtest10           |
| sbtest2            |
| sbtest3            |
| sbtest4            |
| sbtest5            |
| sbtest6            |
| sbtest7            |
| sbtest8            |
| sbtest9            |
+--------------------+
10 rows in set (0.00 sec)

(root@localhost mysql3306.sock)[sysbench]>show create table sbtest1\G
*************************** 1. row ***************************
       Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `k` int(11) NOT NULL DEFAULT '0',
  `c` char(120) NOT NULL DEFAULT '',
  `pad` char(60) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

(root@localhost mysql3306.sock)[sysbench]>alter table sbtest1 modify k int null,modify c char(120) null,modify pad char(60) null;
Query OK, 0 rows affected (4.14 sec)
Records: 0  Duplicates: 0  Warnings: 0

(root@localhost mysql3306.sock)[sysbench]>insert into sbtest1 values(100001,null,null,null);
Query OK, 1 row affected (0.00 sec)

(root@localhost mysql3306.sock)[sysbench]>explain select id,k from sbtest1 where id=100001;
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | sbtest1 | NULL       | const | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 | NULL  |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

(root@localhost mysql3306.sock)[sysbench]>explain select id,k from sbtest1 where k is null;
+----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref   | rows | filtered | Extra                    |
+----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
|  1 | SIMPLE      | sbtest1 | NULL       | ref  | k_1           | k_1  | 5       | const |    1 |   100.00 | Using where; Using index |
+----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

//In the first query,the newly added row is retrieved by primary key.
//In the second query,the newly added row is retrieved by secondary key "k_1"
//It has been proved that indexes can be used on the columns which contain null value.
//column "k" is int datatype which occupies 4 bytes,but the value of "key_len" turn out to be 5.what's happed?Because null value needs 1 byte to store the null flag in the rows.

這個是我自己測試的例子.

mysql> select * from test_1;
+-----------+------+------+
| name      | code | id   |
+-----------+------+------+
| gaoyi     | wo   |    1 |
| gaoyi     | w    |    2 |
| chuzhong  | wo   |    3 |
| chuzhong  | w    |    4 |
| xiaoxue   | dd   |    5 |
| xiaoxue   | dfdf |    6 |
| sujianhui | su   |   99 |
| sujianhui | NULL |   99 |
+-----------+------+------+
8 rows in set (0.00 sec)

mysql> explain select * from test_1 where code is NULL;
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
| id | select_type | table  | partitions | type | possible_keys | key        | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | test_1 | NULL       | ref  | index_code    | index_code | 161     | const |    1 |   100.00 | Using index condition |
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from test_1 where code is not NULL;
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| id | select_type | table  | partitions | type  | possible_keys | key        | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | test_1 | NULL       | range | index_code    | index_code | 161     | NULL |    7 |   100.00 | Using index condition |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from test_1 where code='dd';
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
| id | select_type | table  | partitions | type | possible_keys | key        | key_len | ref   | rows | filtered | Extra                 |
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
|  1 | SIMPLE      | test_1 | NULL       | ref  | index_code    | index_code | 161     | const |    1 |   100.00 | Using index condition |
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from test_1 where code like "dd%";
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| id | select_type | table  | partitions | type  | possible_keys | key        | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | test_1 | NULL       | range | index_code    | index_code | 161     | NULL |    1 |   100.00 | Using index condition |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

Summary 總結(jié)

null value always leads to many uncertainties when disposing sql statement.It may cause bad performance accidentally.

列中使用NULL值容易引發(fā)不受控制的事情發(fā)生,有時候還會嚴重托慢系統(tǒng)的性能.

例如:

null value will not be estimated in aggregate function() which may cause inaccurate results.
對含有NULL值的列進行統(tǒng)計計算,eg. count(),max(),min(),結(jié)果并不符合我們的期望值.

null value will influence the behavior of the operations such as “distinct”,“group by”,“order by” which causes wrong sort.
干擾排序,分組,去重結(jié)果.

null value needs ifnull() function to do judgement which makes the program code more complex.
有的時候為了消除NULL帶來的技術(shù)債務(wù),我們需要在SQL中使用IFNULL()來確保結(jié)果可控,但是這使程序變得復(fù)雜.
null value needs a extra 1 byte to store the null information in the rows.

NULL值并是占用原有的字段空間存儲,而是額外申請一個字節(jié)去標注,這個字段添加了NULL約束.(就像額外的標志位一樣)
As these above drawbacks,it's not recommended to define columns with default null.
We recommand to define “not null” on all columns and use zero number vacant string to substitute relevant data type of null.

根據(jù)以上缺點,我們并不推薦在列中設(shè)置NULL作為列的默認值,你可以使用NOT NULL消除默認設(shè)置,使用0或者''空字符串來代替NULL.

參考資料

 https://www.cnblogs.com/aaron8219/p/9259379.html

到此這篇關(guān)于MySQL為何不建議使用默認值為null列的文章就介紹到這了,更多相關(guān)MySQL默認值為null內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 方便快捷實現(xiàn)springboot 后端配置多個數(shù)據(jù)源、Mysql數(shù)據(jù)庫
  • MySQL DEFINER具體使用詳解
  • MySQL隔離級別和鎖機制的深入講解
  • django生產(chǎn)環(huán)境搭建(uWSGI+django+nginx+python+MySQL)
  • 淺析MySQL 主鍵使用數(shù)字還是uuid查詢快
  • MySQL之權(quán)限以及設(shè)計數(shù)據(jù)庫案例講解
  • Node-Red實現(xiàn)MySQL數(shù)據(jù)庫連接的方法
  • mysql之group by和having用法詳解

標簽:溫州 無錫 福州 阿里 定西 山西 揚州 三明

巨人網(wǎng)絡(luò)通訊聲明:本文標題《MySQL為何不建議使用默認值為null列》,本文關(guān)鍵詞  MySQL,為,何不,建議,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL為何不建議使用默認值為null列》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL為何不建議使用默認值為null列的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    一区二区三区在线高清| 欧美日韩高清一区二区三区| 亚洲四区在线观看| 中文字幕免费一区| 亚洲国产欧美一区二区三区丁香婷| 一区二区三区中文字幕电影| 日本成人在线电影网| 经典三级视频一区| 亚洲成av人片在线观看| 国产一区二区看久久| 欧美视频完全免费看| 久久中文字幕电影| 久久精品久久综合| 欧美日韩国产高清一区二区| 中文字幕高清不卡| 麻豆久久久久久久| 在线精品视频一区二区三四| 精品国产乱码久久久久久久| 亚洲综合图片区| 久久不见久久见免费视频7| 99精品久久免费看蜜臀剧情介绍 | 日韩中文字幕一区二区三区| 成人av午夜电影| 欧美精品一区二区精品网| 亚洲国产精品自拍| 国产欧美视频一区二区三区| 亚洲欧美中日韩| 欧美日韩国产免费| 亚洲裸体xxx| 日韩成人dvd| 三级久久三级久久| 久久av中文字幕片| 色婷婷久久综合| 亚洲成人av资源| 一区二区三区欧美日| 奇米在线7777在线精品| 国产精品国产成人国产三级| 7777精品伊人久久久大香线蕉的 | 91蜜桃网址入口| 亚洲精品美腿丝袜| 91蜜桃免费观看视频| 天天综合网天天综合色| 久久精品亚洲国产奇米99| 国产麻豆午夜三级精品| 五月综合激情网| 男男gaygay亚洲| 99国产精品国产精品久久| voyeur盗摄精品| 丝袜美腿成人在线| 91精品国产免费| 白白色 亚洲乱淫| 午夜精品久久久久影视| 日本中文字幕不卡| 久久婷婷一区二区三区| 国产成人免费9x9x人网站视频| 亚洲成人综合网站| 色婷婷av一区二区三区软件| 精品少妇一区二区三区视频免付费 | 久久精品视频一区二区| 欧美一区二区三区四区久久| 一区二区欧美视频| 偷拍与自拍一区| 日韩一级成人av| 91免费观看国产| 久久久久久久综合狠狠综合| 成人性生交大片免费看中文网站| 青娱乐精品在线视频| 欧美精品18+| 午夜天堂影视香蕉久久| 中文无字幕一区二区三区| 欧美人牲a欧美精品| 色偷偷一区二区三区| 日韩av不卡在线观看| 久久―日本道色综合久久| 精品国产91洋老外米糕| 91成人网在线| 国产一区二区三区四区五区美女 | 精品欧美一区二区在线观看| a美女胸又www黄视频久久| 精品国产91亚洲一区二区三区婷婷| 中文字幕乱码一区二区免费| 色爱区综合激月婷婷| aaa欧美大片| 欧美区视频在线观看| 精品日韩一区二区三区| 欧美人妖巨大在线| 国产激情91久久精品导航| 欧美aaaaaa午夜精品| 久久91精品久久久久久秒播| 亚洲图片有声小说| 亚洲婷婷国产精品电影人久久| 国产精品18久久久久久久久久久久| 国产精品夜夜爽| 免费不卡在线观看| 免费在线观看不卡| 男男视频亚洲欧美| 国产精品嫩草久久久久| 色综合网色综合| 另类小说视频一区二区| 国产传媒一区在线| 国产盗摄女厕一区二区三区| 丰满白嫩尤物一区二区| 一二三区精品视频| 26uuu亚洲综合色| 欧美精彩视频一区二区三区| 国产欧美日韩另类一区| 精品国产3级a| 综合欧美亚洲日本| 亚洲人成网站在线| 日韩电影免费在线观看网站| 视频一区二区三区在线| 激情综合五月婷婷| 欧美精品丝袜中出| 亚洲.国产.中文慕字在线| 国产精品一区二区在线播放 | 777亚洲妇女| 国产亚洲精久久久久久| 欧美第一区第二区| 久久国产精品色| 日本精品裸体写真集在线观看| 亚洲一区二区欧美日韩| 欧美亚洲日本国产| 午夜私人影院久久久久| 7777精品伊人久久久大香线蕉| 亚洲精选视频在线| 日韩电影一区二区三区四区| 欧美精品视频www在线观看| 欧美国产欧美亚州国产日韩mv天天看完整 | 欧美精品一区二区三区很污很色的 | 国产精品不卡视频| 日韩成人精品在线观看| 国产精品一二三| 国产欧美日韩一区二区三区在线观看| 精品在线观看免费| 日韩一级二级三级| 一本大道av伊人久久综合| 亚洲激情自拍偷拍| 国产日韩欧美一区二区三区乱码| 国产乱子轮精品视频| 一区二区三区自拍| 欧美人妖巨大在线| 91麻豆精品视频| 欧美精品在线一区二区三区| 亚洲女同女同女同女同女同69| 理论电影国产精品| 国产区在线观看成人精品 | 成人av网在线| 欧美一区二区在线免费播放| 亚洲黄色在线视频| 欧美国产一区视频在线观看| 欧美日韩视频第一区| 久久国产精品72免费观看| 日韩一区欧美小说| 一区二区三区色| 亚洲国产精品天堂| 亚洲精品乱码久久久久久| 丝袜美腿高跟呻吟高潮一区| 日韩中文字幕不卡| 国产欧美日韩视频在线观看| 亚洲欧美在线aaa| 午夜精品久久久久久久久久久| 天堂av在线一区| 日韩极品在线观看| av不卡一区二区三区| 欧美韩国日本一区| 欧美日韩一区在线| 国产精品亚洲午夜一区二区三区 | 91精品蜜臀在线一区尤物| 久久99久久99| 中文字幕一区不卡| 欧美日韩的一区二区| 国产美女精品一区二区三区| 日韩一区在线播放| 欧美大片日本大片免费观看| aa级大片欧美| 久久99久久99精品免视看婷婷 | 日韩精品一区二区三区视频播放| 国产乱国产乱300精品| 中文字幕亚洲成人| 日韩欧美第一区| 91污片在线观看| 色综合久久中文综合久久97| 欧美日韩www| 美女看a上一区| 国产精品欧美经典| 欧美日韩aaaaa| 中文无字幕一区二区三区| 色88888久久久久久影院按摩| 日本在线不卡视频一二三区| 国产精品不卡一区二区三区| 欧美xxxx老人做受| 欧美人与禽zozo性伦| 不卡视频免费播放| 国产综合久久久久影院| 亚洲精品国产成人久久av盗摄| 2020国产成人综合网| 欧美喷潮久久久xxxxx| 色诱亚洲精品久久久久久| 国产99精品视频| 欧美日韩一级片网站|