MySQL Notes


MySQL Documentation
The main source for documentation is online at http://www.mysql.com/documentation. If you're interested in other aspects of MySQL (downloading, etc.), you'll want to explore the rest of the site as well.

For those of you who like having a hardcopy book in hand, here are a couple of recommendations:
Yarger, R. et al.,(1999). MySQL & mSQL, O'Reilly.
DuBois, P. (2000). MySQL, New Riders.

Accounts
If you already have a MySQL account, you can use it for the workshop and assignment. Otherwise, you need an account on Ruby, and a MySQL account. We'll hand out accounts in class (see home page).

Note that you must first connect to ruby (through SSH) in order to then connect to the MySQL server on opal.

To change your password, log into your account and issue the following command (the parenthesis and single quotes are required):
SET PASSWORD FOR YourAccountName = PASSWORD ('NewPassword');

Connecting to MySQL at SILS
There are three useful ways of connecting to MySQL.

  1. Connecting for interactive work.
    shell> mysql -u YourAccountName -p -h opal.ils.unc.edu YourDatabaseName
    You will then be prompted for your password.

  2. Executing a script.
    shell> mysql -u YourAccountName -p -h opal.ils.unc.edu YourDatabaseName < YourScriptFile
    You will then be prompted for your password.
    A script file is useful for:

  3. Executing a script and directing the output into a file.
    shell> mysql -u YourAccountName -p -h opal.ils.unc.edu YourDatabaseName < YourScriptFile >YourOutputFile

Demo Database
We will setup and use demonstration databases that go along with the tables for the Ramamkrishnan chapter 5 section 3 exercises, as this will help you with your assignments. You should make sure you can create tables, and populate them by scripted methods (i.e. not typing them all in by hand live!). As part of exercise 2 we will

Helpful Hints
A miscellaneous collection. If you know of other useful tidbits, or discover something useful for your classmates, please let me know so I can add it!

  1. Some useful commands: Consult the documentation for details on these commands.
  2. Remember the semi-colon! MySQL won't execute a command unless it ends with a semi-colon (;) or \g. It will just hang around and wait for you.
  3. Check out the enum data type. It lets you define a list of possible values.
  4. The version of MySQL we are using (3.23.39) does not support referential integrity and foreign keys. You will have to provide your own work-arounds when necessary. 3.5.6 Using Foreign Keys of the MySQL Documentation talks about some workarounds.
  5. Our version of MySQL doesn't support transaction processing, either. Again, you can read about some workarounds in the documentation. Think about the implications of this during the classes on transaction processing.
  6. MySQL doesn't support nested queries. So instead of writing a nested query like so:
    SELECT teacher.fname, teacher.lname
    FROM teacher
    WHERE PID
    in
    (SELECT course.teacher
    FROM course
    WHERE course.cnum > 199);

    you write

    SELECT teacher.fname, teacher.lname
    FROM teacher, course
    WHERE teacher.PID = course.teacher AND
    course.cnum > 199;

  7. For two-part queries, where you want to perform a second query on the results of the first, look at create temporary table.
  8. MySQL has extended the functionality of the GROUP BY clause. You'll want to look at this in the documentation, since there is some unexpected behavior when combining this with max().

This page was created by Bradley Hemminger, Kristin Chaffin, Stephanie Haas. Address questions and comments about this page to Brad Hemminger . © Bradley Hemminger 2003-2005. All rights reserved.