Share E-Book

Database Design and Modeling with PostgreSQL and MySQL Build efficient and scalable databases for modern applications using… (Alkin Tezuysal, Ibrar Ahmed) (z-library.sk, 1lib.sk, z-lib.sk)

Author

,

Rating 3.5 AI initial rating

Log in to rate

Database
Language English

Become well-versed with database modeling and SQL optimization, and gain a deep understanding of transactional systems through practical examples and exercises Key Features Get to grips with fundamental-to-advanced database design and modeling concepts with PostgreSQL and MySQL Explore database integration with web apps, emerging trends, and real-world case studies Leverage practical examples and hands-on exercises to reinforce learning Book Description Database Design and Modeling with PostgreSQL and MySQL will equip you with the knowledge and skills you need to architect, build, and optimize efficient databases using two of the most popular open-source platforms. As you progress through the chapters, you'll gain a deep understanding of data modeling, normalization, and query optimization, supported by hands-on exercises and real-world case studies that will reinforce your learning. You'll explore topics like concurrency control, backup and recovery strategies, and seamless integration with web and mobile applications. These advanced topics will empower you to tackle complex database challenges confidently and effectively. Additionally, you’ll explore emerging trends, such as NoSQL databases and cloud-based solutions, ensuring you're well-versed in the latest developments shaping the database landscape. By embracing these cutting-edge technologies, you'll be prepared to adapt and innovate in today's ever-evolving digital world. By the end of this book, you’ll be able to understand the technologies that exist to design a modern and scalable database for developing web applications using MySQL and PostgreSQL open-source databases. Who this book is for This book is for a wide range of professionals interested in expanding their knowledge and skills in database design and modeling with PostgreSQL and MySQL. This includes software developers, database administrators, data analysts, IT professionals, and students. …

Format PDF
Size 4.5 MB
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
(This page has no text content)
Page 2
Database Design and Modeling with PostgreSQL and MySQL Build efficient and scalable databases for modern applications using open source databases Alkin Tezuysal Ibrar Ahmed
Page 3
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
Page 4
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
Page 5
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”
Page 6
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.
Page 7
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.
Page 8
Preface xiii Part 1: Introduction to Databases 1 SQL and NoSQL Databases: Characteristics, Design, and Trade-Offs 3 Understanding databases and data models 4 Exploring the relational data model (SQL databases) 4 Tables, rows, and columns 5 Normalization 5 Structured Query Language (SQL) 6 ACID transactions 6 Navigating the document data model (NoSQL databases) 7 Data models in NoSQL 7 Types of NoSQL databases 8 Key-value stores 8 Document stores 9 Column-family stores 10 Graph databases 12 Applying the CAP theorem and NoSQL design choices 14 Consistency 14 Availability 14 Partition tolerance 14 Consistency models in NoSQL databases 15 NoSQL design choices and use cases 15 Managing transaction management and concurrency control in NoSQL 16 BASE transactions in NoSQL databases 16 Reasons for the BASE model in NoSQL databases 17 Implications for data integrity and concurrency control 17 Analyzing the advantages and disadvantages of NoSQL databases 18 Advantages of NoSQL databases 18 Disadvantages of NoSQL databases 19 Summary 20 Table of Contents
Page 9
Table of Contentsviii 2 Building a Strong Foundation for Database Design 21 The importance of a solid foundation in database design 22 Key terms and data models 22 Understanding the relational model in detail 27 Identifying entities, attributes, and relationships in database design 29 Creating an ER diagram 31 Advanced database design 32 Normalization – reducing redundancy and improving data integrity 33 Achieving normal forms – 1NF, 2NF, and 3NF 33 Ensuring database validity and integrity 43 Concluding thoughts 46 Summary 47 Part 2: Practical Implementation 3 Getting Your Hands Dirty with PostgreSQL and MySQL 51 Understanding the sample database 52 EDA and preprocessing 52 Database schema 52 MySQL 53 Concluding thoughts 68 Summary 68 Part 3: Core Concepts in Database Design 4 Mastering the Building Blocks of Database Design and Modeling 71 Understanding database objects 72 Understanding data types and constraints 73 Keys and how to use them 80 Database checks and constraints 81 Check constraint 81 Default constraint 81 Not null constraint 81 Checking data quality with constraints 82 No date in the future 82 Value within a specific range 83 How to avoid redundancy 83 Database consistency and beyond 84 Transactions – ensuring data integrity 84
Page 10
Table of Contents ix Concurrency control – managing multiple users 86 PostgreSQL 87 MySQL 87 Summary 88 Part 4: Advanced Database Techniques 5 Advanced Techniques for Advanced Databases 91 Creating custom views of your data 92 Understanding the purpose of views 92 The advantages of using views in database management 92 Indexing – how to find data faster 97 Understanding indexing 98 Types of indexes 98 How indexing works 100 PostgreSQL indexes 100 MySQL indexes 101 Stored procedures – reusable code for your database 103 UDFs 103 The essence of UDFs 104 Efficiency through reusability – the role of UDFs in code optimization 104 UDFs and performance optimization in database operations 105 Using UDFs to enrich SQL queries for expressive interactions 105 Practical insights into UDFs in MySQL and PostgreSQL 106 Understanding CTEs 106 The role of CTEs in code modularity 107 Components and integration of CTEs 107 Guidelines for efficient CTE usage 108 Advanced strategies with CTEs 108 Case studies in action – real-world examples of CTE transformations 109 Summary 110 6 Understanding Database Scalability 111 Introducing database scaling 112 Challenges in database scaling 112 Primary methods of database scaling 112 Vitess – a horizontal scaling solution for MySQL 114 Citus – a horizontal scaling solution for PostgreSQL 115 Vertical scaling – enhancing capacity within existing infrastructure 117 InnoDB Cluster for MySQL 120 Sharding and resharding 124 Future trends and emerging challenges in database scaling 125 Serverless databases 127 Summary 132
Page 11
Table of Contentsx Part 5: Best Practices and Future Trends 7 Best Practices for Building and Maintaining Your Database 135 Designing for performance – optimizing database efficiency 136 Schema design 136 Query optimization 139 Advanced query optimization techniques 141 Using subqueries and JOIN clauses wisely 141 Utilizing temporary tables 142 Optimizing aggregates and GROUP BY clauses 142 Using an index to assist GROUP BY clauses 142 Iterative process and analysis 143 Understanding database scaling 143 Vertical scaling 143 Horizontal scaling 145 Replication in PostgreSQL 145 MySQL replication 150 Ensuring database security 153 Access control 153 Implementing robust authentication mechanisms 153 Permission models 153 PostgreSQL permission model 153 Roles and privileges 154 Example setup in PostgreSQL 154 MySQL permission model 154 Privileges and levels 154 Example setup in MySQL 155 Management and flexibility 156 Exploring data encryption 156 Protecting data at rest 156 Protecting data in transit 157 Monitoring and auditing 158 Data quality and governance – maintaining high standards 158 Learning about data cleaning 158 Data governance frameworks 159 Compliance 159 Collaboration and documentation 160 Effective communication 160 Documentation practices 160 Advanced topics in database management 160 Backup and recovery 161 Tools for monitoring PostgreSQL 161 MySQL performance monitoring tools 162 Strategies for regular performance analysis 163 Summary 164
Page 12
Table of Contents xi 8 The Future of Databases and Their Designs 165 Understanding vectorized search 166 MySQL enhancements and innovations 167 MySQL HeatWave 168 Prospects of use cases of HeatWave 169 TiDB as a MySQL drop-in replacement for distributed systems 170 PostgreSQL – expanding horizons 172 pgEdge – fully distributed PostgreSQL optimized for the network edge 173 Multi-master replication in pgEdge 174 Simplified cluster management 175 Enhanced backup and recovery 175 Advanced monitoring and alerting 176 EnterpriseDB – elevating PostgreSQL for the enterprise 176 Choosing your EDB deployment method 177 Introduction to columnar databases with ClickHouse 185 What is ClickHouse? 186 Choosing the right alternative 187 Summary 188 Index 189 Other Books You May Enjoy 200
Page 13
(This page has no text content)
Page 14
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.
Page 15
Prefacexiv 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. Software/hardware covered in the book Operating system requirements MySQL 8.X or higher Windows, macOS, or Linux PostgreSQL 15.X or higher 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.
Page 16
Preface xv 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.
Page 17
Prefacexvi 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 customercare@ packtpub.com and mention the book title in the subject of your message. Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form. Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material. If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Page 18
Preface xvii Share Your Thoughts Once you’ve read Database Design and Modeling with PostgreSQL and MySQL, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback. Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Page 19
Prefacexviii Download a free PDF copy of this book Thanks for purchasing this book! Do you like to read on the go but are unable to carry your print books everywhere? Is your eBook purchase not compatible with the device of your choice? Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost. Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application. The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily Follow these simple steps to get the benefits: 1. Scan the QR code or visit the link below https://packt.link/free-ebook/9781803233475 2. Submit your proof of purchase 3. That’s it! We’ll send your free PDF and other benefits to your email directly
Page 20
Part 1: Introduction to Databases In this part, you will get an overview of the fundamental differences between SQL and NoSQL databases. You will learn about their unique characteristics, design principles, and the trade-offs involved in choosing one over the other. Additionally, you will understand the essential principles and best practices for designing robust databases, covering key concepts such as normalization, data modeling, and understanding business requirements. This section contains the following chapters: • Chapter 1, SQL and NoSQL Databases: Characteristics, Design, and Trade-Offs • Chapter 2, Building a Strong Foundation for Database Design
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List