Get It Done with Mysql 5&6
Total Page:16
File Type:pdf, Size:1020Kb
Get it Done with MySQL 5&6 Print Edition for MySQL 5.0.95, 5.1.61, 5.5.21, 5.6.4, 6.0.11 by Peter Brawley and Arthur Fuller published by Artful Software Development http://www.artfulsoftware.com Copyright © Peter Brawley and Arthur Fuller 2005-2012 March 2012 ISBN 978-0-9765839-0-5 SECTION I: DATABASES AND MYSQL 1. Relational Databases 1 What is a database? 1 Sets 4 EF Codd 6 Practical rules 10 Normalisation 12 Limits of the relational model 18 2. MySQL: Past, Present, Future 20 Beginnings 20 The product 21 3. Installing MySQL 24 Version 24 Configuration 26 Character sets 28 Client program 28 *Nix binary installation 30 *Nix source 31 Second *Nix server 32 Windows binary installation 33 Second Windows server 36 Windows source 37 Testing 38 Upgrades 38 Timezones 39 Other installations 40 4. SQL Data Types and MySQL Column Types 43 Types and modifiers 43 Character/binary 44 ENUM, SET 46 Numeric 47 Datetime 49 OpenGIS 51 NULL 52 Keys 52 Referential integrity 53 5. Writing a Specification 54 Requirements modelling 54 Modelling tools 55 Step 1: Users 57 Step 2: Tasks 57 Step 3: Use cases 59 Step 4: Build and test the database 66 SECTION II: WORKING WITH MYSQL 6. MySQL Command Syntax 69 SQL 69 Identifiers, Comments, Operators 71 Sessions 75 Use, Set 76 DDL: Databases 78 Tables 80 Tablespaces 91 Views 92 Triggers 93 Events 94 Analyze, Backup, Cache Index, Describe 96 Privileges 98 User commands 104 Optimize, Repair 105 Show 106 Information_schema 108 Replication 111 DML: Select 114 Explain 126 Union 123 Insert 128 Update 131 Delete 131 Truncate, Replace 132 Load Data 132 Do, Prepare 135 Transactions 136 7. Introduction to Storage Engines 140 Archive, BDB 140 Blackhole, CSV, Example, Falcon 141 Federated 141 Maria 142 Memory 143 MyISAM, Merge, InnoDB 144 NDB 151 Performance 153 8. MySQL Functions, Procedures, Plugins 154 Control flow 154 Aggregate 154 Numeric 156 Bit 158 String 158 Datetime 164 Data type, Encoding 169 XML 170 Session 172 OpenGiS 173 Miscellaneous 177 Stored procedures and functions 177 Naming 178 Create 179 Show Create 181 Call, Syntax 182 SIGNAL 185 Cursors, Control constructs 187 Plugins 191 9. Designing queries for MySQL 193 Atomic and molecular queries 193 From, Joins 196 Where 200 Many-many 205 Group By 206 Common queries 206 Optimising 208 Explain 209 SECTION III: PROGRAMMING WITH MYSQL 10. MySQL APIs 218 11. ODBC 221 Architecture 221 Connector 221 Port an Access DB 223 Named queries 233 12. PHP 234 Install under *nix 234 Install under Windows 236 PHP basics 240 Sessions and security 248 Small project 252 TheUsual 261 13. Perl 267 Install 267 CGI 268 DBI 269 Query 269 Web example 271 Refinements 273 14. Java 276 Architectures 277 Download and install 278 JDBC models 281 Server-side MySQL access 287 JDBC 3 290 Taglibs 292 GUI controls 295 15. DotNET 297 Architecture 299 ODBC.NET 302 Master-detail window with ODBC 307 Native .NET providers 309 Master-detail window with MSQLDirect 310 With Connector/NET 311 ASP.NET 314 ASP.NET and Web Matrix 316 ASP.NET and web pages 322 Datagrid paging 321 16. C API 327 Compilers 327 Data types 321 Example code 324 Functions 329 SECTION IV: DATABASE ADMINISTRATION 17. Administration 342 Configuring a server 343 Logs 346 Memory 348 Connections, Options 349 Speed and size 352 Reliability 352 Replication 353 RAID 362 Distribution 363 OLTP and OLAP 364 Disaster planning 366 18. Administrative Tools 369 Option syntax 369 myisamchk 370 mysqlcheck 373 msql2mysql 375 mysqlaccess 375 mysqladmin 376 mysqlbinlog 377 mysqlbug 372 mysqlimport 379 mysqldump 380 mysqlhotcopy 384 mysqlmanager 386 mysqlshow 388 mysql_install_db 389 replace 390 myisampack 390 mysqlwatch 392 my_print_defaults 392 perror 393 GUI administrative tools 393 19. Security 395 Grant, Revoke 395 Configure for security 398 Authentication 400 Access roles 403 SECTION V: ADVANCED TOPICS 20: Trees and Other Graphs in MySQL 406 Models 406 Edge list model 408 Edge list tree 410 Nested sets tree 420 Edge list non-tree 429 Parts explosions 436 21: Time in MySQL 444 Basic concepts 444 MySQL limitations 445 Test schemas 445 Row duplication in time 447 Time validity in a table 449 Sequenced keys 450 Referential integrity in time 451 Temporal state queries 454 Snapshot equivalence 455 Sequenced queries 457 Partitioning history from now 461 Modifying state tables 462 Transaction time validity 468 Tracking logs 469 Bitemporal tables 474 22: Visual Studio 2005 and MySQL 486 Connector/NET 486 TheUsual Specification 487 Project setup 489 Connection 401 Query methods 493 Login form 495 Properties 499 Select database 500 Select tables 502 Paging 504 Action buttons 508 TextEditor 509 Find 511 Grid methods 514 Just-in-time lookup 525 Context menu 532 ListView lookups 536 Events 540 Version for SQL Server 540 Build 551 APPENDICES A: Glossary 553 B: MySQL Configuration Settings 559 Option and variable syntax 559 Server settings and system variables 561 sql_mode 588 Structured variables 590 C: Other MySQL Reference Data 591 information_schema 5.0 592 information_schema 5.1 597 information_schema 5.5/6 607 InnoDB 618 Performance_schema 624 Show Status 624 INDEX i1-10 About the Authors Peter Brawley has been designing and developing database software for more than thirty years—since he found he was more interested in the software running his neurobiology experiments than in the experiments themselves. Since then he has been building database software for scientific research and enterprise-level database applications for North American auto and other manufacturers, movie studios, and other clients large and small. With Arthur he has published development tools for database developers. He edited one of Arthur's books, co-authored another, has been a speaker at innumerable software meetings in North America, Europe, the Middle and Far East, and Africa, and writes for database magazines and ezines. After writing thirty-odd screenplays, Arthur Fuller finally sold one, and spent some of the money on his first computer. Within a month he was addicted to programming. For the past thirty years or so he has been developing database applications for worldwide travel agencies, pulp and paper mills, Canadian politicians, and many other clients, large and small, and with Peter has been developing and publishing productivity tools for database software developers. He is the author of a book on database application development, is co-author with Peter of another such book, and has been a speaker at innumerable software development meetings in North America, Europe and Asia. He was Builder.com's SQL Tips columnist for four years, and writes often for other database magazines and ezines. About this book Stage one of the computer revolution worked out the basic principles of computing, and applied them to mainframes that helped run the world. That was pretty much in hand by 1980. Stage two, over the next decade and a half, turned those principles into software you could buy for your desktop or laptop for a reasonable price. By 1995, if you were writing software, your peers numbered in the thousands or more. Then came stage three, which now networks billions of desktops and laptops all over the world, offering them free open source software development tools for any computer task you can imagine. MySQL is one of those tools. This is the print edition of our book about it for versions 5.0 through 5.6. In case you still use the withdrawn 6.0, this edition also covers that series. Another 6.0 release will appear eventually. There are 22 chapters in five sections, and three appendices. If you bought the book from us, you also received the ebook edition with all the above plus Appendix D (Java Tutorial) and Appendix E (Source Code). If you bought it elsewhere, you can obtain the ebook and five years of free updates by emailing proof of purchase and an image of the ISBN barcode on the back cover to [email protected]. Book sections are Databases and MySQL (chapters 1-5), Working with MySQL (chapters 6-9), Programming with MySQL (chapters 10-16), MySQL Admini- stration (chapters 17-19), and Advanced Topics (chapters 20-22). Appendix A is a glossary, Appendix B tabulates MySQL server options and dynamic variables, and Appendix C tabulates information_schema columns and server status messages. Some chapters are meant to be read start to finish. Some are packed with documentary facts and are meant mostly for looking things up—to read them front to back, you might need to be a little bent. The bits intended more for reading are … 1. An account, from scratch, of relational databases 2. A brief history of the company MySQL AB 5. Writing a specification 9. Writing and designing queries in MySQL 10-16: MySQL APIs, ODBC, PHP, Perl, Java, .NET and C. 17. Database administration 19. Security 20. Working with Graphs in MySQL 21. Working with MySQL and Visual Studio 2005 22. Time in MySQL The bits intended more for looking things up are … 3. MySQL installation 4. SQL and MySQL data types 6. MySQL command syntax 7. MySQL table types 8. Syntax for MySQL built-in and stored procedures and functions 18. Syntax for MySQL administrative utilities Appendix A. Glossary Appendix B. System Variables Appendix C. Reference Data If you are new to databases, start with database basics in Chapter 1. If you are an old database hand but new to MySQL, start with the history of MySQL in Chapter 2, then go on to installation in Chapter 3 and data types in Chapter 4. If you are an old MySQL hand, start with how to map specifications to databases in Chapter 5.