Once you login to Mysql, you need to select a database to create tables and other objects.
You cannot create tables without selecting a database to work on.
mysql> create table dump(test varchar(10))
-> ;
ERROR 1046 (3D000): No database selected

You can create your own database by using the CREATE command
mysql> create database test;
Query OK, 1 row affected (0.00 sec)


OR
select any existing database and use it.

To view the list of Databases in your system.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| anand |
| mysql |
| phpmyadmin |
| test |
| wordpress |
+--------------------+
6 rows in set (0.00 sec)

To use any of the databases, use the USE command :
mysql> use test;
Database changed

Now you can perform any SQL operation in this database.

To start with, CREATE the table ‘dump’ :
mysql> create table dump(test varchar(10));
Query OK, 0 rows affected (0.00 sec)
mysql> select * from dump;
Empty set (0.00 sec)

mysql> desc dump;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| test | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

To drop a database use the DROP command.

mysql> drop database test;
Query OK, 1 row affected (0.00 sec)

Now see the list of databases :
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| anand |
| mysql |
| phpmyadmin |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)

You need to select the database every time you begin your mysql session and in UNIX, tables and databases names are case sensitive i.e Test is different from test which is different from TEST….

Related posts:

  1. MySQL : Show all tables in a database
  2. MySQL : G, g or ; to end a query

Leave a Reply

*