Logo

dev-resources.site

for different kinds of informations.

Database and Their Relationships

Published at
11/3/2024
Categories
database
sql
go
dms
Author
haris_tallat_d5c823f903ae
Categories
4 categories in total
database
open
sql
open
go
open
dms
open
Author
25 person written this
haris_tallat_d5c823f903ae
open
Database and Their Relationships

1. What is a Database?
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.
Diagram

Image description

Key Components of a Database:

  1. Data: This refers to raw information, such as names, addresses, or transaction details.
  2. Tables: 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).
  3. DBMS:A Database Management System (like MySQL, PostgreSQL, or Oracle) provides the tools to create, read, update, and delete data within the database.

2. What is a Relationship?
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.

Example of Notations:
Image description

Image description
3. Types of Database Relationships
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:

3.1 One-to-One Relationship
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.

Example: Each person has only one passport, and each passport is assigned to only one person.

Schema Diagram:
Image description
Notable Points:

Foreign Key Placement: 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.
Direction of the Relationship: 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.
Table Representation
Image description
Golang Struct Details

Image description

3.2 One-to-Many Relationship

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.

Example: 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.
Schema Diagram
Image description
Characteristics:

  1. Single Entity: The "one" side represents a single entity.
  2. Multiple Associations: The "many" side consists of multiple entities associated with that single entity.
  3. Foreign Key: The "many" table contains a foreign key referencing the primary key of the "one" table. Table Representation Image description GoLang Struct Image description 3.3 Many-to-Many Relationship

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.
Schema Diagram
Image description

Example Without a Junction Table:

Image description

Drawbacks of Not Using a Junction Table

  1. Data Redundancy You may need to duplicate data across multiple rows, leading to inconsistencies and increased storage requirements.
  2. Limited Flexibility: 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.

Benefits of Using a Junction Table:
Image description

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

GoLang Struct Representation

Image description

When and Why Summary Table

Image description

dms Article's
30 articles in total
Favicon
Top 9 Challenges and Solutions for eDMS Software in Pharma Industry
Favicon
Optimizing Security and Efficiency in AWS Database Migration with DMS: Understanding Outbound-Only Connectivity
Favicon
Database and Their Relationships
Favicon
AWS Database Migration Service now includes enhanced monitoring dashboard for your DMS tasks
Favicon
AWS DMS Table failure due to freeze period in source
Favicon
Building a Scalable Data Platform: Addressing Uncertainty in Data Requirements with AWS
Favicon
Testing AWS Database Migrations & Accelerating Development with Cloud Pods
Favicon
DMS error - Failed to get the capture list from the endpoint
Favicon
Distribution Management Software
Favicon
Troubleshooting and Fixing AWS DMS Instance Creation Error
Favicon
Why AWS DMS is loved by ETL experts
Favicon
RDS Database Migration Series - A horror story of using AWS DMS with a happy ending
Favicon
Migrando geometries con DMS
Favicon
Download Amazon invoices automatically
Favicon
Amazon Rechnungen automatisiert herunterladen
Favicon
Jeder braucht ein DMS zuhause
Favicon
Everyone needs a DMS at home
Favicon
Using DMS to replicate data from RDS(MySQL) to Open Search
Favicon
How can product suppliers balance DMS benefits with integration challenges for an efficient supply chain?
Favicon
AWS DMS - Database Migration to AWS
Favicon
Database Migration steps with the AWS CLI - 2
Favicon
Database Migration steps with the AWS CLI - 1
Favicon
AWS DMS and Prefect: The Key to Building a Robust Data Warehouse
Favicon
Migrating a MySQL database to AWS RDS using AWS DMS with zero downtime
Favicon
Case Study: How to replicate database from AWS to outside?
Favicon
Build An App Like Amazon - Amazon Clone App
Favicon
Implementing Change Data Capute (CDC) with Aurora Serverless v2
Favicon
Configure SSL between RDS and Weblogic / DMS endpoint
Favicon
How to use SQL Developer’s Diff Wizard to Complete a DMS Migration for Oracle 12c
Favicon
AWS Database Migration Service: Limitations

Featured ones: