Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Mysql Basi di Dati Università di Enna A.A. 2005/06
  2. 2. Intro <ul><li>MySQL ® and the MySQL logo are registered trademarks of MySQL AB (a Swedish company) </li></ul><ul><li>The MySQL® software delivers a very fast, multi-threaded, multi-user, and robust SQL (Structured Query Language) database server </li></ul><ul><li>The MySQL Web site ( ) provides the latest information about MySQL and MySQL AB </li></ul><ul><li>the “AB” part of the company name is the acronym for the Swedish “aktiebolag,” or “stock company” </li></ul>
  3. 3. License <ul><li>The MySQL software is Dual Licensed </li></ul><ul><ul><li>Users can choose to use the MySQL software as an Open Source product under the terms of the GNU General Public License </li></ul></ul><ul><ul><li>Users can purchase a standard commercial license from MySQL AB </li></ul></ul><ul><li>See legal / licensing / for more information on our licensing policies. </li></ul>
  4. 4. Documentation <ul><li>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 at http:// / doc / </li></ul><ul><li>Formats available: </li></ul><ul><ul><li>HTML </li></ul></ul><ul><ul><li>PDF </li></ul></ul><ul><ul><li>CHM </li></ul></ul>
  5. 5. Operating Systems Supported by MySQL <ul><li>It is possible to port MySQL to all modern systems that have a C++ compiler and a working implementation of POSIX threads </li></ul><ul><li>Thread support is needed for the server . To compile only the client code, the only requirement is a C++ compiler </li></ul><ul><li>“ We use and develop the software ourselves primarily on Linux (SuSE and Red Hat), FreeBSD, and Sun Solaris (Versions 8 and 9)” </li></ul>
  6. 6. Operating Systems Supported by MySQL <ul><li>MySQL has been reported to compile successfully on the following operating systems: </li></ul><ul><ul><li>AIX 4.x, 5.x </li></ul></ul><ul><ul><li>Amiga </li></ul></ul><ul><ul><li>BSDI 2.x </li></ul></ul><ul><ul><li>Digital Unix 4.x </li></ul></ul><ul><ul><li>FreeBSD 2.x, 3.x, 4.x </li></ul></ul><ul><ul><li>Mac OS X </li></ul></ul><ul><ul><li>Windows 9x, Me, NT, 2000, XP, and 2003 </li></ul></ul>
  7. 7. Installing MySQL on Windows <ul><li>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 </li></ul><ul><li>The installer for the Windows version of MySQL 5.0, combined with a GUI Configuration Wizard , automatically installs MySQL, creates an option file, starts the server, and secures the default user accounts </li></ul>
  8. 8. Installing MySQL on Windows <ul><li>MySQL 5.0 for Windows is available in three distribution formats: </li></ul><ul><ul><li>The binary distribution contains a setup program that installs everything you need so that you can start the server immediately </li></ul></ul><ul><ul><li>The source distribution contains all the code and support files for building the executables using the Visual Studio 2003 compiler system </li></ul></ul><ul><li>Generally speaking, you should use the binary distribution. It is simpler to use than the others, and you need no additional tools to get MySQL up and running </li></ul>
  9. 9. Installing MySQL on Windows <ul><li>A copy of the MySQL binary distribution for Windows, which can be downloaded from http:// / downloads / </li></ul><ul><li>You may also have the following optional requirements: </li></ul><ul><ul><li>If you plan to connect to the MySQL server via ODBC, you also need a Connector/ODBC driver </li></ul></ul><ul><ul><li>If you need tables with a size larger than 4GB, install MySQL on an NTFS or newer filesystem </li></ul></ul>
  10. 10. <ul><li>For MySQL 5.0, there are three installation packages to choose from when installing MySQL on Windows. The packages are as follows: </li></ul><ul><ul><li>Essential package </li></ul></ul><ul><ul><li>Complete package </li></ul></ul><ul><ul><li>Noinstall archive </li></ul></ul><ul><li>The Essentials package is recommended for most users </li></ul>Installing MySQL on Windows
  11. 11. <ul><li>The following entries are created within the new Start menu section : </li></ul><ul><ul><li>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 password when connecting </li></ul></ul><ul><ul><li>MySQL Server Instance Config Wizard : This is a shortcut to the MySQL Configuration Wizard. Use this shortcut to configure a newly installed server, or to re-configure an existing server </li></ul></ul><ul><ul><li>MySQL Documentation : This is a link to the MySQL server documentation that is stored locally in the MySQL server installation directory. This option is not available when the MySQL server is installed using the Essentials installation package. </li></ul></ul>Installing MySQL on Windows
  12. 12. The Main Features of MySQL <ul><li>Written in C and C++ </li></ul><ul><li>Tested with a broad range of different compilers </li></ul><ul><li>Works on many different platforms </li></ul><ul><li>Uses GNU Automake, Autoconf, and Libtool for portability </li></ul><ul><li>APIs for C, C++, Eiffel, Java, Perl, PHP, Python, Ruby, and Tcl are available </li></ul><ul><li>… </li></ul>
  13. 13. Column Types <ul><li>Many column types: </li></ul><ul><ul><li>signed/unsigned integers 1, 2, 3, 4, and 8 bytes long </li></ul></ul><ul><ul><li>FLOAT, DOUBLE </li></ul></ul><ul><ul><li>CHAR, VARCHAR </li></ul></ul><ul><ul><li>TEXT </li></ul></ul><ul><ul><li>BLOB </li></ul></ul><ul><ul><li>DATE, TIME, DATETIME, TIMESTAMP, YEAR </li></ul></ul><ul><ul><li>SET, ENUM </li></ul></ul><ul><li>Fixed-length and variable-length records </li></ul>
  14. 14. Statements and Functions <ul><li>Full operator and function support in the SELECT and WHERE clauses of queries. </li></ul><ul><li>Full support for SQL GROUP BY and ORDER BY clauses. Support for group functions (COUNT(), COUNT(DISTINCT ...), AVG(), STD(), SUM(), MAX(), MIN() </li></ul><ul><li>Support for LEFT OUTER JOIN and RIGHT OUTER JOIN </li></ul><ul><li>Support for aliases on tables and columns as required by standard SQL </li></ul>
  15. 15. Statements and Functions (2) <ul><li>DELETE, INSERT, REPLACE, and UPDATE return the number of rows that were changed (affected) </li></ul><ul><li>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 </li></ul><ul><li>Function names do not clash with table or column names. For example, ABS is a valid column name. The only restriction is that for a function call, no spaces are allowed between the function name and the ‘ ( ’ that follows it </li></ul><ul><li>You can mix tables from different databases in the same query </li></ul>
  16. 16. Limits <ul><li>Handles large databases. </li></ul><ul><ul><li>“ We use MySQL Server with databases that contain 50 million records …” </li></ul></ul><ul><ul><li>“… We also know of users who use MySQL Server with 60,000 tables and about 5,000,000,000 rows …” </li></ul></ul>
  17. 17. Connectivity <ul><li>Clients can connect to the MySQL server using TCP/IP sockets on any platform </li></ul><ul><li>The Connector/ODBC (MyODBC) interface provides MySQL support for client programs that use ODBC (Open Database Connectivity) connections. </li></ul><ul><ul><li>For example, you can use MS Access to connect to your MySQL server. </li></ul></ul><ul><li>The Connector/J interface provides MySQL support for Java client programs that use JDBC connections </li></ul>
  18. 18. How Big MySQL Tables Can Be <ul><li>MySQL 3.22 had a 4GB (4 gigabyte) limit on table size. </li></ul><ul><li>With the MyISAM storage engine in MySQL 3.23 , the maximum table size was increased to 65536 terabytes (256 7 – 1 bytes). </li></ul><ul><li>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 </li></ul>
  19. 19. <ul><li>The following table lists some examples of operating system file-size limits. This is only a rough guide and is not intended to be definitive </li></ul><ul><ul><li>Win32 (FAT/FAT32) 2GB/4GB </li></ul></ul><ul><ul><li>Win32 (NTFS) 2TB </li></ul></ul><ul><ul><li>Linux 2.2-Intel 2GB </li></ul></ul><ul><ul><li>Linux 2.4+ (ext3) 4TB </li></ul></ul><ul><li>Windows users please note: FAT and VFAT (FAT32) are not considered suitable for production use with MySQL. Use NTFS instead </li></ul>How Big MySQL Tables Can Be
  20. 20. Connecting to and Disconnecting from the Server <ul><li>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 will also need to specify a hostname </li></ul><ul><li>shell> mysql -h host -u user –p </li></ul><ul><li>Enter password: ******** </li></ul>
  21. 21. Connecting to and Disconnecting from the Server <ul><li>shell> mysql -h host -u user –p </li></ul><ul><li>Enter password: ******** </li></ul><ul><li>host and user represent the hostname where your MySQL server is running and the username of your MySQL account </li></ul><ul><li>The ******** represents your password; enter it when mysql displays the Enter password: prompt. </li></ul>
  22. 22. Connecting to and Disconnecting from the Server
  23. 23. Connecting to and Disconnecting from the Server <ul><li>Some MySQL installations allow users to connect as the anonymous ( unnamed ) user to the server running on the local host . </li></ul><ul><li>If this is the case on your machine, you should be able to connect to that server by invoking mysql without any options: </li></ul><ul><li>shell> mysql </li></ul>
  24. 24. Connecting to and Disconnecting from the Server <ul><li>After you have connected successfully, you can disconnect any time by typing QUIT (or EXIT ) at the mysql > prompt: </li></ul><ul><li>shell> QUIT </li></ul><ul><li>Bye </li></ul><ul><li>shell> EXIT </li></ul><ul><li>Bye </li></ul><ul><li>On Unix , you can also disconnect by pressing Control-D </li></ul>
  25. 25. Entering Queries <ul><li>mysql> SELECT VERSION( ), CURRENT_DATE; </li></ul>
  26. 26. Notes <ul><li>When you issue a command, mysql sends it to the server for execution and displays the results , then prints another mysql> prompt to indicate that it is ready for another command. </li></ul>
  27. 27. Notes (2) <ul><li>mysql displays query output in tabular form (rows and columns). The first row contains labels for the columns. The rows following are the query results. </li></ul>
  28. 28. Notes (3) <ul><li>mysql shows how many rows were returned and how long the query took to execute, which gives you a rough idea of server performance </li></ul>
  29. 29. Entering Queries <ul><li>Keywords may be entered in any lettercase. The following queries are equivalent: </li></ul><ul><ul><li>mysql> SELECT VERSION(), CURRENT_DATE; </li></ul></ul><ul><ul><li>mysql> select version(), current_date; </li></ul></ul><ul><ul><li>mysql> SeLeCt vErSiOn(), current_DATE; </li></ul></ul>
  30. 30. <ul><li>Here's another query. It demonstrates that you can use mysql as a simple calculator </li></ul>Entering Queries
  31. 31. <ul><li>You can even enter multiple statements on a single line. Just end each one with a semicolon </li></ul>Entering Queries
  32. 32. <ul><li>A command need not be given all on a single line, so lengthy commands that require several lines are not a problem </li></ul><ul><li>mysql determines where your statement ends by looking for the terminating semicolon, not by looking for the end of the input line </li></ul>Entering Queries
  33. 33. Entering Queries
  34. 34. <ul><li>The following table shows each of the prompts you may see and summarizes what they mean about the state that mysql is in: </li></ul><ul><ul><li>mysql> Ready for new command. </li></ul></ul><ul><ul><li>-> Waiting for next line of multiple-line command </li></ul></ul><ul><ul><li>‘ > Waiting for next line, waiting for completion of </li></ul></ul><ul><ul><li>a string that began with a single quote (‘'’) </li></ul></ul>Entering Queries
  35. 35. <ul><li>&quot;> Waiting for next line, waiting for completion of a </li></ul><ul><li>string that began with a double quote (‘&quot;’) </li></ul><ul><li>`> Waiting for next line, waiting for completion of an </li></ul><ul><li>identifier that began with a backtick (‘`’) </li></ul><ul><li>/*> Waiting for next line, waiting for completion of a comment </li></ul><ul><li>that began with /* </li></ul>Entering Queries
  36. 36. Entering Queries
  37. 37. Entering Queries <ul><li>mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30; </li></ul><ul><li>'> </li></ul>
  38. 38. Creating and Using a Database <ul><li>This section shows you how to: </li></ul><ul><ul><li>Create a database </li></ul></ul><ul><ul><li>Create a table </li></ul></ul><ul><ul><li>Load data into the table </li></ul></ul><ul><ul><li>Retrieve data from the table in various ways </li></ul></ul><ul><ul><li>Use multiple tables </li></ul></ul>
  39. 39. <ul><li>Use the SHOW statement to find out what databases currently exist on the server </li></ul>Creating and Using a Database
  40. 40. <ul><li>If the test database exists, try to access it </li></ul>Creating and Using a Database
  41. 41. <ul><li>USE </li></ul><ul><ul><li>USE, like QUIT, does not require a semicolon </li></ul></ul><ul><ul><li>You can terminate such statements with a semicolon if you like; it does no harm </li></ul></ul><ul><ul><li>It must be given on a single line </li></ul></ul>Creating and Using a Database
  42. 42. Creating and Selecting a Database <ul><li>If the administrator creates your database for you when setting up your permissions, you can begin using it. Otherwise, you need to create it yourself </li></ul>
  43. 43. Creating and Selecting a Database
  44. 44.   Creating a Table <ul><li>Creating the database is the easy part, but at this point it's empty, as SHOW TABLES tells you: </li></ul>
  45. 45.   Creating a Table <ul><li>Creating the database is the easy part, but at this point it's empty, as SHOW TABLES tells you: </li></ul>
  46. 46.   Creating a Table Create table slides (id integer, contents varchar(30), images char);
  47. 47.   Creating a Table mysql> show tables;
  48. 48.   Creating a Table mysql> describe slides ;
  49. 49. Loading Data into a Table <ul><li>After creating your table, you need to populate it. The </li></ul><ul><li>LOAD DATA statement is useful for this </li></ul><ul><li>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 single statement </li></ul>
  50. 50. Loading Data into a Table
  51. 51. Loading Data into a Table mysql> LOAD DATA LOCAL INFILE - > 'C:Documents and SettingsUserDocumentiDidatticaceriMysqldati ‘ > slides.txt' INTO TABLE slides;
  52. 52. Loading Data into a Table mysql> LOAD DATA LOCAL INFILE - > 'C:Documents and SettingsUserDocumentiDidatticaceriMysqldati ‘ > slides.txt' INTO TABLE slides;
  53. 53. Retrieving Information from a Table mysql> select * from slides ;
  54. 54. Retrieving Information from a Table mysql> select contents from slides ;
  55. 55. Retrieving Information from a Table mysql> delete from slides;
  56. 56. Retrieving Information from a Table mysql> select * from slides;
  57. 57. Source
  58. 58. Source