Top Banner

Click here to load reader

MySQL Tutorial · PDF fileMySQL Tutorial. Abstract This is the MySQL Tutorial from the MySQL 5.6 Reference Manual. For legal information, see the Legal Notices. ... connect to local

Mar 17, 2019

ReportDownload

Documents

phungnga

MySQL Tutorial

Abstract

This is the MySQL Tutorial from the MySQL 5.6 Reference Manual.

For legal information, see the Legal Notices.

For help with using MySQL, please visit either the MySQL Forums or MySQL Mailing Lists, where you can discussyour issues with other MySQL users.

Document generated on: 2019-03-15 (revision: 61264)

http://forums.mysql.comhttp://lists.mysql.com

iii

Table of ContentsPreface and Legal Notices .................................................................................................................. v1 Tutorial ........................................................................................................................................... 12 Connecting to and Disconnecting from the Server ............................................................................ 33 Entering Queries ............................................................................................................................. 54 Creating and Using a Database ....................................................................................................... 9

4.1 Creating and Selecting a Database ..................................................................................... 104.2 Creating a Table ................................................................................................................ 114.3 Loading Data into a Table .................................................................................................. 124.4 Retrieving Information from a Table ..................................................................................... 13

4.4.1 Selecting All Data .................................................................................................... 144.4.2 Selecting Particular Rows ......................................................................................... 144.4.3 Selecting Particular Columns .................................................................................... 154.4.4 Sorting Rows ........................................................................................................... 174.4.5 Date Calculations ..................................................................................................... 184.4.6 Working with NULL Values ....................................................................................... 204.4.7 Pattern Matching ...................................................................................................... 214.4.8 Counting Rows ........................................................................................................ 244.4.9 Using More Than one Table ..................................................................................... 26

5 Getting Information About Databases and Tables ........................................................................... 296 Using mysql in Batch Mode ........................................................................................................... 317 Examples of Common Queries ...................................................................................................... 33

7.1 The Maximum Value for a Column ...................................................................................... 337.2 The Row Holding the Maximum of a Certain Column ........................................................... 347.3 Maximum of Column per Group .......................................................................................... 347.4 The Rows Holding the Group-wise Maximum of a Certain Column ........................................ 347.5 Using User-Defined Variables ............................................................................................. 357.6 Using Foreign Keys ............................................................................................................ 367.7 Searching on Two Keys ...................................................................................................... 377.8 Calculating Visits Per Day ................................................................................................... 387.9 Using AUTO_INCREMENT ................................................................................................. 38

8 Using MySQL with Apache ............................................................................................................ 43

iv

v

Preface and Legal NoticesThis is the MySQL Tutorial from the MySQL 5.6 Reference Manual.

Licensing informationMySQL 5.6. This product may include third-party software, used underlicense. If you are using a Commercial release of MySQL 5.6, see the MySQL 5.6 Commercial ReleaseLicense Information User Manual for licensing information, including licensing information relating to third-party software that may be included in this Commercial release. If you are using a Community releaseof MySQL 5.6, see the MySQL 5.6 Community Release License Information User Manual for licensinginformation, including licensing information relating to third-party software that may be included in thisCommunity release.

Licensing informationMySQL NDB Cluster 7.3. This product may include third-party software, usedunder license. If you are using a Commercial release of NDB Cluster 7.3, see the MySQL NDB Cluster7.3 Commercial Release License Information User Manual for licensing information relating to third-partysoftware that may be included in this Commercial release. If you are using a Community release of NDBCluster 7.3, see the MySQL NDB Cluster 7.3 Community Release License Information User Manual forlicensing information relating to third-party software that may be included in this Community release.

Licensing informationMySQL NDB Cluster 7.4. This product may include third-party software, usedunder license. If you are using a Commercial release of NDB Cluster 7.4, see the MySQL NDB Cluster7.4 Commercial Release License Information User Manual for licensing information relating to third-partysoftware that may be included in this Commercial release. If you are using a Community release of NDBCluster 7.4, see the MySQL NDB Cluster 7.4 Community Release License Information User Manual forlicensing information relating to third-party software that may be included in this Community release.

Legal Notices

Copyright 1997, 2019, Oracle and/or its affiliates. All rights reserved.

This software and related documentation are provided under a license agreement containing restrictionson use and disclosure and are protected by intellectual property laws. Except as expressly permittedin your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast,modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by anymeans. Reverse engineering, disassembly, or decompilation of this software, unless required by law forinteroperability, is prohibited.

The information contained herein is subject to change without notice and is not warranted to be error-free.If you find any errors, please report them to us in writing.

If this is software or related documentation that is delivered to the U.S. Government or anyone licensing iton behalf of the U.S. Government, then the following notice is applicable:

U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software,any programs installed on the hardware, and/or documentation, delivered to U.S. Government end usersare "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of theprograms, including any operating system, integrated software, any programs installed on the hardware,and/or documentation, shall be subject to license terms and license restrictions applicable to the programs.No other rights are granted to the U.S. Government.

This software or hardware is developed for general use in a variety of information managementapplications. It is not developed or intended for use in any inherently dangerous applications, includingapplications that may create a risk of personal injury. If you use this software or hardware in dangerous

http://downloads.mysql.com/docs/licenses/mysqld-5.6-com-en.pdfhttp://downloads.mysql.com/docs/licenses/mysqld-5.6-com-en.pdfhttp://downloads.mysql.com/docs/licenses/mysqld-5.6-gpl-en.pdfhttp://downloads.mysql.com/docs/licenses/cluster-7.3-com-en.pdfhttp://downloads.mysql.com/docs/licenses/cluster-7.3-com-en.pdfhttp://downloads.mysql.com/docs/licenses/cluster-7.3-gpl-en.pdfhttp://downloads.mysql.com/docs/licenses/cluster-7.4-com-en.pdfhttp://downloads.mysql.com/docs/licenses/cluster-7.4-com-en.pdfhttp://downloads.mysql.com/docs/licenses/cluster-7.4-gpl-en.pdf

Documentation Accessibility

vi

applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and othermeasures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damagescaused by use of this software or hardware in dangerous applications.

Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarksof their respective owners.

Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarksare used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD,Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of AdvancedMicro Devices. UNIX is a registered trademark of The Open Group.

This software or hardware and documentation may provide access to or i

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.