Mysql的NULL和Empty String

來源:互聯網
上載者:User

標籤:nal   select   sql   查詢   type   字元   sans   區別   tab   

最近新接觸Mysql,昨天建立一個表用於儲存表結構資訊:

create table tablist(TABLE_SCHEMA varchar(40),TABLE_NAME varchar(40),COLUMN_NAME varchar(40),COLUMN_TYPE varchar(40),IS_NULLABLE varchar(10),COLUMN_DEFAULT varchar(40),COLUMN_COMMENT varchar(1000),REMARK varchar(2000));insert into tablist(TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,COLUMN_COMMENT) select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE,IS_NULLABLE,COLUMN_DEFAULT,COLUMN_COMMENT from information_schema.`COLUMNS` where TABLE_SCHEMA=‘leo‘;

然後查詢tablist表:

看看有哪些列沒有comment於是:

select * from tablist where COLUMN_COMMENT is null;

查到的結果居然是Empty set。不過從以上查詢結果和navicat都能看出:null值在結果集中顯示的是‘null‘的單詞,而Null 字元串則顯示為空白。

查過資料後發現Mysql的null值和Null 字元串是有區別的,這裡很奇怪COLUMN_COMMENT在經過insert之後,null值居然變成了Null 字元串(原因未明)。

使用select * from tablist where COLUMN_COMMENT=‘‘;查詢正常。

NULL columns require additional space in the row to record whether their values are NULL.For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.在Mysql的myisam引擎中,null值佔用額外的儲存空間(1bit),Null 字元串則完全不佔用空間。同時null值在B樹索引中也無法被儲存,資料量大時會造成較嚴重的效能問題。兩者的查詢方式也不一樣:null值查詢使用is null/is not null查詢,而empty string使用=或者!=查詢即可。

Mysql的NULL和Empty String

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.