O'reilly Mysql and Msql.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
MySQL and mSQL Randy Jay Yarger, George Reese, and Tim King Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo MySQL and mSQL by Randy Jay Yarger, George Reese, and Tim King Copyright © 1999 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472. Editor: Andy Oram Production Editor: Jeffrey Liggett Editorial and Production Services: Electro-Publishing Printing History: July 1999: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly & Associates, Inc. Java™ and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. O'Reilly & Associates, Inc. is independent of Sun Microsystems. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. The association between the image of kingfishers and the topic of MySQL and mSQL is a trademark of O'Reilly & Associates, Inc. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book is printed on acid-free paper with 85% recycled content, 15% post-consumer waste. O'Reilly & Associates is committed to using paper with the highest recycled content available consistent with high quality. ISBN: 1-56592-434-7 [9/99] Preface ix I. Getting Started with MySQL and mSQL 1 1. Introduction to Relational Databases 3 What is a Database? 4 What is a Relational Database? 5 Applications and Databases 6 MySQL and mSQL 7 2. Database Design 13 Database Design 13 Normalization 16 A Logical Data Modeling Methodology 25 Physical Database Design 26 3. Installation 30 MySQL 30 mSQL 37 4. MySQL 39 Design 39 Installing MySQL 41 Running MySQL 41 Database Administration 42 MySQL Utilities 58 Performance Tuning 62 5. mSQL 68 Design 68 mSQL Versions 70 Installing mSQL 72 Running mSQL 73 Database Administration 77 mSQL Utilities 84 6. SQL According to MySQL and mSQL 90 SQL Basics 90 Creating and Dropping Tables 93 SQL Datatypes 94 Indices 100 Sequences and Auto-Incrementing 101 Managing Data 103 Queries 105 Extended Functionality 109 7. Other Mid-Range Database Engines 113 What is "Free"? 113 What MySQL and mSQL Lack 114 PostgreSQL 117 GNU SQL 118 Beagle 119 Making Comparisons 119 II. Database Programming 121 8. Database Application Architectures 123 The Client/Server Architecture 123 Data Processing 124 Data Processing 124 Object/Relational Modeling 125 The Three-tier Architecture 127 9. CGI Programming 130 What is CGI? 130 HTML Forms 131 The CGI Specification 136 Important Considerations for CGI Scripts 143 CGI and Databases 152 10. Perl 154 DBI 154 An Example DBI Application 161 Msql.pm 165 MysqlPerl 174 11. Python 181 Basic Connectivity 181 Dynamic Connectivity 184 12. PHP and Other Support for Database-driven HTML 187 Alternatives for Dynamic Content on the Web 187 W3-mSQL 189 PHP 193 Embedded Perl 195 13. C and C++ 197 The Two APIs 197 Object-oriented Database Access in C++ 203 14. Java and JDBC 216 What is JDBC? 216 Simple Database Access 221 Dynamic Database Access 224 A Guest Book Servlet 227 III. Reference 229 15. SQL Reference 231 MySQL SQL 231 mSQL SQL 269 16. MySQL and mSQL System Variables 275 MySQL System Variables 275 mSQL System Variables 280 17. MySQL and mSQL Programs and Utilities MySQL Utilities 284 mSQL Utilities 299 18. PHP and Lite Reference 303 PHP 303 Lite 334 19. C Reference 345 MySQL C API 345 mSQL C API 361 20. Python Reference 368 Module: MySQL 368 Module: mSQL 372 21. Perl Reference 375 Installation 375 DBI.pm API 377 Msql.pm API 396 Mysql.pm API 412 22. JDBC Reference 416 PREFACE In the world of computing, the 1990s may rightly be called the decade of Open Source software. From Linux to Perl, from palmtop to mainframe, the Open Source movement has left a mark in practically every niche of technology. This impact is especially strong in the commercially neglected world of mid-range server applications commonly needed by nonprofit organizations and small businesses. The idea of mid-range servers was fairly rare in the first few decades of the computer age. Computers were expensive items used by large institutions such as banks and universities. Enormous time-sharing servers provided the computing power for entire companies. Much of the software running on these systems was as monolithic as the servers themselves. After all, because only one computer was serving several departments—if not the whole organization—that computer had to fulfill everyone's needs. At the other end of the spectrum was the personal computer. With the PC revolution, you could find one computer for every household instead of one computer for an entire company. While these computers were easily powerful enough to satisfy the needs of a single user, a wide gulf still existed between the capabilities—and the costs—of personal computing and corporate computing. The area where this gulf was most apparent was in data management. Database applications for large mainframe servers included every feature possible. Because of the multipurpose nature of this software, if any odd feature was needed by a single user, it was included. Database applications that satisfied those data management needs of the individual user emerged. However, where mainframe databases were too massive for mid-range needs, personal databases were too narrow. In the first half of the 1990s, the "lowly" personal computer had advanced to the point where it was actually more powerful than the mainframe computers of yesteryear. While hardware was no longer a barrier to mid-range computing, the lack of affordable software was. To meet the data storage needs of a nonprofit organization or small business, you needed an affordable server operating system and an affordable database management system. The introduction of cheap and powerful server operating systems like FreeBSD and Linux helped solve the operating system side of that equation. MySQL and mSQL are two solutions that solve the database management side of the equation. They are powerful and flexible while at the same time lightweight and efficient. MySQL, in particular, packs a large feature set into a very small and fast engine. While neither database engine has anywhere near the full feature set of expensive corporate databases, they easily have enough of a feature set to meet the needs of mid-range database management. Audience This book is primarily for two classes of readers. The most obvious is the reader interested in using MySQL and mSQL from either a database administration perspective or from a database programmer perspective. In addition, anyone who wants to learn about relational database administration and programming without paying out the nose for a license from one of the big guys will find MySQL or mSQL an excellent starting point. If MySQL or mSQL is your starting point, then this book is your guide. From a database administrator's perspective, we cover the basic methods of creating and managing databases and tables in MySQL and mSQL. We go beyond the simple and provide performance tuning and troubleshooting tips to help you make sure your MySQL and mSQL applications are running their best. Finally, all of the tools that come with MySQL and mSQL are covered in detail. We assume no prior knowledge of SQL or relational databases. Database programmers will find that we have covered all of the major programming interfaces from the most popular client/server and web programming languages. When we cover the interface for a particular language, we assume that the reader has a basic grasp of the language in question. For example, in the Java™ chapter, we assume that the reader knows how to write basic Java applications and now wants to learn how to make those Java applications talk to a MySQL or mSQL database. The immense popularity of MySQL and mSQL on the Web has made it natural to provide a focus on CGI programming with MySQL and mSQL. Web developers should therefore find this book useful in describing how to drive their web sites with a MySQL or mSQL database. For these chapters, very little CGI knowledge is needed, but we still assume that the reader is familiar with the basics of the programming language in question. Purpose At first glance, the purpose of this book seems obvious: MySQL and mSQL are two of the most popular applications offering public source code. They offer the practical advantages of Open Source software even through their licenses are a bit too restrictive for the Open Source mark. For anyone who has spent a significant amount of time learning MySQL or mSQL, the answer is a little more complex. One of the biggest complaints about Open Source projects is almost always the lack of comprehensive and comprehensible documentation. In the case of MySQL and mSQL, however, lack of online documentation is rarely a problem. MySQL has a wonderfully complete and free online reference manual available from the web site at http://www.mysql.com. This manual covers the full MySQL SQL syntax, installation, and its C API, as well as database administration and performance tuning. Similarly, mSQL has a good, if less comprehensive, online manual at http://www.hughes.com.au. To make matters even more complex, MySQL and mSQL are both moving targets because of rapid development.