973 MySQL in a nutshell, 2nd edition

566 80 0
  • Loading ...
    Loading ...
    Loading ...

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

Tài liệu liên quan

Thông tin tài liệu

Ngày đăng: 06/03/2019, 17:00

www.it-ebooks.info www.it-ebooks.info MYSQL IN A NUTSHELL Other resources from O’Reilly Related titles oreilly.com Learning MySQL Learning PHP & MySQL Learning PHP Learning Perl Learning SQL MySQL Cookbook MySQL Pocket Reference MySQL Stored Procedure Programming PHP Cookbook PHP in a Nutshell Programming PHP Programming the Perl DBI SQL Pocket Guide oreilly.com is more than a complete catalog of O’Reilly books You'll also find links to news, events, articles, weblogs, sample chapters, and code examples oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, programming languages, and operating systems Conferences O’Reilly Media, Inc brings diverse innovators together to nurture the ideas that spark revolutionary industries We specialize in documenting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches Visit conferences.oreilly.com for our upcoming events Safari Bookshelf (safari.oreilly.com) is the premier online reference library for programmers and IT professionals Conduct searches across more than 1,000 books Subscribers can zero in on answers to time-critical questions in a matter of seconds Read the books on your Bookshelf from cover to cover or simply flip to the page you need Try it today for free www.it-ebooks.info MYSQL IN A NUTSHELL Second Edition Russell J.T Dyer Beijing • Cambridge • Farnham • Kưln • Paris • Sebastopol • Taipei • Tokyo MySQL in a Nutshell, Second Edition by Russell J.T Dyer Copyright © 2008 Russell J.T Dyer All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://safari.oreilly.com) For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com Editor: Andy Oram Copy Editor: Sarah Schneider Production Editor: Sarah Schneider Proofreader: Genevieve d’Entremont Indexer: Ellen Troutman Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Printing History: April 2008: May 2005: Second Edition First Edition Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc MySQL in a Nutshell, the image of a pied kingfisher, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations uses by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN: 978-0-596-51433-4 [C] 1210161771 To my friend Richard Stringer, for encouraging me in literature, liberalism, and writing, and for helping me to become the person I was meant to be Table of Contents Preface xi Part I Introduction and Tutorials Introduction to MySQL The Value of MySQL The MySQL Package Licensing Mailing Lists Books and Other Publications Installing MySQL Choosing a Distribution Unix Source Distributions Unix Binary Distributions Linux RPM Distributions Macintosh OS X Distributions Novell NetWare Distributions Windows Distributions Postinstallation 3 4 10 11 12 14 15 16 MySQL Basics 19 The mysql Client Creating a Database and Tables Show Me Inserting Data Selecting Data 19 20 23 23 24 vii Ordering, Limiting, and Grouping Analyzing and Manipulating Data Changing Data Deleting Data Searching Data Importing Data in Bulk Command-Line Interface Conclusion 25 27 29 30 31 32 34 35 Part II SQL Statements and Functions Security and User Statements and Functions 39 Statements and Functions SQL Statements in Alphabetical Order Functions in Alphabetical Order Database and Table Schema Statements 57 Statements and Clauses in Alphabetical Order 155 172 Replication Statements and Functions 175 Merits of Replication Replication Process The Replication User Account Configuring the Servers Copying Databases and Starting Replication Starting Replication Backups with Replication SQL Statements and Functions in Alphabetical Order Replication States 113 114 152 Table and Server Administration Statements and Functions 155 Statements and Clauses in Alphabetical Order Functions in Alphabetical Order 57 Data Manipulation Statements and Functions 113 Statements and Functions Grouped by Characteristics Statements and Clauses in Alphabetical Order Functions in Alphabetical Order 40 40 50 175 176 183 185 187 189 190 191 198 Stored Routines Statements 203 Statements in Alphabetical Order viii | Table of Contents 203 mysql_list_tables function, 423, 495 mysql_more_results function, 424 mysql_next_result function, 424 mysql_num_fields function, 424, 496 mysql_num_rows function, 424, 496 mysql_options function, 424 mysql_pconnect function, 496 mysql_ping function, 426, 497 mysql_query function, 426, 497 mysql_real_connection function, 427 mysql_real_escape_string function, 428, 497 mysql_real_query function, 429 mysql_refresh function, 429 mysql_reload function, 429 mysql_result function, 498 mysql_rollback function, 430 mysql_row_seek function, 430 mysql_row_tell function, 430 mysql_select_db function, 431, 498 mysql_setpermission utility, 368 mysql_set_character_set function, 431 mysql_set_charset function, 498 mysql_set_local_infile_default function, 431 mysql_set_local_infile_handler function, 431 mysql_set_server_option function, 432 mysql_shutdown function, 432 mysql_sqlstate function, 432 mysql_ssl_set function, 432 mysql_stat function, 433, 499 mysql_store_result function, 433 mysql_tableinfo utility, 369 mysql_tablename function, 499 mysql_thread_end function, 433 mysql_thread_id function, 499 mysql_thread_init function, 434 mysql_thread_safe function, 434 mysql_unbuffered_query function, 500 mysql_upgrade utility, 370 mysql_use_result function, 434 mysql_waitpid utility, 370 mysql_warning_count function, 435 mysql_zap utility, 371 my_print_defaults utility, 359 534 | Index N NATURAL keyword, using with JOIN statements, 126 NDB storage engine, 113 mysqld server options, 352 neat function, 460 neat_list function, 460 NetWare (Novell), installing MySQL, 14 NetWare Loadable Modules (NLMs), 14 NEXT clause HANDLER READ NEXT statment, 120 HANDLER…READ statements, 121 NLMs (NetWare Loadable Modules), 14 NO ACTION setting, 87 NO keyword, COMMIT statement, 115 NODEGROUP subclause, CREATE TABLE statement, 95 NOT PRESERVE keyword, 208 Novell NetWare, installing MySQL, 14 NOW function, 283 NO_WRITE_TO_BINLOG flag, 42 NO_WRITE_TO_BINLOG option ANALYZE TABLE statement, 156 FLUSH statement, 160 OPTIMIZE TABLE statement, 163 REPAIR TABLE statement, 164 NULL values, changing data in related columns to, 87 NULLIF function, 312 numeric data types, 503–506 O OCT function, 304 OCTET_LENGTH function, 253 OFFSET keyword, LIMIT clause and, 130 OLD_PASSWORD function, 53 ON COMPLETION clause, ALTER EVENT statement, 204 ON DELETE CASCADE clause, 86 ON DELETE clause, 87 ON DUPLICATE KEY UPDATE clause, INSERT statement, 122 ON keyword, JOIN clause, 126 ON SCHEDULE AT clause, ALTER EVENT statement, 204 ON SCHEDULE AT clause, CREATE EVENT statement, 207 ON UPDATE clause, 87 OPEN clause HANDLER statement, 120 OPEN statement, 216 operating systems MySQL distributions, operators, 511–515 arithmetic, 511 bitwise, 513 logical, 513 regular expression, 513 relational, 512 OPTIMIZE PARTITION clause, ALTER TABLE statement, 73 OPTIMIZE TABLE statement, 163 compacting tables after row deletion, 118 OPTIONALLY keyword ENCLOSED BY clause, 131 OR operator bitwise OR, 223 OR REPLACE parameter, CREATE VIEW statement, 98 ORD function, 253 ORDER BY clause, 25 ALTER TABLE statement, 69 GROUP_CONCAT function, 224 SELECT statement, 142 UPDATE statement, 150 OUTER keyword use with LEFT and RIGHT JOINs, 127 outer query, 229 P PACK_KEYS option ALTER TABLE statement, 77 CREATE TABLE statement, 91 parser for FULLTEXT indexes, 62 parsers plugin for an index, 87 parse_dsn function, 460 parse_trace_flag function, 461 parse_trace_flags function, 461 PARTITION BY clause ALTER TABLE statement, 70 CREATE TABLE statement, 93 partitions adding or removing with ALTER TABLE statements, 70 administration clauses, ALTER TABLE statement, 73 CREATE TABLE partition clauses, 93 CREATE TABLE partition definitions, 94 CREATE TABLE subpartition definitions, 96 PASSWORD function, 54 passwords changing on user accounts, 49 decrypting contents of pwd column, 52 encrypting a given string with, 53 encryption method used prior to MySQL 4.1, 53 providing for user account privileges, 44 server access, 83 setting up for user accounts, 41 paths MySQL installation on Windows, 16 PATH environment variable, 19 percent sign (%) wildcard, 31 performance optimization options, mysqld, 336–342 PERIOD_ADD function, 284 PERIOD_DIFF function, 284 Perl, 366, 437–475 attributes for handles, 470 all handles, 470 database handles, 473 dynamic attributes, 475 statement handles, 473 DBI module mailing list, DBI reference, 442–470 interfacing to MySQL, books on, using DBI with MySQL, 437–442 capturing data, 438 connecting to MySQL, 437 disconnecting from MySQL, 439 executing SQL statement, 438 storing results temporarily, 439 PerlDBI module, xiii perror utility, 400 Index | 535 PHP, 477–500 development with MySQL, resources for information, functions used to query and manipulate MySQL, xiii MySQL functions, 479–500 using with MySQL connecting to MySQL, 477 querying MySQL, 478 PI function, 304 ping function, 461 pinging connections, 426, 497 PKG (binary package) files for installing MySQL on Mac OS X, 12 placeholders, 444 in prepared statements, 216 plugins parser for FULLTEXT indexes, 62 SHOW PLUGINS statement, 167 point-in-time recovery methods, 177 PORT option, CREATE SERVER statement, 159 POSITION function, 254 position identification number, master.info file, 180 postinstallation tasks, 16 POW function, 305 POWER function, 305 powers-of-two algorithm, linear, 93 prepare function, 461 PREPARE statement, 216 prepared statements, deleting, 215 prepare_cached function, 462 PRESERVE keyword ON COMPETITION clause, CREATE EVENT, 208 using with ON COMPLETION clause, ALTER EVENT, 204 PREV clause, HANDLER…READ statements, 121 PRIMARY KEY column, 22 primary keys deleting primary key index of a table, 67 ID number of last record inserted, 494 PRIMARY KEY columns, 84 primary_key function, 462 private_attribute_info function, 462 536 | Index privileges replication user account, 184 revoking, 42 PRIVILEGES option, FLUSH statement, 160 privileges or grant tables, 39 generating, 10 setting up for Novell NetWare, 14 PROCEDURE clause, SELECT statement, 143 procedures (see stored procedures) processes listing server processes, 423 terminating based on a given pattern, 371 terminating by given pid, 370 programming languages, C, Perl, and PHP, protocol version for current connection, 418, 493 PURGE MASTER LOGS statement, 193 Python, articles on MySQL APIs, Q QUARTER function, 144, 285 QUERY CACHE option FLUSH statement, 160 RESET statement, 49, 164 QUERY keyword, KILL statement, 161 QUICK keyword DELETE statement, 116 using with REPAIR TABLE statement, 164 quotation marks neat function, 460 OPTIONALLY keyword, ENCLOSED BY clause, 131 QUOTE function, 254 SQL_SHOW_CREATE variable, 107 strings for column name patterns, 99 quote function, 463 quote_identifier function, 463 R RADIANS function, 305 radians, converting to degrees, 300 RAID_TYPE option ALTER TABLE statement, 77 CREATE TABLE statement, 91 RAND function, 305 RANGE subclause, PARTITION BY clause, 94 READ clause, HANDLER statements, 120 READ COMMITTED isolation level, 145 READ LOCAL lock, 162 READ UNCOMMITTED isolation level, 145 REBUILD PARTITION option, ALTER TABLE statement, 73 Redundant Arrays of Independent Disks (RAID), 77 REFERENCES keyword, 86 REFERENCES option, FOREIGN KEY clause of CREATE TABLE, 87 regular expressions, 513 relational operators, 512 relay log files comparison of data to slave server database, 182 controlling processing of, 190 creation of, 180 deleting on slave server, 194 relay-log.index file, 182 relay-log.info file, 182 deleting on slave server, 194 relay.log files, 179 RELEASE SAVEPOINT statement, 132 RELEASE_LOCK function, 173 REMOVE PARTITIONING clause, ALTER TABLE statement, 71 RENAME clause, ALTER TABLE statement, 70 RENAME DATABASE statement, 104 RENAME TABLE statement, 104 RENAME USER statement, 48 REORGANIZE PARTITION clause, ALTER TABLE statement, 71 REPAIR PARTITION clause, ALTER TABLE statement, 73 REPAIR TABLE statement, 163 REPEAT function, 255 REPEATABLE READ isolation level, 145 REPLACE flag, 33 REPLACE function, 210, 255 REPLACE keyword, replacing duplicate rows in new table, 97 REPLACE statement, 30, 133 trigger execution and, 212 replace utility, 400 replication, 175–202 backups of replicated databases on slave server, 190 CHANGE MASTER TO statement, 191 configuring servers, 185 copying databases, 187 alternative methods for, 188 using mysqldump, 187 LOAD DATA FROM MASTER statement (deprecated), 192 master binary log dump thread states, 199 MASTER_POS_WAIT function, 193 mysqld server options, 342–346 process of, 176–183 RESET MASTER statement, 194 RESET SLAVE statement, 194 server states, 198 SET GLOBAL SQL_SLAVE_SKIP_COUN TER statement, 194 SET SQL_LOG_BIN statement, 194 SHOW BINARY LOGS statement, 195 SHOW BINLOG EVENTS statement, 194 SHOW MASTER LOGS statement, 196 SHOW MASTER STATUS statement, 196 SHOW SLAVE HOSTS statement, 196 SHOW SLAVE STATUS statement, 197 slave I/O thread states, 199 slave SQL thread states, 201 START SLAVE statement, 197 starting, 189 STOP SLAVE statement, 198 user account, 183 REQUIRE clause, GRANT statement, 45 Index | 537 RESET MASTER option, FLUSH statement, 161 RESET MASTER statement, 194 RESET SLAVE option, FLUSH statement, 161 RESET SLAVE statement, 194 RESET statement, 49, 164 resolveip utility, 400 resolve_stack_dump utility, 401 resources for further information, RESTORE TABLE statement, 164 RESTRICT keyword, 87 results set, generating with a subquery, 233 REVERSE function, 255 REVOKE statement, 42, 49 privileges, listed, 47 RIGHT function, 255 RIGHT JOIN statement, 127, 222 rollback function, 463 ROLLBACK statement, 134 ROLLBACK TO SAVEPOINT statement, 135 root user, changing password, 17 ROUND function, 306 routines, 203 rows setting average row length in a table, 74 setting minimum and maximum in a table, 77 rows function, 464 ROW_COUNT function, 154 ROW_FORMAT option ALTER TABLE statement, 77 CREATE TABLE statement, 91 RPAD function, 256 RPM distributions, Linux, 11 RTREE index type, 85 RTRIM function, 256 S savepoints identifying with SAVEPOINT statement, 136 releasing, 132 ROLLBACK TO SAVEPOINT statement, 135 538 | Index scalar values returned by subqueries, 230 SCHEMA function, 154 SCHEMA keyword, SHOW CREATE SCHEMA, 107 schemas database and table, statements and clauses related to, 57–112 SHOW SCHEMAS statement, 111 searching data, 31 SECOND function, 286 Secure Hash Algorithm (SHA), 54 security, 39–55 functions for, 50–55 mysqld options, 325–329 user access and privileges, 39 CREATE USER statement, 40 DROP USER statement, 41 GRANT statement, 44 statements and functions, 40 user accounts and privileges FLUSH statement, 42 statements for, 40–50 SEC_TO_TIME function, 285 SELECT statements, 24, 136–144 AS clause, 25 containing a COUNT function, 27 EXPLAIN SELECT statement, 82 FROM clause, 25 GROUP BY clause, 140 IGNORE SELECT, 97 INTO clause, exporting SELECT results, 139 JOIN clause, 25, 125 LIMIT clause, 129, 143 ORDER BY clause, 142 RELEASE_LOCK function, 174 in subqueries, 30, 229 UNION keyword, 148 using with INSERT statement, 124 WHERE clause, 24 selectall_arrayref function, 464 selectall_hashref function, 464 selectcol_array function, 465 selectrow_array function, 466 selectrow_arrayref function, 466 selectrow_hashref function, 467 semicolon (;), ending SQL statements, 206 SEPARATOR keyword, 224 SERIAL data type, 505 SERIALIZABLE isolation level, 145 server identification number (master server), 185 servers, 321 (see also mysqld daemon) changing connection parameters, 59 enabling or disabling an option, 432 global server options, 329–334 mysqld daemon, mysql_get_server_info function, 418 mysql_get_server_version function, 419 replicated, 175 shutting down, 432 states of master and slave replication servers, 198 session variables, 144, 165 SESSION_USER function, 54 SET CHARACTER SET statement, 106 SET clause INSERT statement, 123 LOAD DATA INFILE statement, 131 UPDATE statement, 149, 150 SET data type, 510 SET GLOBAL SQL_SLAVE_SKIP_COUNTER statement, 194 SET NULL clause, CREATE TABLE statement, 87 SET PASSWORD statement, 41, 49 SET SQL_LOG_BIN statement, 194 SET statement, 144, 165 SET statements, 31 SET TRANSACTION statement, 145 set_err function, 467 SHA function, 54 shell prompts, logging into MySQL, 20 shell, entering SQL statements into MySQL, 34 short form, mysqld options, 322 SHOW BINARY LOGS statement, 195 SHOW BINLOG EVENTS statement, 194 SHOW CHARACTER SET statement, 105 SHOW COLLATION statement, 106 SHOW COLUMNS statement, 106 SHOW CREATE DATABASE statement, 107 SHOW CREATE EVENT statement, 217 SHOW CREATE FUNCTION statement, 217 SHOW CREATE PROCEDURE statement, 217 SHOW CREATE TABLE statement, 75, 107 SHOW CREATE VIEW statement, 108 SHOW DATABASES statement, 23, 108 SHOW ENGINE statement, 165 SHOW ENGINES statement, 166 SHOW ERRORS statement, 145 SHOW EVENTS statement, 218 SHOW FULL COLUMNS statement, 84 SHOW FULL TABLES WHERE statement, 99 SHOW FUNCTION CODE statement, 218 SHOW FUNCTION STATUS statement, 219 SHOW GRANTS statement, 50 SHOW INDEXES statement, 60, 109 SHOW MASTER LOGS statement, 178, 196 SHOW MASTER STATUS statement, 196 SHOW OPEN TABLES statement, 167 SHOW PLUGINS statement, 167 SHOW PRIVILEGES statement, 50 SHOW PROCEDURE CODE statement, 219 SHOW PROCEDURE STATUS statement, 219 SHOW PROCESSLIST statement, 168, 198 using with KILL statement, 161 SHOW SCHEMAS statement, 111 SHOW SLAVE HOSTS statement, 196 SHOW SLAVE STATUS statement, 197 SHOW STATUS statement, 168 SHOW TABLE STATUS statement, 64, 111, 170 SHOW TABLES statement, 23, 112 SHOW TRIGGERS statement, 220 SHOW VARIABLES LIKE statement Index | 539 server query caching, determining, 138 SHOW VARIABLES statement, 171 viewing default character set, 106 SHOW WARNINGS statement, 146 SIGN function, 306 SIN function, 306 single field subqueries, 230 SLAVE option FLUSH statement, deprecated, 161 RESET statement, 49, 164 slave servers, 176 changing settings for master server and replication, 191 configuration file, 185 displaying information on slave thread, 197 displaying list for master server, 196 I/O thread states, 199 separation of change entries and their application, 179 SQL thread states, 201 starting, 189 SLEEP function, 286 SMALLINT data type, 505 SOME clause, using with multiple fields subqueries, 231 sorting data output with ORDER BY clause, 25 GROUP BY clause, SELECT statement, 140 SOUNDEX function, 256 source distributions (MySQL), source files, configuring to build binary programs, SPACE function, 257 SPATIAL indexes, 63 creating, 81 SQL SECURITY clause, 98 SQL SECURITY keyword, 211 SQL threads, slave server, 182, 201 SQL_BUFFER_RESULT keyword, SELECT statement, 138 SQL_CACHE keyword, SELECT statement, 138 SQL_CALC_FOUND_ROWS option, SELECT statement, 138, 153 540 | Index SQL_NO_CACHE keyword, SELECT statement, 138 SQL_QUOTE_SHOW_CREATE variable SHOW CREATE DATABASE, 107 SHOW CREATE TABLE, 108 SQL_SMALL_RESULT keyword, SELECT statement, 138 SQRT function, 306 SSL cipher name, 419 DES_ENCRYPT function and, 52 encrypted connections, 45 mysql_ssl_set function, 432 setting variables with CHANGE MASTER TO, 192 variables in master.info file, 181 START SLAVE statement, 189, 197 START TRANSACTION statement, 147 unlocking tables, 163 state method, 468 statements data manipulation, 113 delimiter, changing, 214 executing with Perl DBI, 438 related to database and table schema, 57–112 replication, 191–199 restricting user privileges to, 45 security and user, 40 semicolon ending, 206 stored routines, 203 statistics_info method, 468 status information for tables, 111 STATUS option FLUSH statement, 160 STD function, 226 STDDEV function, 226 STDDEV_POP function, 227 STDDEV_SAMP function, 227 STOP SLAVE statement, 190, 198 storage engines changing for a table, 90 changing with ALTER TABLE, ENGINE option, 76 converting database tables for, 366 index types, 61 instructing how to store rows of data, 77 mysqld server options, 346–352 InnoDB, 348 MEMORY and NDB, 352 MylSAM, 346 SHOW ENGINE statement, 165 supporting transactions, 115 transaction support, 113 stored procedures ALTER PROCEDURE statement, 205 calling, 207 CASE statement, 310 creating, 210 cursors, 214 deleting, 215 displaying information on, 219 displaying internal code, 219 IF statement, 311 SHOW CREATE PROCEDURE statement, 217 starting and ending, 206 use of FLUSH statement, 42 user-defined functions versus, 208 stored routines, 203 ALTER EVENT statement, 203 STRAIGHT_JOIN keyword, 127 STRCMP function, 257 string functions, 235–261 ASCII, 236 BIN, 237 BINARY, 237 BIT_LENGTH, 237 CAST, 238 CHAR, 238 character sets and collation, 235 CHARACTER_LENGTH, 240 CHARSET, 240 CHAR_LENGTH, 239 COALESCE, 240 COERCIBILITY, 241 COLLATION, 241 COMPRESS, 242 CONCAT, 242 CONCAT_WS, 243 conversions, 235 CONVERT, 244 CRC, 244 ELT, 244 EXPORT_SET, 245 expressions, 236 extraction, 236 FIELD, 245 FIND_IN_SET, 246 formatting, 235 grouped by type, 235 HEX, 246 INSERT, 247 INSTR, 247 INTERVAL, 248 LCASE, 248 LEFT, 249 LENGTH, 249 LOAD_FILE, 250 LOCATE, 250 LOWER, 251 LPAD, 251 LTRIM, 252 MAKE_SET, 252 manipulating strings, 236 MATCH, 252 MID, 253 OCTET_LENGTH, 253 ORD, 253 POSITION, 254 QUOTE, 254 REPEAT, 255 REPLACE, 255 REVERSE, 255 RIGHT, 255 RPAD, 256 RTRIM, 256 SOUNDEX, 256 SPACE, 257 STRCMP, 257 SUBSTR, 258 SUBSTRING, 258 SUBSTRING_INDEX, 258 TRIM, 259 UCASE, 259 UNCOMPRESS, 260 UNCOMPRESSED_LENGTH, 260 UNHEX, 260 UPPER, 261 strings character set and collation, 270 CONCAT function, 25 data types, 507–510 Index | 541 STR_TO_DATE function, 210 subject, requiring for X.509 certificate, 46 subpartitions, 93 defining with CREATE TABLE, 96 subqueries, 30, 229–234 multiple fields, 231 results set, 233 sales data example, 222 SELECT statement inner query, 229 single field, 230 using with MAX function, 225 SUBSTR function, 258 SUBSTRING function, 258 SUBSTRING_INDEX function, 258 SUM function, 227 Sun Solaris systems, installing MySQL binary distribution, 10 swap_inner_handle method, 468 system variables, 144, 165 SYSTEM_USER function, 54 T TABLE option, FLUSH statement, 160 tables altering with ALTER TABLE, 59–77 creating in a database, 21 deleting with DROP TABLE, 103 displaying a list of tables and views, 112 listing for a database, 23, 495 listing in current database, 423 listing with mysqlshow, 396 renaming, 104 revoking user privileges for, 49 SHOW CREATE TABLE statement, 107 SHOW TABLE STATUS statement, 111 user access privileges, 44 tables function, 469 TABLES option, FLUSH statement, 160 TABLES WITH READ LOCK option, FLUSH statement, 160 tablespaces discarding or importing, 69 TABLESPACE subclause of CREATE TABLE statement, 96 table_info function, 468 542 | Index take_imp_data method, 469 TAN function, 307 TAR package, MySQL installation on Mac OS X, 13 tar utility, TEMPORARY flag, DROP TABLE statement, 103 TEXT data type, 22, 509 TEXT_FIELDS, 32 threads mysql_thread_end function, 433 mysql_thread_id function, 499 mysql_thread_init function, 434 mysql_thread_safe function, 434 TIME data type, 263, 507 time zones, converting dates and times between, 265 TIMESTAMP data type, 263, 507 TIMESTAMP function, 291 TIMESTAMPDIFF function, 291 timestamps, changing time for events, 204 TINYBLOB data type, 508 TINYINT data type, 504 TINYTEXT data type, 509 TO clause, GRANT statement, 44 TO keyword (RENAME DATABASE TO), 104 TO_DAYS function, 292 trace method, 469 trace_msg function, 469 transactions BEGIN statement, 114 COMMIT statement, 115 committing with mysql_commit, 410 distributed, using XA statement, 151 ROLLBACK statement, 134 ROLLBACK TO SAVEPOINT statement, 135 rolling back with mysql_rollback, 430 setting isolation level, 145 START TRANSACTION statement, 147 statements causing commitment of, 116 statements for, 113 triggers altering, 206 creating, 212 deleting, 215 displaying on a server, 220 FLUSH statement and, 42 starting and ending steps in, 206 tables renamed and moved to another database, 104 trimming strings LTRIM function, 252 RTRIM function, 256 TRIM function, 259 TRUNCATE function, 307 TRUNCATE statement, 116, 147 TYPE clause, 87 TYPE keyword, 80 type_info method, 469 type_info_all function, 470 U UCASE function, 259 UNCOMPRESS function, 260 UNCOMPRESSED_LENGTH function, 260 UNDO handler, 214 UNHEX function, 260 UNION keyword, using with SELECT statements, 148 UNION option ALTER TABLE statement, 77 CREATE TABLE statement, 92 UNIQUE columns, 29 UNIQUE flag, using to prevent index duplicates, 80 UNIQUE key columns, 84 Universal Time, Coordinated (UTC), 293 Universal Unique Identifier (UUID), 174 Unix operating systems replication server configuration file, 185 Unix systems entering SQL statements into MySQL through the shell, 34 installing binary distributions of MySQL, 10 installing source distributions of MySQL, main configuration file for MySQL, 17 server options file, 321 Unix time, 276 UNIX_TIMESTAMP function, 292 UNLOCK TABLES statement, 163, 172 UNTIL clause, START SLAVE statement, 190 UPDATE statements, 29, 149 JOIN clause, 125 MAX_UPDATES_PER_HOUR option, 46 multiple table updates, 150 priority over SELECT statements, 137 SET clause, 149 trigger execution and, 212 upper case (UCASE) function, 259 UPPER function, 261 USAGE keyword, 47 USE INDEX clause, using with JOIN, 127 USE statement, 151 user accounts changing password, 49 creating, 40 creating and setting privileges, deleting from MySQL server, 41 dropped tables and user privileges, 103 flushing and reloading privileges, 42 functions for maintenance of, 50–55 granting user privileges, 44 listing available privileges, 50 mysql group and system accounts, replication account, 183 revoking privileges, 49 setting up a user for general use, 18 statements for maintenance of, 40–50 USER function, 55 user variables, 165 user-defined functions, 208 changing, 210 deleting, 215 displaying information on, 219 user-defined variables, 31 username, 40 changing, 48 specifying in CREATE SERVER statement, 83 USER_RESOURCES option, FLUSH statement, 161 Index | 543 USE_FRM option, REPAIR TABLE statement, 164 USING keyword DELETE statement, 117 specifying index type, 80 using with JOIN, 126 USING subclause, 62 UTC_DATE function, 293 UTC_TIME function, 293 UTC_TIMESTAMP function, 293 utilities (see command-line utilities) UUID function, 174 V VALUES keyword CREATE TABLE subclause, 96 using with REPLACE statement, 133 VARBINARY data type, 508 VARCHAR data type, 508 variables displaying system variables for MySQL server, 171 server and environment variables, 517 setting, 165 setting for global or session use, 144 setting for slave server, 197 user-defined, 31 VARIANCE function, 228 VAR_POP function, 228 VAR_SAMP function, 228 VERSION function, 174 vertical bar (|), changing statement delimiter to, 211 views creating, 97 deleting, 104 displaying a list for the current database, 112 displaying list of, using SHOW TABLES, 112 SHOW CREATE VIEW statement, 108 SHOW TABLE STATUS used with, 171 W warning messages, 146, 435 544 | Index web page for this book, xv web sites for MySQL information, WEEK function, 294 modes, 295 WEEKDAY function, 295 WEEKOFYEAR function, 296 WHERE clause DELETE statement, 116 HANDLER…READ statement, 121 MATCH function, 252 ORDER BY clause and, 26 SELECT statement, 24 SHOW COLUMNS statement, 106 SHOW DATABASES statement, 108 SHOW EVENTS statement, 218 SHOW OPEN TABLES statement, 167 SHOW PROCEDURE STATUS statement, 219 SHOW STATUS statement, 168 SHOW TABLE STATUS statement, 170 SHOW TABLES statement, 112 SHOW VARIABLES statement, 171 single field subquery used with = operator, 230 UPDATE statement, 150 multiple table updates, 150 using with SELECT statements, 137 using with UPDATE statement, 29 WHERE EXISTS clause, 233 Widenius, Michael (Monty), wildcards asterisk (*), 44 using with SELECT statements, 137 percent sign (%), 31 percent sign (%) and _, using with DESCRIBE statement, 99 using with LIKE clause of SHOW CHARACTER SET, 105 Windows Essential package, 15 Windows systems installing MySQL, 15 main configuration file for MySQL, 17 replication server configuration file, 185 server options file, 321 WITH CHECK OPTION clause, 98 WITH CONSISTENT SNAPSHOT clause, 147 WITH GRANT OPTION clause, 44 WITH PARSER clause ALTER TABLE statement, 62 CREATE TABLE statement, 87 WITH ROLLUP keyword, GROUP BY clause of SELECT, 140 WORK keyword COMMIT statement, 115 ROLLBACK statement, 134 WRITE lock, 162 X X.509 certificates, 45 XA statement, 151 XOR, bitwise exclusive OR, 223 Y YEAR data type, 263, 507 YEAR function, 296 YEARWEEK function, 296 Index | 545 About the Author Russell J.T Dyer, a freelance writer specializing in MySQL database software, is the editor of the MySQL Knowledge Base (http://www.mysql.com/network/knowl edgebase.html) He is the author of the first edition of MySQL in a Nutshell (http:// www.oreilly.com/catalog/mysqlian/) and has writen articles for many publications: DevZone (a MySQL publication), Linux Journal, ONlamp.com, The Perl Journal, Red Hat Magazine, Sys Admin magazine, TechRepublic, Unix Review, and XML.com He has also finished his first novel, In Search of Kafka More information on Russell, along with a list of his published articles and links to them, can be found on his web site at http://russell.dyerhouse.com Colophon The animal on the cover of MySQL in a Nutshell, Second Edition, is the pied kingfisher (Ceryle rudis) At 80 grams and 28 centimeters in length, the pied kingfisher is the largest bird in the world capable of a true hover in still air Like most kingfishers, it hunts small fish from a perch or by hovering over open water But unlike others, the pied kingfisher often travels up to three miles from land While the closely related giant kingfisher relies heavily on shoreline perching places, the pied kingfisher can hover above choppy water and swallow its prey on the fly For this adaptive skill, the pied kingfisher is considered the most advanced of the 87 kingfisher species Pied kingfishers are common and widespread across much of Africa, the Middle East, and Southeast Asia, and are easily distinguishable from other kingfishers by their unique black and white markings Never far from water, pied kingfishers breed in burrows excavated into riverbanks These birds form family groups, with the previous season’s offspring often helping to raise their parents’ next brood Additional male helpers may also contribute food depending on their availability If food is scarce, the breeding male feeds its mate, while helpers feed both parents and chicks after hatching Helpers may thus increase their chances of mating with a nesting female the following year Although kingfishers are known for their fishing skills, many kingfishers don’t eat fish at all; among those that do, less than half of all dives are successful Kingfishers are apparently blind under water, so their survival depends on perfect aim from above They are able to judge both the size and depth of fish swimming below—the two greatest factors in determining a likely and rewarding catch The instant a kingfisher hits water, opaque, protective third eyelids called nicitating membranes cover the eyes More than a few hungry kingfishers have been seen emerging with stones in their bills Still, among piscivorous birds, the kingfisher has earned its name justly The cover image is a 19th-century engraving from the Dover Pictorial Archive The cover font is Adobe ITC Garamond The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSansMonoCondensed ... mysql Client Creating a Database and Tables Show Me Inserting Data Selecting Data 19 20 23 23 24 vii Ordering, Limiting, and Grouping Analyzing and Manipulating Data Changing Data Deleting Data... products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps... other databases and wants to move to MySQL, who has used a MySQL database on a hosting service and wants to create a standalone server, or who has other gaps in introductory knowledge Installation,
- Xem thêm -

Xem thêm: 973 MySQL in a nutshell, 2nd edition , 973 MySQL in a nutshell, 2nd edition , Chapter 4. Security and User Statements and Functions, Chapter 5. Database and Table Schema Statements, Chapter 6. Data Manipulation Statements and Functions, Chapter 7. Table and Server Administration Statements and Functions, Chapter 8. Replication Statements and Functions, Chapter 10. Aggregate Clauses, Aggregate Functions, and Subqueries, Chapter 12. Date and Time Functions, Chapter 15. MySQL Server and Client, Appendix C. Server and Environment Variables

Mục lục

Xem thêm