<?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>Forem: Haris Tallat</title>
    <description>The latest articles on Forem by Haris Tallat (@haris_tallat_d5c823f903ae).</description>
    <link>https://forem.com/haris_tallat_d5c823f903ae</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%2F2237440%2Fcae9cce2-7aa1-4037-ab9b-ab57054ea034.png</url>
      <title>Forem: Haris Tallat</title>
      <link>https://forem.com/haris_tallat_d5c823f903ae</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/haris_tallat_d5c823f903ae"/>
    <language>en</language>
    <item>
      <title>ACID Properties in Databases: A Key to Data Integrity and Reliability</title>
      <dc:creator>Haris Tallat</dc:creator>
      <pubDate>Sat, 16 Nov 2024 14:20:43 +0000</pubDate>
      <link>https://forem.com/haris_tallat_d5c823f903ae/acid-properties-in-databases-a-key-to-data-integrity-and-reliability-2fa8</link>
      <guid>https://forem.com/haris_tallat_d5c823f903ae/acid-properties-in-databases-a-key-to-data-integrity-and-reliability-2fa8</guid>
      <description>&lt;p&gt;ACID properties are crucial in ensuring that databases remain reliable and consistent, even in the face of system failures or crashes during transactions. These properties &lt;strong&gt;Atomicity, Consistency, Isolation, and Durability&lt;/strong&gt; help maintain data integrity in environments like banking systems, e-commerce applications, and online transactions.&lt;/p&gt;

&lt;p&gt;In this article, we’ll dive into each ACID property and explore their importance, real-life examples, and use cases.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y7rjf1kgnok5di5kios.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3y7rjf1kgnok5di5kios.png" alt="Image description" width="800" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What Are ACID Properties?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Atomicity&lt;/strong&gt;&lt;br&gt;
The term atomicity comes from the concept of an &lt;strong&gt;atom&lt;/strong&gt;, which is the smallest indivisible unit of matter. In the context of databases, atomicity refers to transactions being indivisible—either they &lt;strong&gt;fully succeed or fully fail&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If a transaction involves multiple steps, all of them must be completed successfully for the transaction to be considered successful. If any part of the transaction fails, the entire transaction is rolled back to maintain data integrity. This ensures that data is never left in an inconsistent or incomplete state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-Life Example: Switch Flipping&lt;/strong&gt;&lt;br&gt;
Imagine flipping a light switch in your house. The light can either be fully ON or OFF, but there’s no state in between. If the switch fails halfway, the light doesn’t flicker or partially turn on—it remains in its original state.&lt;/p&gt;

&lt;p&gt;Similarly, in a database transaction, if one part fails, the entire transaction fails, reverting any changes made.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bank Transfer Example:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User A, Account Balance: $100&lt;/li&gt;
&lt;li&gt;User B, Account Balance: $50&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If User A initiates a transfer of $50 to User B, and an error occurs during the transaction, atomicity ensures that no changes are made to the database—both users’ balances remain unchanged. &lt;strong&gt;Abort&lt;/strong&gt; will trigger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;— Abort :&lt;/strong&gt; If a transaction aborts, changes made to the database are not visible.&lt;/p&gt;

&lt;p&gt;If the transfer is successful, both User A and User B’s balances are updated, and the transaction is committed to the database. &lt;strong&gt;Commit&lt;/strong&gt; will trigger.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;— Commit :&lt;/strong&gt; If a transaction commits, changes made are visible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Consistency&lt;/strong&gt;&lt;br&gt;
Consistency ensures that the database remains in a valid state before and after the transaction. It guarantees that the database adheres to all defined rules, such as constraints, triggers, and relationships, sufficient balance after the transaction completes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-Life Example: Balance Validation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User A, Account Balance: $100&lt;/li&gt;
&lt;li&gt;User B, Account Balance: $50&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If User A tries to transfer $200 to User B, the consistency property ensures that the transaction fails because User A does not have sufficient funds. The database will not allow the transaction to complete if it violates any defined rules, such as ensuring account balances do not go negative.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Isolation&lt;/strong&gt;&lt;br&gt;
Isolation ensures that transactions are processed independently, even if they occur simultaneously. This means that the operations in one transaction are isolated from others and are not affected by concurrent transactions, preventing inconsistencies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Locking Mechanisms: Shared and Exclusive Locks&lt;/strong&gt;&lt;br&gt;
To maintain isolation, databases use locking mechanisms. Locks are applied to data to prevent other transactions from accessing or modifying it while the current transaction is in progress. There are two primary types of locks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Shared Lock (S-lock):&lt;/strong&gt; Allows a transaction to read the data but not modify it. Other transactions can also acquire a shared lock on the same data, allowing multiple transactions to read the data simultaneously but preventing modifications.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Exclusive Lock (X-lock):&lt;/strong&gt; Prevents other transactions from reading or modifying the data. This is applied when a transaction needs to update data to ensure that no other transaction interferes with the process.&lt;br&gt;
&lt;strong&gt;Real-Life Example: Bank Transfer with Concurrent Transactions&lt;/strong&gt;&lt;br&gt;
Let’s consider two transactions happening simultaneously:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transaction 1: User A is transferring $100 from Account A to Account B.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Transaction 2: User B is transferring $50 from Account B to Account C.&lt;br&gt;
Without isolation, these transactions could interfere with each other, leading to inconsistent balances.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How Isolation Works:&lt;/strong&gt;&lt;br&gt;
When Transaction 1 starts, it reads Account A’s balance (say, $500). At the same time, Transaction 2 reads Account B’s balance (say, $200). If there’s no isolation, Transaction 1 might update Account A’s balance to $400, while Transaction 2 could simultaneously update Account B’s balance to $150, leading to incorrect balances.&lt;/p&gt;

&lt;p&gt;With isolation, these two transactions operate independently, so:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transaction 1 locks Account A (using an exclusive lock) to prevent any other transaction from modifying it until the transaction is complete.&lt;/li&gt;
&lt;li&gt;Transaction 2 waits until Transaction 1 commits, at which point it reads the updated balance of Account B and proceeds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This prevents transactions from reading or modifying the same data simultaneously, avoiding inconsistencies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Handling Concurrent Transactions with Locking:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If both Transaction 1 and Transaction 2 try to &lt;strong&gt;access the same record at the same time&lt;/strong&gt;, the database will use &lt;strong&gt;locking&lt;/strong&gt; to ensure that only one transaction can proceed at a time, preventing data corruption.&lt;/li&gt;
&lt;li&gt;If both transactions attempt to lock the same resource, a &lt;strong&gt;deadlock&lt;/strong&gt; may occur, where both transactions are waiting for each other to release the lock. The database can resolve deadlocks by rolling back one of the transactions, ensuring the system doesn’t hang.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Durability&lt;/strong&gt;&lt;br&gt;
Durability ensures that once a transaction has been committed, its changes are permanent. Even in the event of system crashes, power outages, or failures, the changes made by the transaction will not be lost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-Life Example:&lt;/strong&gt; &lt;strong&gt;Power Outage after Bank Transfer&lt;/strong&gt;&lt;br&gt;
Let’s say User A transfers $500 to User B. After the transaction is committed, the system crashes due to a power outage. However, because the transaction was committed, the changes to both User A’s and User B’s balances are written to durable storage (such as a disk). When the system restarts, the committed transaction is recovered, and both users’ balances are updated as expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thank you for reading my article. Stay tuned for more in-depth articles&lt;/strong&gt; 😊&lt;/p&gt;

</description>
      <category>dbms</category>
      <category>database</category>
      <category>acid</category>
      <category>backenddevelopment</category>
    </item>
    <item>
      <title>Database and Their Relationships</title>
      <dc:creator>Haris Tallat</dc:creator>
      <pubDate>Sun, 03 Nov 2024 08:33:44 +0000</pubDate>
      <link>https://forem.com/haris_tallat_d5c823f903ae/understanding-database-and-relationships-edj</link>
      <guid>https://forem.com/haris_tallat_d5c823f903ae/understanding-database-and-relationships-edj</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. What is a Database?&lt;/strong&gt;&lt;br&gt;
A database is a structured collection of data stored electronically, designed to facilitate easy access, management, and updating of that data. You can think of it as a digital filing system where information is organized into tables, making it efficient to retrieve the data you need.&lt;br&gt;
&lt;strong&gt;Diagram&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenvebhesr7bn03ugo3n0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenvebhesr7bn03ugo3n0.png" alt="Image description" width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Components of a Database:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Data:&lt;/strong&gt; This refers to raw information, such as names, addresses, or transaction details.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tables:&lt;/strong&gt; A database consists of tables that resemble spreadsheets, organized into rows and columns. Each row represents a record (an individual entry), while each column represents a field (a specific piece of information about that entry).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DBMS&lt;/strong&gt;:A Database Management System (like MySQL, PostgreSQL, or Oracle) provides the tools to create, read, update, and delete data within the database.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;2. What is a Relationship?&lt;/strong&gt;&lt;br&gt;
In general terms, a relationship refers to a connection or association between two or more entities. In the context of databases, relationships define how data in one table relates to data in another. These relationships are essential for organizing and structuring data across multiple tables, helping to avoid redundancy (duplicated data) and enhancing data integrity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Notations:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakwgj3qmyy45y2bham87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakwgj3qmyy45y2bham87.png" alt="Image description" width="416" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpid4agpwxmwybmzsag8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpid4agpwxmwybmzsag8y.png" alt="Image description" width="800" height="444"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;3. Types of Database Relationships&lt;/strong&gt;&lt;br&gt;
A database relationship is a defined connection between two tables, specifying how records in one table relate to records in another. There are three primary types of database relationships:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.1 One-to-One Relationship&lt;/strong&gt;&lt;br&gt;
In a one-to-one relationship, each record in Table A corresponds to a single record in Table B, and vice versa. This type of relationship is often used when two tables contain different types of information about the same entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Each person has only one passport, and each passport is assigned to only one person.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Schema Diagram:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furguroexcawg8y6psgxo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furguroexcawg8y6psgxo.png" alt="Image description" width="800" height="380"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Notable Points:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Foreign Key Placement:&lt;/strong&gt; The PersonID is included in the Passport table instead of the Passport ID in the Person table because the passport is dependent on the person. If a person exists, the passport exists; a profile doesn’t make sense without a user. The table that has the dependency contains the foreign key.&lt;br&gt;
&lt;strong&gt;Direction of the Relationship:&lt;/strong&gt; The arrow in database relationship diagrams indicates which table contains the foreign key that references the other. When reading a one-to-one relationship in a database schema, starting from the foreign key (FK) side often provides clearer context.&lt;br&gt;
&lt;strong&gt;Table Representation&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqjpgzb0k12lnzxxxc11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqjpgzb0k12lnzxxxc11.png" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Golang Struct Details&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93m90fu4g1isw3dcvpyg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93m90fu4g1isw3dcvpyg.png" alt="Image description" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.2 One-to-Many Relationship&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A one-to-many relationship occurs when a single record in one table (the "one" side) can be associated with multiple records in another table (the "many" side). However, each record in the "many" table is linked back to only one record in the "one" table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; One user can create multiple posts. Each post will reference a single user, establishing a one-to-many relationship between the Users and Posts tables.&lt;br&gt;
&lt;strong&gt;Schema Diagram&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficbzoa3ms05pcnieih5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficbzoa3ms05pcnieih5v.png" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Single Entity:&lt;/strong&gt; The "one" side represents a single entity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple Associations:&lt;/strong&gt; The "many" side consists of multiple entities associated with that single entity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Foreign Key:&lt;/strong&gt; The "many" table contains a foreign key referencing the primary key of the "one" table.
&lt;strong&gt;Table Representation&lt;/strong&gt;
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wqsz6rdztkdlk7ry6cq.png" alt="Image description" width="800" height="346"&gt;
&lt;strong&gt;GoLang Struct&lt;/strong&gt;
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frj5e7fioh4ukg0y4yp3i.png" alt="Image description" width="800" height="334"&gt;
&lt;strong&gt;3.3 Many-to-Many Relationship&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In a many-to-many relationship, multiple records in one table can be associated with multiple records in another table. This relationship is typically implemented using a junction (or join) table that holds foreign keys referencing the primary keys of both tables.&lt;br&gt;
&lt;strong&gt;Schema Diagram&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxs4mrsqo3ku0oe2nqtdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxs4mrsqo3ku0oe2nqtdw.png" alt="Image description" width="800" height="972"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Without a Junction Table:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wcj4vktsoobm8imfgng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3wcj4vktsoobm8imfgng.png" alt="Image description" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Drawbacks of Not Using a Junction Table&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Data Redundancy&lt;/strong&gt; You may need to duplicate data across multiple rows, leading to inconsistencies and increased storage requirements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited Flexibility&lt;/strong&gt;: Not using a junction table restricts your ability to store additional attributes about the relationship itself (e.g., timestamps or status), complicating queries and reducing the richness of your data model.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Using a Junction Table:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eakty6fi1unv7ea56ft.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eakty6fi1unv7ea56ft.png" alt="Image description" width="800" height="723"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reduced Redundancy:&lt;/strong&gt; The Users and Roles tables are free from redundancy. Each user and each role is stored only once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clear and Manageable Relationships:&lt;/strong&gt; The junction table clearly defines the many-to-many relationships without confusion. It simplifies the process of adding or removing roles for users.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;GoLang Struct Representation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi64vjyqlz1kmt3h54xrs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi64vjyqlz1kmt3h54xrs.png" alt="Image description" width="800" height="583"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When and Why Summary Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73cnr9umyhvup92ckx1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F73cnr9umyhvup92ckx1n.png" alt="Image description" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>sql</category>
      <category>go</category>
      <category>dms</category>
    </item>
    <item>
      <title>Understanding Databases and Relationships</title>
      <dc:creator>Haris Tallat</dc:creator>
      <pubDate>Sun, 03 Nov 2024 07:44:03 +0000</pubDate>
      <link>https://forem.com/haris_tallat_d5c823f903ae/understanding-databases-and-relationships-fh4</link>
      <guid>https://forem.com/haris_tallat_d5c823f903ae/understanding-databases-and-relationships-fh4</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. What is a Database?&lt;/strong&gt;&lt;br&gt;
A database is a structured collection of data stored electronically, designed to facilitate easy access, management, and updating of that data. You can think of it as a digital filing system where information is organized into tables, making it efficient to retrieve the data you need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenvebhesr7bn03ugo3n0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenvebhesr7bn03ugo3n0.png" alt="Image description" width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Components of a Database:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Data:&lt;/strong&gt; This refers to raw information, such as names, addresses, or transaction details.&lt;br&gt;
&lt;strong&gt;Tables:&lt;/strong&gt; A database consists of tables that resemble spreadsheets, organized into rows and columns. Each row represents a record (an individual entry), while each column represents a field (a specific piece of information about that entry).&lt;br&gt;
&lt;strong&gt;DBMS:&lt;/strong&gt; A Database Management System (like MySQL, PostgreSQL, or Oracle) provides the tools to create, read, update, and delete data within the database.&lt;br&gt;
&lt;strong&gt;2. What is a Relationship?&lt;/strong&gt;&lt;br&gt;
In general terms, a relationship refers to a connection or association between two or more entities. In the context of databases, relationships define how data in one table relates to data in another. These relationships are essential for organizing and structuring data across multiple tables, helping to avoid redundancy (duplicated data) and enhancing data integrity.&lt;br&gt;
Examples of notations:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakwgj3qmyy45y2bham87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fakwgj3qmyy45y2bham87.png" alt="Image description" width="416" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpid4agpwxmwybmzsag8y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpid4agpwxmwybmzsag8y.png" alt="Image description" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Types of Database Relationships&lt;/strong&gt;&lt;br&gt;
A database relationship is a defined connection between two tables, specifying how records in one table relate to records in another. There are three primary types of database relationships:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.1 One-to-One Relationship&lt;/strong&gt;&lt;br&gt;
In a one-to-one relationship, each record in Table A corresponds to a single record in Table B, and vice versa. This type of relationship is often used when two tables contain different types of information about the same entity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Each person has only one passport, and each passport is assigned to only one person.&lt;/p&gt;

&lt;p&gt;Schema Diagram: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furguroexcawg8y6psgxo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Furguroexcawg8y6psgxo.png" alt="Image description" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notable Points:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Foreign Key Placement:&lt;/strong&gt; The PersonID is included in the Passport table instead of the Passport ID in the Person table because the passport is dependent on the person. If a person exists, the passport exists; a profile doesn’t make sense without a user. The table that has the dependency contains the foreign key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Direction of the Relationship:&lt;/strong&gt; The arrow in database relationship diagrams indicates which table contains the foreign key that references the other. When reading a one-to-one relationship in a database schema, starting from the foreign key (FK) side often provides clearer context.&lt;/p&gt;

&lt;p&gt;Here is the sample table explanation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqjpgzb0k12lnzxxxc11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwqjpgzb0k12lnzxxxc11.png" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;br&gt;
Go lang strcut details&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93m90fu4g1isw3dcvpyg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93m90fu4g1isw3dcvpyg.png" alt="Image description" width="800" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.2 One-to-Many Relationship&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficbzoa3ms05pcnieih5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficbzoa3ms05pcnieih5v.png" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A one-to-many relationship occurs when a single record in one table (the "one" side) can be associated with multiple records in another table (the "many" side). However, each record in the "many" table is linked back to only one record in the "one" table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; One user can create multiple posts. Each post will reference a single user, establishing a one-to-many relationship between the Users and Posts tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Characteristics:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Single Entity:&lt;/strong&gt; The "one" side represents a single entity.&lt;br&gt;
Multiple Associations: The "many" side consists of multiple entities associated with that single entity.&lt;br&gt;
Foreign Key: The "many" table contains a foreign key referencing the primary key of the "one" table.&lt;br&gt;
Table:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wqsz6rdztkdlk7ry6cq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wqsz6rdztkdlk7ry6cq.png" alt="Image description" width="800" height="346"&gt;&lt;/a&gt;&lt;br&gt;
Golang struct:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frj5e7fioh4ukg0y4yp3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frj5e7fioh4ukg0y4yp3i.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.3 Many-to-Many Relationship&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxs4mrsqo3ku0oe2nqtdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxs4mrsqo3ku0oe2nqtdw.png" alt="Image description" width="800" height="972"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a many-to-many relationship, multiple records in one table can be associated with multiple records in another table. This relationship is typically implemented using a junction (or join) table that holds foreign keys referencing the primary keys of both tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Without a Junction Table&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzwx21by4t2b500zbqx81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzwx21by4t2b500zbqx81.png" alt="Image description" width="800" height="972"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When not using a junction table, you may encounter several drawbacks:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Redundancy:&lt;/strong&gt; You may need to duplicate data across multiple rows, leading to inconsistencies and increased storage requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limited Flexibility:&lt;/strong&gt; Not using a junction table restricts your ability to store additional attributes about the relationship itself (e.g., timestamps or status), complicating queries and reducing the richness of your data model.&lt;/p&gt;

&lt;p&gt;........&lt;/p&gt;

&lt;p&gt;Using Junction Table&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eakty6fi1unv7ea56ft.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eakty6fi1unv7ea56ft.png" alt="Image description" width="800" height="723"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Benefits of Using a Junction Table&lt;br&gt;
Reduced Redundancy:&lt;/p&gt;

&lt;p&gt;The Users and Roles tables are free from redundancy. Each user and each role is stored only once.&lt;br&gt;
Clear and Manageable Relationships:&lt;/p&gt;

&lt;p&gt;The junction table clearly defines the many-to-many relationships without confusion. It simplifies the process of adding or removing roles for users.&lt;/p&gt;

</description>
      <category>database</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
