Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Database Design and Modeling with PostgreSQL and MySQL: Build efficient and scalable databases for modern applications using open source databases
Database Design and Modeling with PostgreSQL and MySQL: Build efficient and scalable databases for modern applications using open source databases
Database Design and Modeling with PostgreSQL and MySQL: Build efficient and scalable databases for modern applications using open source databases
Ebook501 pages3 hours

Database Design and Modeling with PostgreSQL and MySQL: Build efficient and scalable databases for modern applications using open source databases

Rating: 0 out of 5 stars

()

Read preview
LanguageEnglish
Release dateJul 26, 2024
ISBN9781803240961
Database Design and Modeling with PostgreSQL and MySQL: Build efficient and scalable databases for modern applications using open source databases

Related to Database Design and Modeling with PostgreSQL and MySQL

Related ebooks

Data Modeling & Design For You

View More

Reviews for Database Design and Modeling with PostgreSQL and MySQL

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Database Design and Modeling with PostgreSQL and MySQL - Alkin Tezuysal

    Cover.png

    Database Design and Modeling with PostgreSQL and MySQL

    Copyright © 2024 Packt Publishing

    All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

    The authors acknowledge the use of cutting-edge AI, such as ChatGPT, with the sole aim of enhancing the language and clarity within the book, thereby ensuring a smooth reading experience for readers. It’s important to note that the content itself has been crafted by the authors and edited by a professional publishing team.

    Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

    Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

    Associate Group Product Manager: Apeksha Shetty

    Book Project Manager: Aparna Nair

    Senior Editor: Tiksha Lad

    Technical Editor: Seemanjay Ameriya

    Copy Editor: Safis Editing

    Proofreader: Tiksha Lad

    Indexer: Subalakshmi Govindhan

    Production Designer: Nilesh Mohite

    Senior DevRel Marketing Executive: Nivedita Singh

    First published: June 2024

    Production reference: 1280624

    Published by Packt Publishing Ltd.

    Grosvenor House

    11 St Paul’s Square

    Birmingham

    B3 1RB, UK.

    ISBN 978-1-80323-347-5

    www.packtpub.com

    To my mother, Sermin Tezuysal, and the memory of my father, Nilhan Tezuysal, for their sacrifices and for exemplifying the power of determination. To my wife, Aslihan, and my daughters, Lara and Ilayda, for being my loving partners throughout our joint life journey.

    – Alkin Tezuysal

    To my beloved parents, whose presence I deeply miss – your dedication has left an indelible mark on my life and accomplishments. Equally, I cannot overlook the profound impact of my teachers and mentors throughout my educational and professional journey.

    – Ibrar Ahmed

    Foreword

    Databases underpin almost any application you use on an everyday basis, and without proper database design, you can’t achieve optimal database performance and availability, despite overspending on the database hardware or cloud fees. It is not uncommon to see 100x or more difference in performance and efficiency between outstanding and mediocre database design.

    Yet database design and modeling do not only impact database applications; they impact development too. Poor database design will lead to a slow development pace, a higher volume of bugs, and more difficulty in introducing new features, whereas with good database design, you can build applications faster than your competition while maintaining higher quality and better user experience.

    In this book, Alkin and Ibrar examine two of the most popular open source databases – PostgreSQL and MySQL. This is a fantastic choice because examining more than one technology allows the authors to show their similarities and contrast the differences between them, helping you to take your understanding to a level impossible to achieve by focusing on just one database.

    I had the pleasure of working with both Ibrar and Alkin and can say they are not only fantastic engineers, having helped countless customers design and run databases better, but also passionate educators – having written books, spoken at countless conferences, and authored countless articles. Coming together, they have experience, knowledge, and passion second to none.

    In addition to covering MySQL and PostgreSQL as they are now, Alkin and Ibrar take us to the future, covering both emerging developments in MySQL and PostgreSQL ecosystem as well as general market trends, such as cloud databases and columnar databases – being aware of these trends is absolutely essential for any well-rounded developer.

    Whenever you are a database-intensive application developer or responsible for database operations, this book can help you take your skills in essential areas of database design and modeling to the next level.

    Peter Zaitsev

    Founder of Percona and a MySQL Rockstar

    Contributors

    About the authors

    Alkin Tezuysal is EVP of Global Services at ChistaDATA Inc. He has extensive experience in open-source relational databases, working in various sectors and large functions. With over three decades of industry experience, he has led global operations teams for MySQL customers and users. He's a known speaker at worldwide open-source database events.

    He was awarded Most Influential in Database Community 2022 by The Redgate 100. He has co-authored MySQL Cookbook, 4th Edition 2022, by O'Reilly Media, Inc. He was awarded MySQL Rockstar 2023 - Oracle (MySQL Community).

    Ibrar Ahmed is an accomplished principal engineer at Percona LLC with over 23 years of experience in designing and developing software. He is a renowned expert in the field of the PostgreSQL core database engine, having contributed significantly to open source development. His extensive knowledge and expertise have led to the implementation of major performance feature enhancements and the development of various PostgreSQL modules.

    Apart from his specialization in PostgreSQL, Ibrar has a wealth of experience working with other prominent databases, including MySQL, Oracle, and NoSQL databases such as MongoDB and Hadoop. He is also well-versed in tools related to databases, such as Hive, HBase, and Spark.

    About the reviewers

    Naresh Kumar Miryala is a highly experienced engineering leader with nearly 20 years of industry experience and a strong background in the cloud, platform engineering, and artificial intelligence. He leads high-performing cloud data platform teams in his current role at Meta Platforms, Inc. He has a proven track record of carrying out cloud transformations, infrastructure implementation, database management, ERP solutions, and DevOps deployments. His expertise spans multiple domains, such as database systems, large-scale backend infrastructure, security, multi-cloud deployments, cloud infrastructure, DevOps, and artificial intelligence.

    I’d like to thank my dear wife, Aruna, and my kids, Yojith and Yuvan, who understand the time and commitment it takes to research. I am grateful for your unwavering belief in me and for always pushing me to be my best self.

    Frédéric Descamps has been working with open source and MySQL technology for more than 25 years. After graduating in management information technology, Frédéric started his career as a developer for a multinational company. He then opted for a different career, joining one of the first Belgian start-ups fully dedicated to open source projects around GNU/Linux. He joined the MySQL Community Team in 2016 as a MySQL community manager and evangelist. Frédéric is also a regular speaker at open source conferences. His blog, mostly dedicated to MySQL, is at http://lefred.be. Frédéric is also the devoted father of three adorable daughters, Wilhelmine, Héloïse, and Barbara.

    Table of Contents

    Preface

    Part 1: Introduction to Databases

    1

    SQL and NoSQL Databases: Characteristics, Design, and Trade-Offs

    Understanding databases and data models

    Exploring the relational data model (SQL databases)

    Tables, rows, and columns

    Normalization

    Structured Query Language (SQL)

    ACID transactions

    Navigating the document data model (NoSQL databases)

    Data models in NoSQL

    Types of NoSQL databases

    Key-value stores

    Document stores

    Column-family stores

    Graph databases

    Applying the CAP theorem and NoSQL design choices

    Consistency

    Availability

    Partition tolerance

    Consistency models in NoSQL databases

    NoSQL design choices and use cases

    Managing transaction management and concurrency control in NoSQL

    BASE transactions in NoSQL databases

    Reasons for the BASE model in NoSQL databases

    Implications for data integrity and concurrency control

    Analyzing the advantages and disadvantages of NoSQL databases

    Advantages of NoSQL databases

    Disadvantages of NoSQL databases

    Summary

    2

    Building a Strong Foundation for Database Design

    The importance of a solid foundation in database design

    Key terms and data models

    Understanding the relational model in detail

    Identifying entities, attributes, and relationships in database design

    Creating an ER diagram

    Advanced database design

    Normalization – reducing redundancy and improving data integrity

    Achieving normal forms – 1NF, 2NF, and 3NF

    Ensuring database validity and integrity

    Concluding thoughts

    Summary

    Part 2: Practical Implementation

    3

    Getting Your Hands Dirty with PostgreSQL and MySQL

    Understanding the sample database

    EDA and preprocessing

    Database schema

    MySQL

    Concluding thoughts

    Summary

    Part 3: Core Concepts in Database Design

    4

    Mastering the Building Blocks of Database Design and Modeling

    Understanding database objects

    Understanding data types and constraints

    Keys and how to use them

    Database checks and constraints

    Check constraint

    Default constraint

    Not null constraint

    Checking data quality with constraints

    No date in the future

    Value within a specific range

    How to avoid redundancy

    Database consistency and beyond

    Transactions – ensuring data integrity

    Concurrency control – managing multiple users

    PostgreSQL

    MySQL

    Summary

    Part 4: Advanced Database Techniques

    5

    Advanced Techniques for Advanced Databases

    Creating custom views of your data

    Understanding the purpose of views

    The advantages of using views in database management

    Indexing – how to find data faster

    Understanding indexing

    Types of indexes

    How indexing works

    PostgreSQL indexes

    MySQL indexes

    Stored procedures – reusable code for your database

    UDFs

    The essence of UDFs

    Efficiency through reusability – the role of UDFs in code optimization

    UDFs and performance optimization in database operations

    Using UDFs to enrich SQL queries for expressive interactions

    Practical insights into UDFs in MySQL and PostgreSQL

    Understanding CTEs

    The role of CTEs in code modularity

    Components and integration of CTEs

    Guidelines for efficient CTE usage

    Advanced strategies with CTEs

    Case studies in action – real-world examples of CTE transformations

    Summary

    6

    Understanding Database Scalability

    Introducing database scaling

    Challenges in database scaling

    Primary methods of database scaling

    Vitess – a horizontal scaling solution for MySQL

    Citus – a horizontal scaling solution for PostgreSQL

    Vertical scaling – enhancing capacity within existing infrastructure

    InnoDB Cluster for MySQL

    Sharding and resharding

    Future trends and emerging challenges in database scaling

    Serverless databases

    Summary

    Part 5: Best Practices and Future Trends

    7

    Best Practices for Building and Maintaining Your Database

    Designing for performance – optimizing database efficiency

    Schema design

    Query optimization

    Advanced query optimization techniques

    Using subqueries and JOIN clauses wisely

    Utilizing temporary tables

    Optimizing aggregates and GROUP BY clauses

    Using an index to assist GROUP BY clauses

    Iterative process and analysis

    Understanding database scaling

    Vertical scaling

    Horizontal scaling

    Replication in PostgreSQL

    MySQL replication

    Ensuring database security

    Access control

    Implementing robust authentication mechanisms

    Permission models

    PostgreSQL permission model

    Roles and privileges

    Example setup in PostgreSQL

    MySQL permission model

    Privileges and levels

    Example setup in MySQL

    Management and flexibility

    Exploring data encryption

    Protecting data at rest

    Protecting data in transit

    Monitoring and auditing

    Data quality and governance – maintaining high standards

    Learning about data cleaning

    Data governance frameworks

    Compliance

    Collaboration and documentation

    Effective communication

    Documentation practices

    Advanced topics in database management

    Backup and recovery

    Tools for monitoring PostgreSQL

    MySQL performance monitoring tools

    Strategies for regular performance analysis

    Summary

    8

    The Future of Databases and Their Designs

    Understanding vectorized search

    MySQL enhancements and innovations

    MySQL HeatWave

    Prospects of use cases of HeatWave

    TiDB as a MySQL drop-in replacement for distributed systems

    PostgreSQL – expanding horizons

    pgEdge – fully distributed PostgreSQL optimized for the network edge

    Multi-master replication in pgEdge

    Simplified cluster management

    Enhanced backup and recovery

    Advanced monitoring and alerting

    EnterpriseDB – elevating PostgreSQL for the enterprise

    Choosing your EDB deployment method

    Introduction to columnar databases with ClickHouse

    What is ClickHouse?

    Choosing the right alternative

    Summary

    Index

    Other Books You May Enjoy

    Preface

    The goal of Database Design and Modeling with PostgreSQL and MySQL is to provide you with a comprehensive understanding of database design and modeling principles, techniques, and best practices using the two most popular open source relational databases, MySQL and PostgreSQL. This book covers essential topics such as understanding databases, designing databases, advanced database design, scaling databases, using MySQL and PostgreSQL with web applications, and the future of databases.

    By the end of the book, you should have a solid understanding of the fundamental concepts of database design and modeling, as well as some advanced topics.

    This book’s ultimate goal is to help you create well-structured, efficient, and reliable databases using the latest technologies and best practices in the field. It aims to equip you with the skills and knowledge you need to become proficient in database design and modeling for MySQL and PostgreSQL by providing practical guidance, real-world examples, and hands-on exercises.

    Who this book is for

    For a beginner who’s new to database design and modeling, this book is an excellent resource for learning the field’s fundamental principles, techniques, and best practices. This book is aimed at software developers, database administrators, and data analysts who want to improve their knowledge and skills in designing, building, and managing databases; it is suitable for beginners who have no or little experience in the field.

    For experienced database developers, this book is a valuable tool for updating their knowledge, learning new techniques, and becoming familiar with the latest technologies and best practices in the field. The advanced topics are suitable for experienced database developers who want to stay up to date with the latest developments in the field.

    What this book covers

    Chapter 1, SQL and NoSQL Databases: Characteristics, Design, and Trade-Offs, provides an overview of SQL and NoSQL databases, discussing their unique characteristics, design principles, and the trade-offs involved in choosing one over the other.

    Chapter 2, Building a Strong Foundation for Database Design, covers the essential principles and best practices for designing robust databases. This chapter covers key concepts such as normalization, data modeling, and the importance of understanding business requirements when creating efficient and scalable database structures.

    Chapter 3, Getting Your Hands Dirty with PostgreSQL and MySQL, focuses on the practical aspects of working with two of the most popular open source relational databases: PostgreSQL and MySQL. You will gain hands-on experience with installation, configuration, and basic operations, laying the groundwork for more advanced database tasks.

    Chapter 4, Mastering the Building Blocks of Database Design and Modeling, explores the core components of database design and modeling in depth. The topics covered in this chapter include entity-relationship modeling, schema design, indexing strategies, and the use of constraints to ensure data integrity and performance.

    Chapter 5, Advanced Techniques for Advanced Databases, goes into advanced database techniques such as query optimization, stored procedures, triggers, and views. You will learn how to use these features to enhance the functionality and efficiency of your databases.

    Chapter 6, Understanding Database Scalability, covers scalability, which is a critical aspect of modern database design. This chapter examines different strategies for scaling databases, including vertical and horizontal scaling, sharding, replication, and the use of distributed databases to handle large-scale data processing.

    Chapter 7, Best Practices for Building and Maintaining Your Database, provides a comprehensive guide to the best practices for database maintenance, including backup and recovery strategies, security measures, performance monitoring, and routine maintenance tasks to ensure the long-term health and reliability of databases.

    Chapter 8, The Future of Databases and Their Designs, looks ahead and explores emerging trends and technologies in the database field. The topics in this chapter include the impact of artificial intelligence and machine learning on database management, the rise of cloud-based databases, and predictions for the future evolution of database design and technology.

    To get the most out of this book

    Before diving into this book, it is helpful to have a basic understanding of database concepts and some familiarity with SQL. While no prior experience with PostgreSQL or MySQL is necessary, a general knowledge of relational databases will allow you to grasp the more advanced topics. Additionally, having basic proficiency in any programming language will be advantageous as we explore practical examples and database interactions.

    If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

    Download the example code files

    You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Database-Design-and-Modeling-with-PostgreSQL-and-MySQL. If there’s an update to the code, it will be updated in the GitHub repository.

    We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

    Conventions used

    There are a number of text conventions used throughout this book.

    Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: We will now introduce a unique identifier (though, in some cases, the combination of StudentID and Course could itself act as a composite primary key).

    A block of code is set as follows:

    CREATE SCHEMA IF NOT EXISTS `DesignAndModeling`

    DEFAULT CHARACTER SET utf8mb4;

    CREATE TABLE `StudentCourses` (

        `StudentID` int unsigned NOT NULL AUTO_INCREMENT,

        `StudentNAME` varchar(45) DEFAULT NULL,

        `Courses` varchar(45) DEFAULT NULL,

        PRIMARY KEY (`StudentID`)

    ) ENGINE=InnoDB

    DEFAULT CHARSET=utf8mb4

    COLLATE=utf8mb4_0900_ai_ci;

    Any command-line input or output is written as follows:

    $ mysql –u root –p

    mysql> \s

    Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: Redis is a popular in-memory key-value store known for its exceptional speed and versatility.

    Tips or important notes

    Appear like this.

    Get in touch

    Feedback from our readers is always welcome.

    General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.

    Errata: Although we have taken

    Enjoying the preview?
    Page 1 of 1