IBM DB2 IBM DB2 is a commercial DBMS that sup- ports very large databases and numbers of transactions. It runs on many operating sys- tems and hardware platforms and is com- plex enough to require a full-time database administrator (DBA) to run and maintain it. Learn about DB2 products at www.ibm.com/db2 and download a free 90-day trial copy of DB2 or a (permanently) free copy of IBM DB2 Express-C. This book covers DB2 9.5 but also includes tips for earlier versions, back to 8.0. To deter- mine which version of DB2 you’re running, run the DB2 command-line command db2level or run the query SELECT service_level FROM SYSIBMADM.ENV_INST_INFO; . To run SQL programs, use the Command Center graphical tool in DB2 8.x, the Command Editor graphical tool in DB2 9.x, or the db2 command-line processor (CLP). 20 Chapter 1 IBM DB2 To use Command Center (DB2 8.x): 1. Start Command Center. This procedure varies by platform. In Windows, for example, choose Start > All Programs > IBM DB2 > Command Line Tools > Command Center. In Unix and Linux, use the command db2cctr . Alternatively, if Control Center is open, choose Tools > Command Center or click . 2. Choose the command-type SQL Statements and DB2 CLP Commands. 3. Click the Interactive tab. 4. In the Database Connection box, click , expand the object tree until you find the Databases folder, select a database, and then click OK. 5. To run SQL interactively, in the Command box, type or paste an SQL statement (Figure 1.27). or To run an SQL script, click the Scripts tab, choose Script > Import, navigate to and select the script file, and then click OK. 6. Click or press Ctrl+Enter. or Choose Interactive > Execute or Script > Execute. Interactive-SQL results appear in the Results tab (Figure 1.28). SQL-script results appear in the box below the Script box. 21 DBMS Specifics IBM DB2 Figure 1.27 A ready-to-run SELECT statement in Command Center. Figure 1.28 The results of the SELECT statement. To use Command Editor (DB2 9.x): 1. Start Command Editor. This procedure varies by platform. In Windows, for example, choose Start > All Programs > IBM DB2 > [db2_copy_name] > Command Line Tools > Command Editor. In Unix and Linux, use the command db2ce . Alternatively, if Control Center is open, choose Tools > Command Editor or click . 2. On the Commands tab, choose a data- base in the Target list. If the desired database doesn’t appear in the Target list, click Add (to the right of the list), choose a database, and then click OK. 3. To run SQL interactively, type or paste an SQL statement in the top box of the Commands tab (Figure 1.29). or To run an SQL script, choose Selected > Open (or press Ctrl+O), navigate to and select the script file, and then click OK. 4. Click or press Ctrl+Enter. or Choose Selected > Execute. Command Editor displays the results in the Query Results tab (Figure 1.30). The bottom box of the Commands tab shows query-processing information. 22 Chapter 1 IBM DB2 Figure 1.29 A ready-to-run SELECT statement in Command Editor. Figure 1.30 The results of the SELECT statement. For technical reasons involving parent and child processes, (only) Microsoft Windows users must start the db2 command-line processor with a special preliminary step. To start the db2 command-line processor in Windows: ◆ At a command prompt, type db2cmd and then press Enter. or For DB2 8.x, choose Start > All Programs > IBM DB2 > Command Line Tools > Command Window. or For DB2 9.x, choose Start > All Programs > IBM DB2 > [db2_copy_name] > Command Line Tools > Command Window. A new DB2 CLP command-prompt win- dow appears. ✔ Tips ■ You must use the DB2 CLP window for all db2 commands (described next). If you try to run db2 at a regular Windows command prompt, DB2 responds with the error “Command line environment not initialized.” ■ If you launch a new DB2 CLP window via the db2cmd command, you can close the original command-prompt window. ■ In the DB2 CLP window, change ( cd ) your working directory if necessary before you run db2 commands. 23 DBMS Specifics IBM DB2 To use the db2 command-line processor interactively: 1. At a command prompt, type db2 -t and then press Enter. The -t option tells db2 that a semicolon (;) terminates statements. The db2 => prompt appears. 2. At the db2 prompt, type connect to dbname ; and then press Enter. dbname is the name of the database to use. 3. Type an SQL statement. The statement can span multiple lines. Terminate it with a semicolon and then press Enter to display the results (Figure 1.31). ✔ Tip ■ Alternatively, you can avoid the db2 => prompt by typing commands and SQL statements right on the command line. For example: db2 connect to books db2 SELECT * FROM authors If you omit the -t option, as here, don’t terminate commands and SQL state- ments with a semicolon. 24 Chapter 1 IBM DB2 Figure 1.31 The same SELECT statement in db2 interactive mode. 25 DBMS Specifics IBM DB2 To use the db2 command-line processor in script mode: 1. At a command prompt, type: db2 connect to dbname dbname is the name of the database to use. 2. At a command prompt, type: db2 -t -f sql_script sql_script is a text file containing SQL statement(s) and can include an absolute or relative pathname. The -t option tells db2 that a semicolon (;) terminates statements. 3. Press Enter to display the results (Figure 1.32). ✔ Tips ■ In step 2, add the option -v if you want to echo the contents of sql_script in the output. ■ An alternative script tool is db2batch . Figure 1.32 The same SELECT statement in db2 script mode. To exit the db2 command-line tool: ◆ At the db2 prompt, type quit; and then press Enter. (Omit the semicolon if you didn’t use the -t option when you started db2 .) To show db2 command-line options: ◆ At a command prompt, type db2 ? and then press Enter. This command displays a few pages that speed by quickly. To view one page at a time, instead type db2 ? | more and then press Enter. Tap the spacebar to advance pages (Figure 1.33). ✔ Tips ■ To get help while you’re at a db2 => prompt, type ?; and then press Enter. (Omit the semicolon if you didn’t use the -t option when you started db2 .) ■ DB2 for Linux, Unix, and Windows (“LUW” in IBM-speak) differs somewhat from DB2 on other platforms. This sec- tion covers LUW DB2. If you’re running non-LUW DB2 (on z/OS or OS/390, for example), your commands might differ. 26 Chapter 1 IBM DB2 Figure 1.33 The db2 help screen. MySQL MySQL (pronounced my-es-kyoo-el) is an open-source DBMS that supports large data- bases and numbers of transactions. MySQL is known for its speed and ease of use. It’s free for personal use and runs on many operating systems and hardware platforms. You can download it at www.mysql.com . This book covers MySQL 5.1 but also includes tips for earlier versions, back to 4.0. To determine which version of MySQL you’re running, run the MySQL command- line command mysql -V or run the query SELECT VERSION(); . To run SQL programs, use the mysql command-line tool. ✔ Tip ■ To open a command prompt in Windows, choose Start > All Programs > Accessories > Command Prompt. To use the mysql command-line tool interactively: 1. At a command prompt, type: mysql -h host -u user -p dbname host is the host name, user is your MySQL user name, and dbname is the name of the database to use. MySQL will prompt you for your password (for a passwordless user, either omit the -p option or press Enter at the password prompt). 2. Type an SQL statement. The statement can span multiple lines. Terminate it with a semicolon (;) and then press Enter to display the results (Figure 1.34). 27 DBMS Specifics MySQL Figure 1.34 The results of a SELECT statement in mysql interactive mode. To use the mysql command-line tool in script mode: 1. At a command prompt, type: mysql -h host -u user -p -t ➞ dbname < sql_script host is the host name, user is your MySQL user name, and dbname is the name of the database to use. MySQL will prompt you for your password (for a passwordless user, either omit the -p option or press Enter at the password prompt). The -t option formats the results as a table; omit this option if you want tab-delimited output. dbname is the name of the database to use. The < redirection operator reads from the file sql_script, which is a text file containing SQL statement(s) and can include an absolute or relative pathname. 2. Press Enter to display the results (Figure 1.35). 28 Chapter 1 MySQL Figure 1.35 The same SELECT statement in mysql script mode. To exit the mysql command-line tool: ◆ Type quit or \q and then press Enter. To show mysql command-line options: ◆ At a command prompt, type mysql -? and then press Enter. This command displays a few pages that speed by. To view one page at a time, type mysql -? | more and then press Enter. Tap the spacebar to advance pages (Figure 1.36). ✔ Tips ■ If MySQL is running on a remote net- work computer, ask your database administrator (DBA) for the connection parameters. If you’re running MySQL locally (that is, on your own computer), then set host to localhost , set user to root , and use the password you assigned to root when you set up MySQL. ■ As an alternative to the command prompt, you can use the graphical tools at www.mysql.com/products/tools . ■ You can learn more about open-source software at www.opensource.org . 29 DBMS Specifics MySQL Figure 1.36 The mysql help screen. . www.mysql.com . This book covers MySQL 5.1 but also includes tips for earlier versions, back to 4.0. To determine which version of MySQL you’re running, run the MySQL command- line command mysql. SELECT statement in mysql interactive mode. To use the mysql command-line tool in script mode: 1. At a command prompt, type: mysql -h host -u user -p -t ➞ dbname < sql_ script host is the. SELECT statement in mysql script mode. To exit the mysql command-line tool: ◆ Type quit or q and then press Enter. To show mysql command-line options: ◆ At a command prompt, type mysql -? and then