Sam's teach yourself MySQL in 21 days

308 550 1
Sam's teach yourself MySQL in 21 days

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Sam's teach yourself MySQL in 21 days

Release Team[oR] 2001 [x] Database - 2 - Sam's Teach Yourself MySQL in 21 Days by Mark Maslakowski ISBN: 0672319144 Sams © 2000, 532 pages A complete course in using one of the most-popular open source databases. Table of Contents Colleague Comments Back Cover Synopsis by Stan Miastkowski For those who need a full-powered relational DBMS without the considerable expense of well-known commercial products (such as Microsoft SQL Server), MySQL has quickly become a favorite. This open source database is available for virtually all platforms, including the various incarnations of Windows. But like many open source products, its documentation is sparse. Enter Teach Yourself MySQL in 21 Days, a book that solves the documentation dearth problem with an effective and accessible step-by-step course in the DBMS. No prior SQL experience is assumed so even beginners will benefit from this book. Table of Contents Sams Teach Yourself MySQL in 21 Days - 3 Introduction - 5 Week 1 At a Glance Day 1 - What Is MySQL? - 9 Day 2 - Getting Started - 13 Day 3 - Designing Your First Database - 21 Day 4 - Creating Your First Database - 32 Day 5 - Making Your Data Normal - 38 Day 6 - Adding Tables, Columns, and Indexes to Your Database - 43 Day 7 - MySQL Data Types - 50 Week 2 At a Glance Day 8 - Populating the Database - 58 Day 9 - Querying the Database - 71 Day 10 - Letting MySQL Do the Work—Intrinsic Functions - 85 Day 11 - MySQL Table Locks and Assorted Keys - 93 Day 12 - How to Get the Data—Database Interfaces - 107 Day 13 - How to Use MyODBC - 111 Day 14 - The Perl Database Interface - 139 Week 3 At a Glance Day 15 - MySQL and PHP - 156 - 3 - Day 16 - MySQL and Time - 171 Day 17 - MySQL Database Security - 185 Day 18 - How MySQL Compares - 195 Day 19 - Administrating MySQL - 204 Day 20 - Optimizing MySQL - 219 Day 21 - Putting It All Together - 225 Appendix A - ASQL Syntax and Commands - 271 Appendix B - Current MySQL Functions - 278 Appendix C - Answers to Exercises - 298 List of Figures List of Tables List of Listings List of Sidebars Back Cover In just 21 days, you'll have all the skills you need to get up and running efficiently. With this complete tutorial, you'll master the basics and then move on to the more advanced features and concepts. • Understand the fundamentals and master all of the key features of MySQL • Learn how to effectively use the latest tools and features of MySQL by following practical, real-world examples • Get expert tips from a leading authority on implementing MySQL in the corporate environment This book is designed for the way you learn. Go through the lessons day by day or just chose those lessons that interest you the most. • Build robust databases from the ground up • Access data easily and efficiently using database interfaces • Optimize MySQL and keep your databases secure • Design efficient MySQL databases using normalization • Learn how to define business rules and objects • Protect access with Table Locks and Keys • Make the best use of MySQL's intrinsic fuctions such as numerical operations, string manipulations, and logical conditions. About the Author Mark Maslakowski is a consultant working for Thor Systems Inc., a company specializing in systems integration. As a Senior Solution Developer. Mark has designed and created computer-based solutions for several national and international corporations on a diverse set of platforms. Originally a Windows developer who used Visual Basic and Visual C++, Mark has joined the open source revolutions and prefers using programming languages such as Perl and Tcl/Tk. Sams Teach Yourself MySQL in 21 Days Mark Maslakowski Tony Butcher Copyright © 2000 by Sams - 4 - All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-31914-4 Library of Congress Catalog Card Number: 99-69680 Printed in the United States of America First Printing: June, 2000 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an "as is" basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the CD or programs accompanying it. Associate Publisher Michael Stephens Executive Editor Rosemarie Graham Acquisitions Editor Shelley Johnston Development Editor Clint McCarty Gus A. Miklos Managing Editor Matt Purcell Project Editor Andy Beaster Copy Editor Pat Kinyon Indexer Sandy Henselmeier Proofreader Matt Wynalda Technical Editor Pierre Boutquin Team Coordinator Pamalee Nelson Software Development Specialist Jason Haines Interior Design Gary Adair Cover Design Aren Howell Copy Writer Eric Borgert Production Brandon Allen Cheryl Lynch About the Authors Mark Maslakowski is a consultant working for Thor Systems Inc., a company specializing in systems integration. As Senior Solutions Developer, Mark is responsible for developing solutions that fit clients' needs, no matter what the platform. He has been involved in developing large multi-tier applications for - 5 - several major corporations. Mark started his career in the U.S. Army and has been on the move since. He has filled of many roles, including Database Administrator, Senior Developer, and Senior Systems Analyst. Mark can be reached at markm@thorinc.com . Tony Butcher is a consultant specializing in the design of interactive and database-driven Web sites. As Technology Director for Tribal Internet in the UK, he creates solutions based on MySQL in combination with Perl and PHP3. He has designed Web sites as front-ends to vast data mines searchable by sophisticated user queries and has created dynamic virtual communities of thousands of people with loyalty systems and personalized publishing of information. Although he has had a varied career, he has implemented databases in almost every job he has ever had. "We shouldn't be worried about information overload," he might say, "What matters is how we phrase our questions." Dedication I would like to dedicate this book in loving memory of my Father who left this world early but gave those he touched a lifetime of memories and teachings. —Mark Acknowledgments First and foremost I'd like to thank Ken Robertson for giving me the time, the insights, and the opportunity to make this book a reality. I'd also like to thank Carla Maslakowski. Without her push and direction, I would not have had the career I've enjoyed. Also, I'd like to thank David Smith of Cornerstone Information Systems, for giving me a chance when no else would. To all my friends at the club for helping me blow off steam when I needed to the most. I can't forget my family for being there when I needed them most and for putting up with my bad habits. I'd also like to thank the team at Sams Publishing, especially Shelley Johnston for making this experience a pleasurable one. —Mark Maslakowski Tell Us What You Think! As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. As an Associate Publisher for Sams, I welcome your comments. You can fax, email, or write me directly to let me know what you did or didn't like about this book—as well as what we can do to make our books stronger. Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message. When you write, please be sure to include this book's title and author as well as your name and phone or fax number. I will carefully review your comments and share them with the authors and editors who worked on the book. Fax: 317.581.4770 Email: opsys_sams@macmillanusa.com Mail: Michael Stephens Sams Publishing 201 West 103rd Street Indianapolis, IN 46290 USA Introduction Since before the dawn of the computer age, people have been using databases. Before computers, a database may have been a Rolodex containing phone numbers of the important people you knew, or it was a filing cabinet that contained all the personnel records for the company. Today, databases are computer-based and are found virtually everywhere. From desktop databases of your record collection to Web-enabled databases that run large corporations, - 6 - databases come in all shapes and sizes. Because of this fact, the database industry has grown as fast and as large as the rest of the computer industry. Until recently, most high-powered databases cost an arm and a leg. They could provide all the tools and functionality to run a business but at a very high price. So most companies would use a database that was cheaper and sacrifice functionality. Additionally, the Internet has spawned a new need for databases that can be accessed via the Web. This need has led software manufacturers to create products that can take advantage of this technology. Again, price plays a large role. These products are generally very expensive and very platform-dependant, so not all Internet Service Providers (ISPs) or small companies can take advantage of this technology. Enter the Linux and Open Source revolution. This cutting-edge idea—to have an operating system and the source code available free of charge—changed the way the industry looked at how it did business. Now that the revolution has finally taken a foothold, people are flocking in masses to see how they can use this newly affordable solution. MySQL is part of that solution. MySQL was developed by TcX in 1996. They created it because they needed a relational database that could handle large amounts of data on relatively cheap hardware. Nothing out there could provide what they needed, so they created it themselves. MySQL is the fastest relational database on the market. It outperforms all the leading databases in almost every category. It has almost all the functionality the leading databases have, but it does not carry the hefty price tag that its competitors do. This may seem like a lot of hype and marketing talk, but, after a little time working with MySQL, you will agree. If MySQL is so good, why hasn't it already caught the attention of the industry? The answer is that until 1999, Linux and the Open Source movement were practically unknown. MySQL runs primarily on UNIX-based systems—though there are ports for almost every platform on the market. Until the Open Source movement and the availability of UNIX-based operating systems at affordable prices, no one really looked at MySQL as a contender. Because of the recent success of Linux, MySQL has grown in popularity. Unfortunately, there is not much out there in the form of documentation. That is where Sams Teach Yourself MySQL in 21 Days comes in. You'll be introduced to the various components of MySQL, such as installation, administration, and interfacing. By the time you are finished, you will be well acquainted with these topics as well as others. You will understand why MySQL is one of the best RDBMS available to date. How This Book Is Organized This book covers the MySQL relational database management system in 21 days, broken into three separate weeks. Each week covers a different area of MySQL and builds on information learned on previous days. In the first week , you will learn some of the basics of MySQL components:  Day 1, "What is MySQL," is the introduction to MySQL—what it is and how it compares to other RDBMs in its class. You will learn about the various components of a database awill be introduced to relational databases.  On Day 2, "Getting Started," you will learn how to install MySQL on a Linux platform as well as a Windows platform.  Day 3, "Designing Your First Database," covers the basics of how to design a database. You will design the sample database that will be used throughout the rest of the book as an example.  On Day 4, "Creating Your First Database," you will create your first MySQL database. You will learn the commands that accomplish this as well as some neat MySQL utilities.  Day 5, "Making Your Data Normal," covers the topic of normalization—a very important subject when dealing with relational databases.  On Day 6, "Adding Tables, Columns, and Indexes to Your Database," you'll beef up your database by learning how to add tables, columns, and indexes, which give structure to your design.  Day 7, "MySQL Data Types," deals with the various data types that MySQL uses. - 7 - Week 2 is dedicated to teaching you how to work with your database. You'll cover the various ways to manipulate the data stored inside your MySQL database. You'll learn about MySQL's intrinsic functions and about interfaces, including the popular Perl DBI/DBD interface:  On Day 8, "Populating the Database," you'll learn the tools and tricks you can use to populate your database.  On Day 9, "Querying the Database," an SQL primer is given. This basic guide will give you the foundation necessary to manipulate your data.  Day 10, "Letting MySQL Do the Work—Intrinsic Functions," covers the functions that are available in MySQL.  Day 11, "MySQL Table Locks and Assorted Keys," introduces you to locks and keys. The uses and reasons for these features will be covered on this day.  Day 12, "How to Get to the Data—Database Interfaces," starts the topic of interfaces and the various APIs that are available in MySQL. You'll see how the various interfaces share common functions.  Day 13, "How to Use MyODBC," covers the ODBC driver interface. On this day we'll explore the techniques needed to access a MySQL database using this technology.  Day 14, "The Perl Database Interface," covers the Perl DBI for MySQL in great detail. You'll build on the previous day's lesson and create your own Perl program to access data via the Internet. Week 3 introduces some of the more advanced techniques of administrating a MySQL database. You'll cover how to administrate a MySQL database server, including security and optimization techniques. At the end of this week, you'll finish things up by building a Web site using MySQL to build Web pages dynamically and processing user input:  On Day 15, "MySQL and PHP," you'll learn how to interface MySQL with this hot new technology.  On Day 16, "MySQL and Time," covers how dates are used in MySQL. You'll learn about the various functions that are available to you from MySQL.  On Day 17, "MySQL Database Security," you'll learn about MySQL security. You'll learn how it is implemented within MySQL, as well as how to keep your database secure.  On Day 18, "How MySQL Compares," we'll compare MySQL to other databases. We'll take a look at the features that other databases have that MySQL does not have and discuss various workarounds to implement them.  On Day 19, "Administrating MySQL," covers administration in detail. You'll learn about maintaining logs and searching logs for problems. You'll also learn about backing up and recovering a MySQL database.  On Day 20, "Optimizing MySQL," you'll learn about performance tuning your database engine. You'll learn how to build better queries, as well as some tricks to tweak performance.  On the last day, "Putting It All Together," you'll use what you have learned in previous lessons to build a working application using MySQL. About This Book This book teaches you about the MySQL database management system. You'll learn about a wide range of topics, from creating to interfacing to administrating. By the end of this book, you will be able to install, create, use, and maintain a MySQL database. Along the way, you will learn about database design, as well as how to use a relational database. Who Should Read This Book This is book is for you if any of the following are true:  You have outgrown the database you are currently using and are searching for a good replacement.  You are developing a Web site that needs database access.  You have never worked with a relational database before and want to learn how to use one.  You are moving to a Linux platform and are looking for a proven RDBMS that can support your business. This book will show you the steps, from start to finish, you'll need to know to use MySQL. Every aspect of MySQL is covered. If you have never used a Relational Database Management System (RDBMS) before, you may be wondering if this book is for you. It is. It will walk you through the most difficult situations step by - 8 - step, offering examples and illustrations to help explain and guide you through the toughest aspects of MySQL. If you have used a RDBMS, such as Microsoft SQL Server, Sybase, or Oracle, this book is also for you. It will give you a chance to see one of the lesser-known systems that can hold its own against the big boys. If you have used MySQL in the past or are currently using MySQL, this book is for you too. Inside, you will find shortcuts and explanations that you might not find anywhere else. A lot of the questions that you might have asked yourself are answered in this book. Sams Teach Yourself MySQL in 21 Days assumes that you have no prior experience in database management systems. So take things a little slowly, make sure you understand each lesson completely before you move on. Each chapter builds on previously covered material. You will be able to take what you have learned in one chapter and apply it later. How This Book Is Structured This book is intended to be read and absorbed over the course of three weeks. During each week, you read seven chapters and perform the exercises at the end of each chapter. Conventions Used in this Book Note A Note presents interesting, sometimes technical, pieces of information related to the surrounding discussion. Tip A Tip offers advice or an easier way to do something. Caution A Caution advises you of potential problems and helps you steer clear of disaster. New terms appear in italic in paragraphs where they are defined. At the end of each chapter, you'll find handy Summary and Q&A sections. In addition, you'll find various typographic conventions throughout this book:  Commands, variables, directories, and files appear in text in a special monospaced font.  Commands and such that you type appear in monospaced bold type.  Placeholders in syntax descriptions appear in a monospaced italic typeface. This indicates that you will replace the placeholder with the actual filename, parameter, or other element that it represents. Week 1: At a Glance Day List Day 1: What is MySQL? Day 2: Getting Started Day 3: Designing Your First Database Day 4: Creating Your First Database Day 5: Making Your Data Normal Day 6: Adding Tables, Columns, and Indexes to Your Database Day 7: MySQL Data Types As you prepare for your first week of learning how to use MySQL, you will need a few things: a computer, a Web server and this book. If you don't have the first two things, you can still use this book. However, you'll still need to practice these lessons somewhere. You cannot expect to learn something without trying it firsthand. This book will take you step by step through each aspect of MySQL. This book is set up so that each day ends with an exercise. Take advantage of these exercises; they can further help you on your journey to becoming a full-fledged MySQL Database Administrator. Where You're Going This week covers the basics of MySQL. On Day 1, you'll learn what MySQL is and some of its uses. Day 2 is where you'll learn how to install MySQL for the Windows and Linux platforms. On Day 3 and Day 4, you'll learn about designing a database, and then you'll actually create one. Day 5 covers normalization. Day 6 is where you'll learn how to add columns and indexes to your database. The week ends with day 7's MySQL data types. On this day, you'll learn about the various data types and how they apply to MySQL. - 9 - This is a lot material to cover in a week, but if you take your time and follow the exercises, you'll do all right. Day 1: What Is MySQL? Overview Welcome to Sams Teach Yourself MySQL in 21 Days. Today, you will start your adventure of discovering one of the best relational database management systems on the market today. Today, you will learn the following:  What a relational database is and for what it can be used  What it means to be on the Enterprise level  What client/server programming is all about  Some of MySQL's features Understanding MySQL MySQL, pronounced "my Ess Que El," is an open source, Enterprise-level, multi-threaded, relational database management system. That sounds like a lot of sales or marketing hype, but it truly defines MySQL. You may not be familiar with some of these terms but, by the end of today, you will be. MySQL was developed by a consulting firm in Sweden called TcX. They were in need of a database system that was extremely fast and flexible. Unfortunately (or fortunately, depending on your point of view), they could not find anything on the market that could do what they wanted. So, they created MySQL, which is loosely based on another database management system called mSQL. The product they created is fast, reliable, and extremely flexible. It is used in many places throughout the world. Universities, Internet service providers and nonprofit organizations are the main users of MySQL, mainly because of its price (it is mostly free). Lately, however, it has begun to permeate the business world as a reliable and fast database system. Some examples of commercial use are available on the CD-ROM that accompanies this book. The reason for the growth of MySQL's popularity is the advent of the Open Source Movement in the computer industry. The Open Source Movement, in case you haven't heard about it, is the result of several computer software vendors providing not only a product but the source code as well. This allows consumers to see how their program operates and modify it where they see fit. This, and the popularity of Linux, has given rise the use of open source products in the business world. Because of Linux's skyrocketing popularity, users are looking for products that will run on this platform. MySQL is one of those products. MySQL is often confused with SQL, the structured query language developed by IBM. It is not a form of this language but a database system that uses SQL to manipulate, create, and show data. MySQL is a program that manages databases, much like Microsoft's Excel manages spreadsheets. SQL is a programming language that is used by MySQL to accomplish tasks within a database, just as Excel uses VBA (Visual Basic for Applications) to handle tasks with spreadsheets and workbooks. Other programs that manage databases include Microsoft's SQL Server, Sybase Adaptive Server, and DB2. Now that you know where MySQL came from, look at what it is. To begin with, start with the term database. What is a database? You have probably used one in your lifetime. If you've ever bought anything over the Internet or have a driver's license, you can be assured that you have used one. A database is a series of structured files on a computer that are organized in a highly efficient manner. These files can store tons of information that can be manipulated and called on when needed. A database is organized in the following hierarchical manner, from the top down. You start with a database that contains a number of tables. Each table is made up of a series of columns. Data is stored in rows, and the place where each row intersects a column is known as a field. Figure 1.1 depicts this breakdown. For example, at your favorite online book store there is a database. This database is made up of many tables. Each table contains specific, common data. You would probably see an Authors table or a Books table. These tables are made up of named columns that tell what data is contained in them. When a record is inserted into a table, a row of data has been created. Where a row and a column intersect, a field is created. This how databases are broken down. - 10 - Figure 1.1 The anatomy of a database. MySQL is more than just a database. It is a system that manages databases. It controls who can use them and how they are manipulated. It logs actions and runs continuously in the background. This is different from what you may be used to. Most people think about Microsoft Access or Lotus Approach when they think about databases. These are databases, but they are not management systems. A DBMS can contain many databases. Users connect to the database server and issue requests. The database server queries its databases and returns the requests to the issuers. Databases, such as Approach and Access, are a step down from this type of system. They share their files with multiple users, but there is no interface controlling the connections or answering requests. There are many uses for a DBMS such as MySQL. Uses can range from help desk systems to Web site applications. The important thing to remember is that MySQL is large enough and quick enough to function in almost any situation. Where it finds itself most comfortable is the Enterprise. What Is the Enterprise? The Enterprise I'm referring to is not a starship or a space shuttle. The Enterprise is the area in the business world where many large systems interact with one another to accomplish a common goal. Some applications that are at this level of business include SAP, Microsoft SQL Server, Oracle 8i, and Sybase Adaptive Server. The computer applications that exist at this level of business tend to have certain characteristics. They are usually multiuser in nature—many people can use the same application at the same time. Another characteristic is that they provide some sort of security mechanism. The final characteristic is that applications at this level have to be very flexible. The first characteristic of an Enterprise-level application is that it can be used by more than one person at a time. This is a requirement at this level of business. More than one person may need to have access to business information at a given time. This is critical for the business to function successfully. MySQL meets this requirement. It can have up to 101 simultaneous connections. This doesn't mean that only 101 people can use this application. It means it can have 101 connections going on at the same time—which is a little different. A connection is the time it takes for a user to receive the data that he or she has requested. In the case of MySQL, this is hardly any time at all. Most database systems in the same class as MySQL allow fewer simultaneous connections. Currently, the only DBMS to offer more connections is Microsoft SQL Server. The next characteristic that an Enterprise-level application must have is security. When dealing with mission-critical information, only people with the need to know should be allowed to view it. Security keeps malicious people at bay; without it, disasters can happen. MySQL meets this requirement. The security in MySQL is unparalleled. Access to a MySQL database can be determined from the remote machine that can control which user can view a table. The database can be locked down even further by having the operating system play a role in security as well. Very few databases in the same class as MySQL can compare to the level of security that MySQL provides. One other characteristic of an Enterprise-level application is flexibility. How flexible is the application? Can it change to meet the ever-changing needs of business? How deep can you make those changes? How hard is it to change? MySQL answers these questions very well. It is extremely flexible and easy to use. MySQL can run on almost any platform. If a new CIO wants to change from Windows NT to Linux, fine—MySQL can adapt. MySQL also comes with the source code. If there are any deep-level changes that you need to make, you can edit the source and make these changes yourself. If MySQL is missing a feature that you can't live without, just add it yourself. No other database on the market can offer you that kind of flexibility. MySQL also has several application-level interfaces in a variety of languages. If yours is mainly a Microsoft shop, you can use ODBC to interact with MySQL. If your company is a UNIX shop, you can use C, Perl, or JDBC. There is no end to the flexibility that MySQL has to offer.

Ngày đăng: 25/11/2013, 11:09

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan