The MySQL command interpreter is commonly used to create databases and tables in web database applications and to test queries. Throughout the remainder of this chapter we discuss the SQL statements for managing a database. All these statements can be directly entered into the command interpreter and executed. In later chapters, we'll show how to include SQL statements in PHP scripts so that web applications can get and change data in a database. Once the MySQL server is running, the command interpreter can be used. The command interpreter can be run using the following command from the shell in a Unix or Mac OS X system, assuming you've created a user hugh with a password shhh: % /usr/local/bin/mysql -uhugh -pshhh The shell prompt is represented here as a percentage character, %. On a Microsoft Windows platform, you can access the command interpreter by clicking on the Start menu, then the Run option, and typing into the dialog box: "C:\Program Files\EasyPHP1-7\mysql\bin\mysql.exe" -uhugh -pshhh Then, press the Enter key or click OK. (For both Unix and Microsoft Windows environments, we're assuming you've installed MySQL in the default directory location using our instructions in Appendix A through Appendix C.) Running the command interpreter displays the output: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 to server version: 4.0.15-log Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> The command interpreter displays a mysql> prompt and, after executing any command or statement, it redisplays the prompt. For example, you might issue the statement: mysql> SELECT NOW( ); This statement reports the time and date in the following output: +---------------------+ | NOW( ) | +---------------------+ | 2004-03-01 13:48:07 | +---------------------+ 1 row in set (0.00 sec) mysql> After running a statement, the interpreter redisplays the mysql> prompt. We discuss the SELECT statement later in this chapter. As with all other SQL statements, the SELECT statement ends in a semicolon. Almost all SQL command interpreters permit any amount of whitespace (spaces, tabs, or carriage returns) in SQL statements, and they check syntax and execute statements only after encountering a semicolon that is followed by a press of the Enter key. We have used uppercase for the SQL statements throughout this book so that it's clear what's an SQL statement and what isn't. However, any mix of upper- and lowercase is equivalent in SQL keywords. Be careful, though: other parts of SQL statements such as database and table names are case sensitive. You also need to be careful with values: for example, Smith, SMITH, and smith are all different. On startup, the command interpreter encourages the use of the help command. Typing help produces a list of commands that are native to the MySQL interpreter and that aren't part of SQL. All non-SQL commands can be entered without the terminating semicolon, but the semicolon can be included without causing an error. The MySQL command interpreter provides a lot of flexibility and many shortcuts:
|