MySQL basic commands

Default featured post

MySQL database is tightly coupled with Linux and in many extends it cannot be separated from Linux in my point of view.

In this post I want to describe about some basic commands of MySQL. Note that MySQL also has GUI in Linux but I prefer to work in command and write command instead of using GUI which sometimes is faster than GUI especially when batch/bulk operation(s) need to be applied.

Firstly make sure that the MySQL packages (MySQL client and server) are installed and configured in your system properly. In Ubuntu you can install MySQL client and server with following commands.

$ sudo apt-get install mysql-client-core-5.5

$ sudo apt-get install mysql-client-5.5

$ sudo apt-get install mysql-server

The first thing is to connect to your MySQL server. So you need to type the following command.

$ mysql -u [Username Mostly root] -p

After running this command the system will ask the password, so you need to enter the password which was asked on installation time of MySQL server.

After entering password you will be redirected to MySQL and the command prompt will be changed from either “$” or “#” to “mysql>”.

In order to see available databases type the following command.

mysql> SHOW DATABASES;

Be carful about semicolon.

For creating new database you can use the below command

mysql> CREATE DATABASE X;

For using a specific database you can use

mysql> USE X;

Now you can create/remove tables of data base. The explanation of creating tables and some queries is available here. In the mentioned post the description of altering tables, feilds, and many things can be found. Therefore, I do not explain them again, you can use all of those queries here as well.

For showing available table you can use

mysql> SHOW TABLES;

For dropping a table you can use drop table like

mysql> DROP TABLE mytable;

For removing a database you can use following command

mysql> DROP DATABASE X;

Finally for exiting mysql and returning back to command prompt type “EXIT;”.

Hint: Always avoid to alter or remove default databases and instead create test database and after finishing your work remove it.

More information available in the MySQL handbook which can free download from here.