Tải bản đầy đủ (.pdf) (776 trang)

mysql and msql

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.46 MB, 776 trang )







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
. 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
.





To make matters even more complex, MySQL and mSQL are both moving targets
b
ecause of rapid development. In the case of MySQL, "moving target" is a euphemism.
Thanks mainly to the efforts of Michael "Monty" Widenius, MySQL is a target moving
about as fast as a freight train. So be prepared, you may find some of the information in

this book either ahead of older versions or behind newer versions.






But wait! Don't put this book back on the shelf just yet. We knew about all of these
issues before we tackled the task of writing this book. One major reason made a book
on this topic not only justified, but essential. The worlds of MySQL and mSQL are not
limited to a couple of database engines. Because of the degree to which they have been
embraced by the Open Source community, MySQL and mSQL also encompass a host
of tools for managing and programming applications for these databases. The purpose
of this book






is therefore to provide a single, definitive guide to these database engines and the world
of APIs and tools used to build end-to-end database solutions. Anyways, a book is
much easier to read in the bathroom or on a plane than online documentation.






Using This Book










We have divided this book into three sections. The first section covers getting started
and managing a MySQL or mSQL database. The second section builds upon that
foundation by demonstrating how you build applications that use your MySQL or
mSQL database. Finally, we provide a full reference section to provide a resource for
quickly looking up any of the APIs or tools we cover in the first two sections.







If you are a member of the audience we described earlier, you fall into one of three
categories:




• MySQL users and administrators






• mSQL users and administrators





• Undecided, but definitely will be using either MySQL or mSQL






We have directed the first section of the book at database administrators. We start at a
high level by addressing the question of what exactly is a database. Perhaps you have
experience with databases; if so, such a discussion is certainly way too basic for you.
You will still want to catch the end of Chapter 1, Introduction to Relational Databases,
for a short introduction to MySQL and mSQL. This discussion is of particular interest
to anyone who has not yet made a decision on which database to use and who wants a
short overview of the two.






Chapter 2, Database Design, may appear at first unimportant. It is, on the contrary, one

of the most important chapters of the book. Proper database design is essential for both
database administrators and programmers if the goal is to build database applications
that will be flexible enough to scale as application needs change. You also need a
proper database design if you want your database to actually perform well.





Chapter 3, Installation, covers installation of both engines.






If you have chosen a particular database engine, you can skip either Chapter 4, MySQL,
or Chapter 5, mSQL. Each chapter is specific to one of the two database engines and is
completely irrelevant to users of the other engine. If, however, you have not yet made a
decision, then these two chapters will be key to your selection process.







We close out the administrative section with a discussion of the variants of SQL
presented by MySQL and mSQL. While programmers may be largely uninterested in
the first section, they will find the need to occasionally refer to this section even if they

are experienced SQL programmers. Anyone who has never before used SQL definitely
needs to read this chapter.







The second section begins with an overview of the client/server programming model.
This discussion is very high level, but it is key to understanding how the database and
the application programming work together to build a solid database application. Of
course, if you are experienced with client/server programming in other environments,
this sort of architectural discussion may be old hat to you. The rest of the section
contains chapters devoted to programming in specific languages or using specific tools.
Of particular interest to web programmers will be the chapters on Perl, Java, Python,
and PHP.

The book closes with a reference section that covers all of the tools and APIs we address
in the book.*





On MySQL and mSQL









We have attempted to provide balanced and full coverage of both MySQL and mSQL
in this book. A single book on both engines makes sense since they are so similar.
While they do not have common code, the similarities are quite intentional. In order to
be consistent, we have chosen always to use the expression ''MySQL and mSQL" or
"MySQL or mSQL" wherever something is true of both database engines. MySQL
comes first only because we arbitrarily decided to make the most popular of the two
appear first. Where MySQL or mSQL appear alone in a sentence without the other, the
sentence is specifically addressing an issue associated with that database engine.
Except for chapters dedicated solely to one engine or the other, you can assume that
MySQL or mSQL appearing alone in a sentence means that the sentence is true only of
the database being mentioned.







Conventions Used in this Book





The following conventions are used in this book:







Constant width
Used for anything that might appear in a program, including keywords, function
names, SQL commands, and variable names. This font is also used for code
examples, output displayed by commands, and system configuration files.






Constant width bold
Used for user input.






Constant width italic
Indicates an element (e.g., a filename or variable) that you supply.









Italic
Used for directory names, filenames, program names, Unix commands, and URLs.
This font is also used to introduce new terms and for emphasis.







* Chapter 22, JDBC Reference, is a revised version of an appendix from O'Reilly's Database
Programming with JDBC and Java by George Reese.








The owl symbol is used to indicate a tip, suggestion, or general
note.











The turkey symbol is used to indicate a warning.





Comments and Questions





Please address comments and questions concerning this book to the publisher:







O'Reilly & Associates
101 Morris Street
Sebastopol, CA 95472
800-998-9938 (in the U.S. or Canada)
707-829-0515 (international or local)

707-829-0104 (FAX)







You can also send us messages electronically. To be put on our mailing list or to
request a catalog, send email to:

To ask technical questions or comment on the book, send email to:













Before submitting a bug report concerning MySQL, please check the online manual
(and particularly the list of problems and common errors) at:
/>
You can search the MySQL mailing list at:




and the MySQL web site at:




Acknowledgments






The authors would first like to thank their editor Andy Oram for both his skill at
making our work look more professional and for the less obvious and likely less
enjoyable task of putting us back on focus when our minds would wander.






We also owe a huge debt of gratitude to those who provided us with a critical look at
the book. Anyone familiar with MySQL knows the name Michael Widenius, the head
of the MySQL project. He and another member of the MySQL team, Paul DuBois, sent
in many valuable comments. Brian Jepson, one of the authors of the Official Guide to
Mini SQL (John Wiley & Sons, Inc.), kindly offered us valuable input on our mSQL
coverage. Finally, Glenn MacGregor and Michael Schecter provided useful comments
on the book overall.







From Randy Yarger








I would like to thank my fiancée, Stacie Sheldon, for the support and love that has kept
me sane. I would also like to thank Andy Oram for encouraging a relatively unknown
author. Finally, but definitely not lastly, I would like to thank the ones that made it
possible for me to be here, my mother, my father, and my creator.




Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×