SlideShare a Scribd company logo
データベース入門(第3回)
前回のあらすじ。
データベースを使うためにはSQLという言葉を使います。
そのSQLにはDDL、DML、DCLといった種類がありました。
テーブルを作るときなどはDDLである、CREATE文なども使いますが、
データベースのテーブルの中のデータを操作するのに使うのは主に、
DMLである、SELECT、INSERT、UPDATE、DELETEでしょう。
そしてこれらと密接にかかわってくるのが、COMMIT、ROLLBACKなどの
DCLの存在です。
今日はこの、COMMITやROLLBACKによってできることを
ざっくりと、説明します。
トランザクションとは。
トランザクション。
そもそもトランザクションという言葉は、データベースの世界だけの言葉では
ありません。
いろいろな分野で、トランザクションという単語が使われています。
今回のトランザクションというのは、あくまで
「データベースの世界」に閉じた話だということを頭の片隅に
置いていてください。
また、トランザクションにはロックという概念、
「鍵をかけてほかの人が触れないようにする」といった意味合いの言葉が
密接に絡んでくるのですが、この話も時間がかかるので割愛します。
トランザクションの定義。
データベースとしてのトランザクションの定義としては、おおまかに以下のよ
うに書くことができます。
1)複数のデータの登録や削除や更新をすべて有効なものとする(これを「コ
ミットする」といいます)、もしくはすべて無効なものとして元の状態に戻す
(これを「ロールバックする」といいます)といった、半分だけ登録されてい
るといった中途半端な状態にならないようにするための機能
2)ほかの人の操作からの影響を防ぐ(Aさんが編集中のデータをBさんは編
集できない、または見ることもできない)
2のほうは、データベースにかかわらず、ネットワーク共有フォルダのExcel
ファイルを複数人で開いたときのことを考えると、わかりやすいかもしれませ
ん。
意味不明なので。
1)複数のデータの登録や削除や更新をすべて有効なものとする、もしくはす
べて無効なものとして元の状態に戻す
というのは、
Aさん「トランザクション開始!」
Aさん「1のデータをデータベースに登録したよ!」
Aさん「2のデータを消したよ!」
Aさん「トランザクション完了!(コミット!)」
⇒こうすると、1のデータが登録され、2のデータが削除された状態で確定に
なります。
つまり、Excelなどで考えれば、いろいろ書いて、シートも1つ消して、保存
ボタン押してExcelを終了したら、書いた内容は保存されていて、削除した
シートもなくなっている状態になっているということです。
意味不明なので。
1)もしくはすべて無効なものとして元の状態に戻す
というのは、
Aさん「トランザクション開始!」
Aさん「1のデータをデータベースに登録したよ!」
Aさん「2のデータを消したよ!」
Aさん「やっぱやめ!(ロールバック!)」
⇒こうすると、1のデータは登録されず、2のデータも削除されていない状態
に戻った状態になります。
つまり、Excelなどで考えれば、いろいろ書いて、シートも1つ消したけど、
保存しないでExcelを終了しちゃったら、書いた内容はなくなっちゃうし、
シートも削除されていない状態に戻っちゃう、ということです。
意味不明なので。
2)ほかの人の操作からの影響を防ぐ(Aさんが編集中のデータをBさんは編
集できない、または見ることもできない)
というのは、
Aさん「トランザクション開始!」
Aさん「1のデータを変更したよ!」
Bさん「1のデータを見よう!でもなんか変更できない!」
Bさん「なら、2のデータを見よう!みれた!」
Aさん「2のデータを消したよ!」
Aさん「トランザクション完了!(コミット)」
Bさん「もう一度検索しよう。あれ?1のデータが変わってる?」
Bさん「あれ?2のデータがない!」
⇒このように、1のデータは変更され、2のデータは削除された状態になりますが、A
さんがトランザクションを完了(コミット)した場合は、Bさんは開いているときは見
えたままですが、SELECTを再実行すると変更後の状態が見えるようになります。
よくある説明。
トランザクションの説明でよく出てくる例が、銀行の口座振込みの例です。
例えば、Aさんの口座からBさんの口座に、50万円を振り込みました、
って時の場合、Aさんの口座から50万円引くのは正常に終わったけど、
Bさんの口座に50万円移すときに障害が起こって、Bさんの口座には50万
円は入らなかった。。。。Aさんは大損ですね。
それをなくすためには。
この振込みをする前に、
「トランザクション開始!」と叫んでみましょう。
「Aさんの口座からの引落⇒Bさんの口座への入金」という一連の流れを
トランザクションのなかで行うということです。
そうすれば、Bさんに入金失敗しても、魔法の呪文「ロールバック!」を唱え
れば、Aさんの口座にはお金が戻ってきます。
ACID特性。
ちょっと難しい言葉と話になりますが、
トランザクションはACID特性を持っています。
Atomicity(原子性):トランザクションは実行が完了するか、あるいはまっ
たく実行されないかのどちらかとなる
Consistency(整合性):トランザクションの開始と終了時には、データベー
スは整合性を保った状態となっている
Isolation(分離性):トランザクションは別のトランザクションの影響を受け
ない
Durability(持続性):トランザクションが正常に完了し、変更が確定された
データは確実に保持される
先ほどの銀行口座のお話も、これらを満たしていると、正しく処理が行われる
ことになります。
ACID特性。
・原子性
もし2の処理を行った時点でトランザクションが終了した場合、Aさんの口座から
1000円が引かれただけの状態になってしまいます。原子性はこういった中途半端な状
態になることを防ぎます
・整合性
もし2の処理で間違ってAさんの口座から20000円を引いてしまい、3の処理でBさ
んに20000円正常に足してしまった場合、トランザクション終了時にAさんはマイナ
スの残高となってしまいます。これはありえないので、正しくない状態になることを防
ぎます
1振り込み開始前 Aさんの口座10000円、Bさんの口座5000円
2Aさんの口座から1000円引く Aさんの口座9000円、Bさんの口座5000円
3Bさんの口座に1000円足す Aさんの口座9000円、Bさんの口座6000円
ACID特性。
・分離性
2の処理中に、Aさんとは別にCさんがBさんの口座に1000円振り込んだ場合、Bさ
んの残高は6000円となります。それとは別に2の処理の後にAさんの振込みの結果と
して、Bさんの残高を6000円で上書きしてしまうような状況が発生すると、本来はC
さんからの振込みが1000円、Aさんからの振込みが1000円でBさんの残高が7000
となるところが6000円になってしまいます。
分離性はこのようにトランザクションが別のトランザクションの影響を受けてしまうこ
とを防ぎます。
・持続性
完了したトランザクションが消失してしまった場合、どのトランザクションをやり直す
のか判別して再度やり直すのかを判別し、場合によっては再度実行する必要があります。
持続性は、完了したトランザクションの結果は消失するのを防ぎます。
1振り込み開始前 Aさんの口座10000円、Bさんの口座5000円
2Aさんの口座から1000円引く Aさんの口座9000円、Bさんの口座5000円
3Bさんの口座に1000円足す Aさんの口座9000円、Bさんの口座6000円
出てくるコマンド。
トランザクションに関係するSQLとしては、
トランザクションの開始と終了を自分で宣言する場合に使う、
BEGIN(TRANSACTION)、START TRANSACTION、
トランザクションを終了して変更などを確定するCOMMIT、
トランザクションを中止して変更などをなかったことにして元の状態に戻す
ROLLBACKなどがあります。
データベースの種類によってはトランザクション開始を宣言しなくても、
勝手に始まって、勝手に終了するものもあります。
勝手に始まる場合、暗黙のトランザクションといって、
裏でトランザクションが使われているような感じです。
コミットしたら確定して保存される。
Excelでも、保存ボタンをクリックしたら、編集した内容がファイルに保存さ
れます。一度保存したら、そのファイルを削除しない限り、ずっと残ります。
データベースもこれと同じです。
COMMITを実行したら、データベースに変更した内容が保存されます。
データベースの内部では、このCOMMITのタイミングで裏でいろいろ小人さ
んが作業をしているのですが、
その内容はOracle、PostgreSQL、MySQL、SQLServerなどで
やっていることが違います。
でも、使う人はとりあえず、COMMITしたら保存が確定する、と覚えておけ
ばいいかなと思います。
ロールバックしたらもとにもどる。
Excelでも、保存さえしなければ、閉じて開きなおしたら、元の状態に戻りま
すし、「CTRL+Z」キーで、たいていのアプリケーションは1つ前の状態に
戻ります。データベースと同じようなことをしています。
なぜ戻れるかといえば、変更した情報をすべて保存しているので、
なにをやったかが記録されていて、戻すときは保存した場所から古い情報を
もってきて、昔の状態にもどすということです。
なにを変更したか記録を保存する方法や、保存している場所は、
データベースによって仕組みが違います。
やり直したい、元に戻したい、というときは、とりあえずロールバックすれば
いいと覚えておけばいいかなと思います。
ただし、一度でもコミットしてしまったら、そこまでしかもどりません!
まとめ。
・トランザクションはDBが標準的に持っている機能です。
・トランザクション開始~コミットまたはロールバックで、一つのトランザク
ションが成立します。
・確定するか、なかったことにするか、そのどちらかの状態しかありません。
・コマンドは細かい違いはあれど、OracleでもMySQLでもPostgreSQLでも
かわりません。COMMITとROLLBACKは存在します。
・COMMITは保存、ROLLBACKは全部取り消し、みたいなものです。
・プログラムを作るときは、自動的にトランザクション制御をやってくれる場
合もあります。
・もちろん自分でトランザクション開始処理やCOMMIT処理を書かなければ
ならない場合もあります。
・プログラムによるトランザクション制御の違いは、実際に使うときに覚えて
ください。
つぎ。
次回は、SQLの関数や文法などを掘り下げるか、
インデックスなどのデータベース内のオブジェクト
について話すか、検討中です。
ご清聴ありがとうございました。

More Related Content

Recently uploaded

NIST Cybersecurity Framework 2.0の変更点整理をしよう
NIST Cybersecurity Framework 2.0の変更点整理をしようNIST Cybersecurity Framework 2.0の変更点整理をしよう
NIST Cybersecurity Framework 2.0の変更点整理をしよう
You&I
 
アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)
アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)
アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)
You&I
 
FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)
FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)
FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)
fisuda
 
受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf
受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf
受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf
ooishi1
 
Grokking Simplicity探訪
Grokking Simplicity探訪Grokking Simplicity探訪
Grokking Simplicity探訪
Yoshitaka Kawashima
 
CO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdf
CO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdfCO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdf
CO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdf
yamamotominami
 

Recently uploaded (6)

NIST Cybersecurity Framework 2.0の変更点整理をしよう
NIST Cybersecurity Framework 2.0の変更点整理をしようNIST Cybersecurity Framework 2.0の変更点整理をしよう
NIST Cybersecurity Framework 2.0の変更点整理をしよう
 
アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)
アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)
アジャイルの30年(Tree Decades of Agileというブログ記事に関する要約)
 
FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)
FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)
FIWARE Orion Context Broker コンテキスト情報管理 (Orion 4.0.0対応)
 
受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf
受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf
受発注バスターズ説明資料  株式会社batton Saleshub掲載用.pdf
 
Grokking Simplicity探訪
Grokking Simplicity探訪Grokking Simplicity探訪
Grokking Simplicity探訪
 
CO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdf
CO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdfCO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdf
CO2排出量見える化・削減・報告クラウド「アスエネ」サービス紹介_Saleshub.pdf
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
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
Neil 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 2024
Albert 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 Insights
Kurio // 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 2024
Search 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 summary
SpeakerHub
 
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 Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit 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 management
MindGenius
 
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
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
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...
 

データベース入門3