shell> mysql –help
Connect to Server
shell> mysql -h host -u user -p
Enter password: ********
Disconnect from the Server
mysql> QUIT
On Unix, you can also disconnect by pressing Control-D.
Create Database
mysql> CREATE DATABASE menagerie;
Creating a database does not select it for use; you must do that explicitly. To make menagerie the current database, use this command:
mysql> USE menagerie
Database changed
Use a CREATE TABLE statement to specify the layout of your table:
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
Once you have created a table, SHOW TABLES should produce some output:
mysql> SHOW TABLES;
To verify that your table was created the way you expected, use a DESCRIBE statement:
mysql> DESCRIBE pet;
After creating your table, you need to populate it. The LOAD DATA and INSERT statements are useful for this.
To load the text file pet.txt into the pet table, use this statement:
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;
If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead:
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet
-> LINES TERMINATED BY '\r\n';
mysql> SELECT VERSION(), CURRENT_DATE;
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;
It demonstrates that you can use mysql as a simple calculator:
mysql> SELECT SIN(PI()/4), (4+1)*5;
mysql> SELECT VERSION(); SELECT NOW();
Here is a simple multiple-line statement:
mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
If you decide you do not want to execute a command that you are in the process of entering, cancel it by typing \c:
mysql> SELECT
-> USER()
-> \c
The simplest form of SELECT retrieves everything from a table:
mysql> SELECT * FROM pet;
mysql> SELECT DATABASE();
mysql> SHOW TABLES;
mysql> DESCRIBE pet;
mysql> SELECT USER()