<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Harry Bui</title>
    <description>The latest articles on DEV Community by Harry Bui (@harrybui).</description>
    <link>https://dev.to/harrybui</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2199194%2F7fe3739a-8119-4944-b4de-ab4ba2238e1e.png</url>
      <title>DEV Community: Harry Bui</title>
      <link>https://dev.to/harrybui</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harrybui"/>
    <language>en</language>
    <item>
      <title>Transactions</title>
      <dc:creator>Harry Bui</dc:creator>
      <pubDate>Thu, 16 Jan 2025 17:15:55 +0000</pubDate>
      <link>https://dev.to/harrybui/transactions-3gpm</link>
      <guid>https://dev.to/harrybui/transactions-3gpm</guid>
      <description>&lt;p&gt;In this section, we will explore the concept of &lt;strong&gt;transactions&lt;/strong&gt; and &lt;strong&gt;ACID properties&lt;/strong&gt;—an important topic in database management to ensure data integrity and reliability.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is a Transaction?
&lt;/h2&gt;

&lt;p&gt;Imagine a database transaction as the action of withdrawing money from your bank account. You need to perform multiple actions, such as checking your balance, entering the amount to withdraw, and completing the withdrawal. You expect the entire process to run smoothly without errors. &lt;/p&gt;

&lt;p&gt;Similarly, a &lt;strong&gt;database transaction&lt;/strong&gt; is a unit of work that consists of a collection of queries, which must be executed as a whole or not at all. This &lt;strong&gt;all-or-nothing principle&lt;/strong&gt; ensures data integrity and reliability. Transactions are primarily used to change or modify data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Transaction Lifespan
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;BEGIN&lt;/code&gt; to start a transaction.
&lt;/li&gt;
&lt;li&gt;Once all operations are successfully performed, use &lt;code&gt;COMMIT&lt;/code&gt; to finalize and save the changes to the database.
&lt;/li&gt;
&lt;li&gt;If any errors occur during the transaction, use &lt;code&gt;ROLLBACK&lt;/code&gt; to undo all changes that have not yet been saved to the database.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example: How a Transaction Works
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario&lt;/strong&gt;: Transferring money between bank accounts.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Table: &lt;code&gt;bank_accounts&lt;/code&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;id&lt;/th&gt;
&lt;th&gt;balance&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;$1000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;$200&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Transaction Steps:
&lt;/h3&gt;

&lt;p&gt;User 1 wants to send $100 to User 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Start Transaction&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;bank_accounts&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Check User 1's balance (should be &amp;gt; $100)&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;bank_accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Deduct $100 from User 1&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;bank_accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Add $100 to User 2&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Finalize and save the transaction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This example demonstrates how transactions work in practice to ensure data integrity. By wrapping the operations in a transaction, we guarantee that the money transfer is completed entirely or not at all, preventing issues like incorrect balances if an error occurs midway. &lt;/p&gt;

&lt;p&gt;In the next section, we will explore the &lt;strong&gt;ACID properties&lt;/strong&gt;, which further define how transactions ensure consistency, durability, and reliability in databases.&lt;/p&gt;

</description>
      <category>database</category>
      <category>computerscience</category>
      <category>backend</category>
    </item>
    <item>
      <title>Series: Learn Database Fundamental</title>
      <dc:creator>Harry Bui</dc:creator>
      <pubDate>Thu, 16 Jan 2025 16:28:40 +0000</pubDate>
      <link>https://dev.to/harrybui/series-learn-database-fundamental-5c3</link>
      <guid>https://dev.to/harrybui/series-learn-database-fundamental-5c3</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this series, I will note and share all things I learn about &lt;strong&gt;Database Fundamentals&lt;/strong&gt;. This series will not focus on the SQL language syntax. Instead, we will focus on topics that you can apply in database engineering, which are common across all databases (e.g., MySQL, SQL Server, PostgreSQL, etc.). &lt;/p&gt;

&lt;p&gt;For this series, we will primarily discuss &lt;strong&gt;PostgreSQL&lt;/strong&gt; and cover the following topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/harrybui/transactions-3gpm"&gt;Transactions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;ACID Properties&lt;/li&gt;
&lt;li&gt;Database Internals&lt;/li&gt;
&lt;li&gt;Index in Database&lt;/li&gt;
&lt;li&gt;Explain B-Tree and B+Tree&lt;/li&gt;
&lt;li&gt;Partition, Sharding, Replica in Database&lt;/li&gt;
&lt;li&gt;Locking&lt;/li&gt;
&lt;li&gt;Normalization and Denormalization&lt;/li&gt;
&lt;li&gt;How to Design a Database: Step-by-Step&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Ask for Help or Give Feedback
&lt;/h2&gt;

&lt;p&gt;If you need any assistance or have feedback to share, you can reach out to me through the following channels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Email&lt;/strong&gt;: &lt;a href="//mailto:mr.flooo1230@gmail.com"&gt;mr.flooo1230@gmail.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub&lt;/strong&gt;: &lt;a href="https://github.com/harrybui03" rel="noopener noreferrer"&gt;harrybui03&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LinkedIn&lt;/strong&gt;: &lt;a href="https://www.linkedin.com/in/harry-bui-41b01b24b/" rel="noopener noreferrer"&gt;Harry Bui&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>database</category>
      <category>computerscience</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
