When we work with different databases based on different requirements, we need to select the database before running any SQL command. The SQL command is then run post selection of database. Let us try to understand how to select a database in different Databases.
- SQL Server
MySQL Select Database
In order to perform any operation on MySQL, it is important to select a database. Selection of a database from the mysql> prompt is done using SQL use command.
We will look into an example to understand this better.
Copy[root@host]# mysql -u root -p Enter password:****** mysql> use TESTDB; Database changed mysql>
In the above example, we have selected TESTDB as the database and the subsequent operations will be performed on the same database.
We can select the database (schema) in MySQL workbench by selecting it in the left-hand side panel.
In the above image, world schema is selected.
PostgreSQL Select Database
PostgreSQL allows selecting database in different ways. If we are using pgAdmin, we can just double click on the database and it will automatically select the database and will prompt for a password.
If we are using psql command-line client we can use the following command.
Copypostgres=# \c testdb; psql (9.2.4) Type "help" for help. You are now connected to database "testdb" as user "postgres". testdb=#
In the above command, we are connecting to the testdb database.
For the UI part of PostgreSQL, we will be using pgAdmin, for selecting any database in pgAdmin, just double click on the database.
SQL Server Select Database
In order to select a database in SQL server, please follow the below-mentioned steps.
- Connect to the database.
- On the left-hand side Object Explorer, expand the databases folder and double click on the database you want to select.
Above mentioned are some ways to select the database. It is very important to select a database before performing any operation as otherwise, it might result in issues due to incorrect selection of the database.