How do i select a database in mysql terminal?


Once you get connected with the MySQL server, it is required to select a database to work with. This is because there might be more than one database available with the MySQL Server.

Selecting MySQL Database from the Command Prompt

It is very simple to select a database from the mysql> prompt. You can use the SQL command use to select a database.

Example

Here is an example to select a database called TUTORIALS

[root@host]# mysql -u root -p
Enter password:******
mysql> use TUTORIALS;
Database changed
mysql> 

Now, you have selected the TUTORIALS database and all the subsequent operations will be performed on the TUTORIALS database.

NOTE − All the database names, table names, table fields name are case sensitive. So you would have to use the proper names while giving any SQL command.

Selecting a MySQL Database Using PHP Script

PHP uses mysqli_select_db function to select the database on which queries are to be performed. This function takes two parameters and returns TRUE on success or FALSE on failure.

Syntax

mysqli_select_db ( mysqli $link , string $dbname ) : bool
Sr.No.Parameter & Description
1

$link

Required - A link identifier returned by mysqli_connect() or mysqli_init().

2

$dbname

Required - Name of the database to be connected.

Example

Try the following example to select a database −

Copy and paste the following example as mysql_example.php −

<html>
   <head>
      <title>Selecting MySQL Database</title>
   </head>
   <body>
   <?php
      $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = 'root@123';
      $conn = mysqli_connect($dbhost, $dbuser, $dbpass);

      if(! $conn ) {
         die('Could not connect: ' . mysqli_error($conn));
      }
      echo 'Connected successfully<br />';
      $retval = mysqli_select_db( $conn, 'TUTORIALS' );
      if(! $retval ) {
         die('Could not select database: ' . mysqli_error($conn));
      }
      echo "Database TUTORIALS selected successfully\n";
      mysqli_close($conn);
   ?>
   </body>
</html>

Output

Access the mysql_example.php deployed on apache web server and verify the output.

Database TUTORIALS selected successfully

I've managed to get into MySQL using the command line terminal, but when I tried to enter some SQL, it said 'no database selected'

how do I select a database? my database name is: photogallery

What code do I use to select it?

How do i select a database in mysql terminal?

codeforester

35.7k16 gold badges102 silver badges126 bronze badges

asked Mar 13, 2011 at 2:36

Use USE. This will enable you to select the database.

USE photogallery;

12.8.4: USE Syntax

You can also specify the database you want when connecting:

$ mysql -u user -p photogallery

answered Mar 13, 2011 at 2:37

Andrew MooreAndrew Moore

91.6k30 gold badges161 silver badges175 bronze badges

1

While invoking the mysql CLI, you can specify the database name through the -D option. From mysql --help:

-D, --database=name Database to use.

I use this command:

mysql -h <db_host> -u <user> -D <db_name> -p

answered Jul 2, 2020 at 23:47

How do i select a database in mysql terminal?

codeforestercodeforester

35.7k16 gold badges102 silver badges126 bronze badges

2

Switch to a database.

mysql> use [db name];

MySQL Commands

answered Mar 13, 2011 at 2:38

skazskaz

21.5k19 gold badges67 silver badges97 bronze badges

0

Hope this helps.

use [YOUR_DB_NAME];

How do i select a database in mysql terminal?

answered Mar 13, 2011 at 2:37

0

Alternatively, you can give the "full location" to the database in your queries a la:

SELECT photo_id FROM [my database name].photogallery;

If using one more often than others, use USE. Even if you do, you can still use the database.table syntax.

answered Jul 9, 2013 at 17:08

Nick TNick T

24.6k11 gold badges77 silver badges118 bronze badges

Use the following steps to select the database:

mysql -u username -p

it will prompt for password, Please enter password. Now list all the databases

show databases;

select the database which you want to select using the command:

use databaseName;

select data from any table:

select * from tableName limit 10;

You can select your database using the command use photogallery; Thanks !

answered Jul 13, 2017 at 5:42

USE database_name;

eg. if your database's name is gregs_list, then it will be like this >>

USE gregs_list;

How do i select a database in mysql terminal?

smonff

3,2983 gold badges39 silver badges46 bronze badges

answered Jun 26, 2020 at 5:41

How do I select a specific database in MySQL?

You can use the SQL command use to select a database..
Example. Here is an example to select a database called TUTORIALS − [root@host]# mysql -u root -p Enter password:****** mysql> use TUTORIALS; Database changed mysql> ... .
Syntax. mysqli_select_db ( mysqli $link , string $dbname ) : bool. ... .
Example. ... .
Output..

How do I switch databases in MySQL?

You have to indicate it with the USE command. The USE command is also used when you have more than one database on a MySQL server and need to switch between them. You must choose the correct database each time you start a MySQL session.

How do you access a MySQL database?

To connect to MySQL Server:.
Locate the MySQL Command-Line Client. ... .
Run the client. ... .
Enter your password. ... .
Get a list of databases. ... .
Create a database. ... .
Select the database you want to use. ... .
Create a table and insert data. ... .
Finish working with the MySQL Command-Line Client..

How do I select a specific database in SQL?

SELECT Syntax.
SELECT column1, column2, ... FROM table_name;.
SELECT * FROM table_name;.
Example. SELECT CustomerName, City FROM Customers;.
Example. SELECT * FROM Customers;.