MySQL - Università di Enna A.A. 2005-06

Embed Size (px)

Citation preview

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    1/58

    Mysql

    Basi di Dati

    Universit di Enna A.A. 2005/06

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    2/58

    Intro

    MySQL and the MySQL logo are registered trademarks ofMySQL AB (a Swedish company)

    The MySQL software delivers a very fast, multi-threaded, multi-user, and robust SQL (Structured Query Language) database

    server

    The MySQL Web site (http://www.mysql.com/) provides the

    latest information about MySQL and MySQL AB

    the AB part of the company name is the acronym for theSwedish aktiebolag, orstockcompany

    http://www.mysql.com/http://www.mysql.com/http://www.mysql.com/http://www.mysql.com/http://www.mysql.com/http://www.mysql.com/http://www.mysql.com/http://www.mysql.com/
  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    3/58

    License

    The MySQL software is Dual Licensed

    Users can choose to use the MySQL software as an Open

    Source product under the terms of the GNU General Public

    LicenseUsers can purchase a standard commercial license from

    MySQL AB

    See http://www.mysql.com/company/legal/licensing/for more information on our licensing policies.

    http://www.mysql.com/company/legal/licensing/http://www.mysql.com/company/legal/licensing/http://www.mysql.com/company/legal/licensing/http://www.mysql.com/company/legal/licensing/http://www.mysql.com/company/legal/licensing/http://www.mysql.com/company/legal/licensing/http://www.mysql.com/company/legal/licensing/http://www.mysql.com/company/legal/licensing/
  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    4/58

    Documentation

    The MySQL Database Software is under constant

    development, and the Reference Manual is updated

    frequently as well. The most recent version of the

    manual is available online in searchable form athttp://dev.mysql.com/doc/

    Formats available:

    HTML

    PDF

    CHM

    http://dev.mysql.com/doc/http://dev.mysql.com/doc/http://dev.mysql.com/doc/http://dev.mysql.com/doc/http://dev.mysql.com/doc/http://dev.mysql.com/doc/http://dev.mysql.com/doc/http://dev.mysql.com/doc/
  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    5/58

    Operating Systems Supported by MySQL

    It is possible to port MySQL to all modern systems

    that have a C++ compiler and a working

    implementation ofPOSIX threads

    Thread support is needed for the server. To compileonly the client code, the only requirement is a C++

    compiler

    We use and develop the software ourselves primarily

    on Linux (SuSE and Red Hat), FreeBSD, and Sun

    Solaris (Versions 8 and 9)

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    6/58

    Operating Systems Supported by MySQL

    MySQL has been reported to compile successfully onthe following operating systems:

    AIX 4.x, 5.x

    Amiga BSDI 2.x

    Digital Unix 4.x

    FreeBSD 2.x, 3.x, 4.x

    Mac OS X

    Windows 9x, Me, NT, 2000, XP, and 2003

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    7/58

    Installing MySQL on Windows

    A native Windows version of MySQL has been

    available from MySQL AB since version 3.21 and

    represents a sizable percentage of the daily

    downloads of MySQL

    The installer for the Windows version ofMySQL 5.0,

    combined with a GUI Configuration Wizard,

    automatically installs MySQL, creates an option file,starts the server, and secures the default user accounts

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    8/58

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    9/58

    Installing MySQL on Windows

    A copy of the MySQL binary distribution forWindows, which can be downloaded fromhttp://dev.mysql.com/downloads/

    You may also have the following optionalrequirements:

    If you plan to connect to the MySQL server via ODBC,

    you also need a Connector/ODBC driver

    If you need tables with a size larger than 4GB, install

    MySQL on an NTFS or newer filesystem

    http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/http://dev.mysql.com/downloads/
  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    10/58

    For MySQL 5.0, there are three installation packagesto choose from when installing MySQL on Windows.The packages are as follows:

    Essential package

    Complete package

    Noinstall archive

    The Essentials package is recommended for mostusers

    Installing MySQL on Windows

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    11/58

    The following entries are created within the newStart menu section:MySQL Command Line Client : This is a shortcut to the

    mysql command-line client and is configured to connect as

    the root user. The shortcut prompts for a root user passwordwhen connecting

    MySQL Server Instance Config Wizard : This is ashortcut to the MySQL Configuration Wizard. Use thisshortcut to configure a newly installed server, or to re-configure an existing server

    MySQL Documentation : This is a link to the MySQLserver documentation that is stored locally in the MySQLserver installation directory. This option is not availablewhen the MySQL server is installed using the Essentialsinstallation package.

    Installing MySQL on Windows

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    12/58

    The Main Features of MySQL

    Written in C and C++

    Tested with a broad range of differentcompilers

    Works on many different platforms

    Uses GNU Automake, Autoconf, and Libtoolfor portability

    APIs for C, C++, Eiffel, Java, Perl, PHP,Python, Ruby, and Tcl are available

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    13/58

    Column Types

    Many column types:

    signed/unsigned integers 1, 2, 3, 4, and 8 bytes long

    FLOAT, DOUBLE

    CHAR, VARCHAR

    TEXT

    BLOB

    DATE, TIME, DATETIME, TIMESTAMP, YEARSET, ENUM

    Fixed-length and variable-length records

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    14/58

    Statements and Functions

    Full operator and function support in the SELECTand WHERE clauses of queries.

    Full support for SQL GROUP BY and ORDER BY

    clauses. Support for group functions (COUNT(),COUNT(DISTINCT ...), AVG(), STD(), SUM(),MAX(), MIN()

    Support for LEFT OUTER JOIN and RIGHT

    OUTER JOIN Support for aliases on tables and columns as required

    by standard SQL

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    15/58

    Statements and Functions (2)

    DELETE, INSERT, REPLACE, and UPDATE return the

    number of rows that were changed (affected)

    The MySQL-specific SHOW command can be used to

    retrieve information about databases, database engines, tables,and indexes. The EXPLAIN command can be used to

    determine how the optimizer resolves a query

    Function names do not clash with table or column names. For

    example, ABSis a valid column name. The only restriction isthat for a function call, no spaces are allowed between the

    function name and the ( that follows it

    You can mix tables from different databases in the same query

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    16/58

    Limits

    Handles large databases.

    We use MySQL Server with databases that

    contain 50 million records We also know of users who use MySQL

    Server with 60,000 tables and about 5,000,000,000

    rows

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    17/58

    Connectivity

    Clients can connect to the MySQL server usingTCP/IP sockets on any platform

    The Connector/ODBC (MyODBC) interface provides

    MySQL support for client programs that use ODBC(Open Database Connectivity) connections.

    For example, you can use MS Access to connect to yourMySQL server.

    The Connector/J interface provides MySQLsupport for Java client programs that useJDBC connections

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    18/58

    How Big MySQL Tables Can Be

    MySQL 3.22 had a 4GB (4 gigabyte) limit on table

    size.

    With the MyISAM storage engine in MySQL 3.23,

    the maximum table size was increased to 65536terabytes (25671 bytes).

    With this larger allowed table size, the maximum

    effective table size for MySQL databases is usually

    determined by operating system constraints on file

    sizes, not by MySQL internal limits

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    19/58

    The following table lists some examples of operatingsystem file-size limits. This is only a rough guide andis not intended to be definitive

    Win32 (FAT/FAT32) 2GB/4GB Win32 (NTFS) 2TB

    Linux 2.2-Intel 2GB

    Linux 2.4+ (ext3) 4TB

    Windows users please note: FAT and VFAT (FAT32)are not considered suitable for production use withMySQL. Use NTFS instead

    How Big MySQL Tables Can Be

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    20/58

    Connecting to and Disconnecting from

    the Server

    To connect to the server, you will usually need to

    provide a MySQL username when you invoke mysql

    and, most likely, a password. If the server runs on a

    machine other than the one where you log in, you willalso need to specify a hostname

    shell> mysql -h host-u userp

    Enter password: ********

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    21/58

    Connecting to and Disconnecting from

    the Server

    shell> mysql -h host-u userp

    Enter password: ********

    host and user represent the hostname where your

    MySQL server is running and the username of your

    MySQL account

    The ******** represents your password; enter itwhen mysql displays the Enter password: prompt.

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    22/58

    Connecting to and Disconnecting from

    the Server

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    23/58

    Connecting to and Disconnecting from

    the Server

    Some MySQL installations allow users to connect asthe anonymous (unnamed) user to the server runningon the local host.

    If this is the case on your machine, you should beable to connect to that server by invoking mysqlwithout any options:

    shell> mysql

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    24/58

    Connecting to and Disconnecting from

    the Server

    After you have connected successfully, you can

    disconnect any time by typing QUIT (or EXIT) at

    the mysql > prompt:

    shell> QUIT

    Bye

    shell> EXIT

    Bye

    On Unix, you can also disconnect by pressing Control-D

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    25/58

    Entering Queries

    mysql> SELECT VERSION( ), CURRENT_DATE;

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    26/58

    Notes When you issue a command, mysql sends it to the

    server for execution and displays the results, thenprints another mysql> prompt to indicate that it is

    ready for another command.

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    27/58

    Notes (2)

    mysql displays query output in tabular form (rows

    and columns). The first row contains labels for thecolumns. The rows following are the query results.

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    28/58

    Notes (3)

    mysql shows how many rows were returned and how

    long the query took to execute, which gives you arough idea of server performance

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    29/58

    Entering Queries

    Keywords may be entered in any lettercase. The

    following queries are equivalent:

    mysql> SELECT VERSION(), CURRENT_DATE;

    mysql> select version(), current_date;

    mysql> SeLeCt vErSiOn(), current_DATE;

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    30/58

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    31/58

    You can even enter multiple statements on a singleline. Just end each one with a semicolon

    Entering Queries

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    32/58

    A command need not be given all on a single line, so

    lengthy commands that require several lines are not a

    problem

    mysql determines where your statement ends by

    looking for the terminating semicolon, not by looking

    for the end of the input line

    Entering Queries

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    33/58

    Entering Queries

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    34/58

    The following table shows each of the prompts you may

    see and summarizes what they mean about the state that

    mysql is in:

    mysql>Ready for new command.->Waiting for next line of multiple-line command

    >Waiting for next line, waiting for completion of

    a string that began with a single quote (')

    Entering Queries

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    35/58

    "> Waiting for next line, waiting for completion of a

    string that began with a double quote (")

    `> Waiting for next line, waiting for completion of an

    identifier that began with a backtick(`)

    /*> Waiting for next line, waiting for completion of a comment

    that began with /*

    Entering Queries

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    36/58

    Entering Queries

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    37/58

    Entering Queries

    mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30;

    '>

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    38/58

    Creating and Using a Database

    This section shows you how to:

    Create a database

    Create a table

    Load data into the table

    Retrieve data from the table in various ways

    Use multiple tables

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    39/58

    Use the SHOW statement to find out what databasescurrently exist on the server

    Creating and Using a Database

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    40/58

    If the test database exists, try to access it

    Creating and Using a Database

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    41/58

    USE

    USE, like QUIT, does not require a semicolon

    You can terminate such statements with asemicolon if you like; it does no harm

    It must be given on a single line

    Creating and Using a Database

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    42/58

    Creating and Selecting a Database

    If the administrator creates your database for you when settingup your permissions, you can begin using it. Otherwise, you

    need to create it yourself

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    43/58

    Creating and Selecting a Database

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    44/58

    Creating a Table

    Creating the database is the easy part, but at this pointit's empty, as SHOW TABLES tells you:

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    45/58

    Creating a Table

    Creating the database is the easy part, but at this pointit's empty, as SHOW TABLES tells you:

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    46/58

    Creating a Table

    Create table slides (id integer, contents varchar(30), images char);

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    47/58

    Creating a Table

    mysql> show tables;

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    48/58

    Creating a Table

    mysql> describe slides;

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    49/58

    Loading Data into a Table

    After creating your table, you need to populate it. The

    LOAD DATA statement is useful for this

    Because you are beginning with an empty table, an

    easy way to populate it is to create a text file

    containing a row for each of your slide, then load the

    contents of the file into the table with a singlestatement

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    50/58

    Loading Data into a Table

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    51/58

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    52/58

    Loading Data into a Table

    mysql> LOAD DATA LOCAL INFILE- > 'C:\\Documents and Settings\\User\\Documenti\\Didattica\\ceri\\Mysql\\dati\\

    > slides.txt' INTO TABLE slides;

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    53/58

    Retrieving Information from a Table

    mysql> select * from slides;

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    54/58

    mysql> select contents from slides;

    Retrieving Information from a Table

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    55/58

    mysql> delete from slides;

    Retrieving Information from a Table

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    56/58

    mysql> select * from slides;

    Retrieving Information from a Table

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    57/58

    Source

  • 7/27/2019 MySQL - Universit di Enna A.A. 2005-06

    58/58

    Source