[Image of Linux]
Linux's Witness Ministry
The Personal Web Pages of Chris X. Edwards

Xed's MySQL Notes

--------------------------
Purpose: List databases that the MySQL server is storing.
Command: [xed@cardinal]$ mysqlshow
Produces: Produces:
+-----------+
| Databases |
+-----------+
| mysql     |
| pvs       |
| test      |
+-----------+

Purpose: Create a new database on the server.
Command: [xed@cardinal]$ mysqladmin -u root -p create test_db

Purpose: Delete an entire database from the server. This can be very serious!
Command: [xed@cardinal]$ mysqladmin -u root -p drop test_db
Produces:
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
 
 Do you really want to drop the 'test_db' database [y/N]

Purpose: Enter interactive mysql client. The -u is the database username, not the system username. The -p says that you want to enter a password. Just using root with no password (again, just for database operations, not system authentication) is often fine for casual work. Just hit enter at the password prompt.
Command: [xed@cardinal]$ mysql -u root -p
Produces:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 519 to server version: 3.23.54
Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 
mysql>

Purpose: Select a particular database to work with.
Command: mysql> use lame_excuses
Produces:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
 Database changed

Purpose: Display a list of the current database's tables.
Command: mysql> SHOW TABLES;
Produces:
+---------------+
| Tables_in_pvs |
+---------------+
| category      |
| project       |
+---------------+
6 rows in set (0.00 sec)

Purpose: Create a new table in the current database.
Command: mysql> CREATE TABLE category ( c_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20) );

Purpose: Print a summary of the fields in a particular table.
Command: mysql> DESCRIBE category;
Produces:
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| c_id  | int(11)     |      | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

Purpose: Delete an entire table from the current database.
Command: mysql> DROP TABLE category;

Purpose: Rename a MySQL database. This is a multistep process using a collection of smaller steps.
Command:
[xed@cardinal]$ mysqldump -u root -p old_db > old_db.sql
[xed@cardinal]$ mysql -u root -p
mysql> CREATE DATABASE new_db;
mysql> USE new_db;
mysql> SOURCE ./old_db.sql;
[xed@cardinal]$ mysqladmin -u root -p drop old_db

Purpose: Add a record to a table.
Command: mysql> INSERT INTO excuses VALUES ( NULL, "eaten by dog" );

Purpose: Return all records. This is the general format of the SELECT.
Command: mysql> SELECT * FROM excuses;
Produces:
+------+---------------+
| c_id | name          |
+------+---------------+
|    1 | eaten by dog  |
|    2 | someone else  |
+------+---------------+

Purpose: Change the name of a table.
Command: mysql> ALTER TABLE excuses RENAME TO excuse_list;

Purpose: Add a column to a table.
Command: mysql> ALTER TABLE excuse_list ADD COLUMN (times_used INT);

Purpose: Delete some records.
Command: mysql> DELETE FROM excuse_list WHERE c_id=4;

Purpose: Make arbitary changes to records.
Command: mysql> UPDATE excuse_list SET name='some other guy' WHERE c_id=4;
--------------------------
Return to LWM
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ January 2005