SlideShare uma empresa Scribd logo
1 de 74
NULLのはな
し
2013.7.6 ユルカワPHP#2
VOLARE, INC CTO 田中創基
アンケート
突然ですが
アンケートです
アンケート
該当する人は挙手
アンケート1
アンケート1
アンケート1
SQLで
NULLを使った事
のある人
アンケート2
アンケート2
アンケート2
WHERE a = NULL
と書いて動かず
悩んだ事のある人
アンケート3
アンケート3
アンケート3
SQLのNULLが
他の言語とどう違
うか説明できる人
アンケート4
アンケート4
アンケート4
SQL99の仕様の
UNKNOWNを
知っている人
NULLって
何?
NULLって何?
NULLって何?
基本的には
値が無いって意味
NULLって何?
ぶっちゃけ
NULLって何?
NULLについて
深くは知らない
NULLって何?
だから勉強しよう
NULLって何?
これが今回の
スタート地点です
NULLは
値じゃない
NULLは値じゃない
NULLは値じゃない
値じゃないなら
何だって言うんだ
NULLは値じゃない
エドガー・F・
コッド
曰く
RDBMSの発案者
NULLは値じゃない
「欠損値と不適切
な値」の表現であ
る
NULLは値じゃない
どういうこと?
NULLは値じゃない
NULLとは「ここ
には値がない」と
いう文
NULLは値じゃない
文章では幅を取る
ので「NULL」と
いう簡潔なマーク
で代用している
NULLは値じゃない
NULLは
値ではない
大切なことなので
二度言います
NULLは値じゃない
ちなみにNULL値
と
呼ぶのは間違い
NULLは値じゃない
イメージ
NULLは値じゃない
id 顧客名 取引額 年齢 性別
1 福沢諭吉 10000 56 男
2 慶応義塾大学 10000 null null
3 樋口一葉 5000 20 女
4 野口英世 1000 41 男
5 野口英世記念館 1000 null null
NULLは値じゃない
id 顧客名 取引額 年齢 性別
1 福沢諭吉 10000 56 男
2 慶応義塾大学 10000
3 樋口一葉 5000 20 女
4 野口英世 1000 41 男
5 野口英世記念館 1000
枠自体がない
NULLは値じゃない
外部結合をしたと
きも構造は一緒
NULLは値じゃない
値がないという
「状態」
NULLは値じゃない
「状態」と「値」
は
演算できない
NULLは値じゃない
だから
a = NULL
は使えない
NULLは値じゃない
「状態」は
「値」ではないか
ら
NULLは値じゃない
INDEXの対象にも
ならない
UNKNOWN
UNKNOWN
UNKNOWN
Boolean型の値
UNKNOWNが追加
されました
UNKNOWN
NULL: 値ではな
い
UNKNOWN: 値
UNKNOWN
NULLで演算を
すると発生します
UNKNOWN
MySQLも
Postgresも
Oracleも未対応
UNKNOWN
NULLを
代用しています
UNKNOWN
ANDとORは
NULLの演算が
可能
例外として
UNKNOWN
演算可能な
NULLが発生
UNKNOWN
これが混乱のもと
UNKNOWN
内部的に
NULL⇄UNKNOW
N
自動型変換
UNKNOWN
自動型変換には
悩みがつきもの
PHPユーザも苦しむ
UNKNOWN
NULLの中身が
どっちか
考えて使うべし
UNKNOWN
そうしないと
思わぬ事故に
UNKNOWN
実際にあった例
UNKNOWN
startとendで期間
start < now() AND
now() < end
としたところ
UNKNOWN
end = NULLの
終了日未定が
出てこなかった
UNKNOWN
対策
NOT NULL
3値理論
3値理論
UNKNOWN
事故はどういうと
きに起こるか
UNKNOWN
WHEREの条件が
UNKNOWNに
なってることが多
い さっきの例も
UNKNOWN
WHERE age >=
20 AND age < 20
は全員か?
UNKNOWN
年齢不明の人が
入らない
どうして
NULLがある
か
どうしてNULLがあるか
どうしてNULLがあるか
UNKNOWNは
Boolean以外でも
ありえる
どうしてNULLがあるか
データを実世界の
データを扱うため
に
不明は必要
どうしてNULLがあるか
UNKNOWNだけ
で
十分では?
どうしてNULLがあるか
足りない
どうしてNULLがあるか
1つのテーブルに
複数のデータタイ
プを入れるため
NULLは値じゃない
id 顧客名 取引額 年齢 性別
1 福沢諭吉 10000 56 男
2 慶応義塾大学 10000
3 樋口一葉 5000 20 女
4 野口英世 1000 41 男
5 野口英世記念館 1000
法人も個人も同じ
テーブルで管理した
い
どうしてNULLがあるか
UNIONした結果
を
テーブル自体に
どうしてNULLがあるか
OOPの継承を連
想
どうしてNULLがあるか
SQLでは容易に
カラムを増やした
り減らしたりでき
ない
どうしてNULLがあるか
NULL受け入れる
ことで多態性を
用意している
NULLと
上手につきあ
う
UNKNOWNについて
NULLと上手につきあう
1.NULLは値では
ないと意識する
NULLと上手につきあう
2. WHEREの条件
は2値理論に丸め
込む
NULLと上手につきあう
3. 出来る限り
not nullとdefault
の設定をする
NULLと上手につきあう
4. NULL可の
カラムは副次的な
情報に制限する
NULLと上手につきあう
5. 困ったら
COALESCEを使
う

Mais conteúdo relacionado

Destaque

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Destaque (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Nullのはなし(up用)