Praise for MySQL Cookbook, 4th Edition
Whether you are struggling to simply log in to your MySQL server, or trying to architect the right replication topology, MySQL Cookbook has your back. Sveta and Alkin share their decades of experience helping hundreds of MySQL users complete their everyday tasks.
Henrik Ingo, Chief of Staff, Engineering, DataStax
MySQL gets better with each release. This update by long-time MySQL experts makes it easier to keep up with the improvements. A valuable resource whether you are a beginner or an experienced user like me.
Mark Callaghan, ProUnlimited, long-time MySQL contributor
A great learning resource for all MySQL users. Sveta and Alkin have curated a comprehensive list of solutions to problems spanning all aspects of MySQL.
Shlomi Noach, database engineer, PlanetScale
I recommend this comprehensive book for all MySQL users. Its examples will help both beginners and advanced users of MySQL, and it features excellent chapters about MySQL Shell and JSON.
Frederic Descamps, MySQL Community Manager, Oracle
Sveta and Alkin walk you through practical examples of things you will need to do as a MySQL developer, operator, or DBA. This wealth of knowledge, distilled and condensed, is a next level how to guide for mastering MySQL.
Matt Lord, Vitess Maintainer, PlanetScale
MySQL Cookbook
by Sveta Smirnova and Alkin Tezuysal
Copyright 2022 Sveta Smirnova and Alkin Tezuysal. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Andy Kwan
- Development Editors: Amelia Blevins and Jeff Bleiel
- Production Editor: Ashley Stussy
- Copyeditor: Piper Editorial Consulting, LLC
- Proofreader: Liz Wheeler
- Indexer: Sue Klefstad
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- October 2002: First Edition
- November 2006: Second Edition
- August 2014: Third Edition
- August 2022: Fourth Edition
Revision History for the Fourth Edition
- 2022-08-02: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492093169 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. MySQL Cookbook, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the authors, and do not represent the publishers views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-09316-9
[LSI]
Foreword
MySQL is one of the most pragmatic relational databases that I have come across. It is fast, reliable, and easy to use. You can start off fairly easily with a very small footprint. Yet, it can be deployed at a massive scale. Some of the largest companies in the world run on MySQL. What makes MySQL attractive is that it makes the bread-and-butter features of a relational database work extremely well: these are indexes, joins and transactions. To top it off, it provides all the benefits of being open source.
The recent trend to migrate software to cloud providers has brought a unique momentum to MySQL. This is because some of the complexities associated with managing a relational database are being taken on by the cloud providers. This lets you enjoy all that is good about MySQL without incurring the overhead of having to manage it.
I have known Alkin and Sveta for many years. We have been meeting at various conferences, I have attended many of their sessions, and we have spent time together at many social events. Alkin was also a colleague at PlanetScale where he made substantial contributions to the Vitess project. What is best about Alkin and Sveta? They are both genuine individuals who like to work hard and want to help the community. Also, they have a vast amount of experience and possess a deep knowledge of how to get the best out of MySQL.
You can see their diligence in this book. They spend time making sure that every subject is well introduced. Each chapter reads like a story. But then, at the end, you have learned something extremely valuable. If you are getting started with MySQL, this is a great book. If you come back to this after using MySQL for a bit, youll discover hidden gems that youve missed before. And finally, the Problem-Solution-Discussion format allows you to quickly find a solution if you run into a specific problem while using MySQL.
If you intend to learn and use MySQL, this is the book for you.
Sugu Sougoumarane
CTO, PlanetScale
Co-creator, Vitess
Preface
The MySQL database management system is popular for many reasons. Its fast, and its easy to set up, use, and administer. It runs under many varieties of Unix and Windows, and MySQL-based programs can be written in many languages.
MySQLs popularity raises the need to address questions its users have about how to solve specific problems. That is the purpose of MySQL Cookbook: to serve as a handy resource to which you can turn for quick solutions or techniques for attacking particular types of questions that come up when you use MySQL. Naturally, because its a cookbook, it contains recipes: straightforward instructions you can follow, rather than how to develop your own code from scratch. Its written using a problem-and-solution format designed to be extremely practical and to make the contents easy to read and assimilate. It contains many short sections, each describing how to write a query, apply a technique, or develop a script to solve a problem of limited and specific scope. This book doesnt develop full-fledged, complex applications. Instead, it assists you in developing such applications yourself by helping you get past problems that have you stumped.
For example, a common question is How can I deal with quotes and special characters in data values when Im writing queries? Thats not difficult, but figuring out how to do it is frustrating when youre not sure where to start. This book demonstrates what to do; it shows you where to begin and how to proceed from there. This knowledge will serve you repeatedly because after you see whats involved, youll be able to apply the technique to any kind of data, such as text, images, sound or video clips, news articles, compressed files, or PDF documents. Another common question is Can I access data from multiple tables at the same time? The answer is Yes, and its easy to do because its just a matter of knowing the proper SQL syntax. But its not always clear how until you see examples, which this book gives you. Other techniques that youll learn from this book include how to do the following: