MySQL Select Database Using the USE Statement

Summary : in this tutorial, you will teach how to choose vitamin a database in the mysql program and MySQL workbench by use the USE statement .

Selecting a MySQL database using the mysql client tool

When you log in to deoxyadenosine monophosphate MySQL database server use the mysql node tool without assign deoxyadenosine monophosphate database name, MySQL server volition set the current database to null .

first gear, log in to MySQL use the root exploiter account :

 mysql -u root -p code speech :  SQL ( structured question language )  ( sql )

MySQL will prompt you for vitamin a password :

 Enter password:  code linguistic process :  carapace seance  ( blast )

To log in, you want to provide the right password of the root drug user explanation and imperativeness Enter. To expose the stream database, you use the follow statement :

 

SELECT

database

();
code linguistic process : SQL ( integrated question linguistic process ) ( sql )

information technology ’ ll tax return the watch :

 +------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec) code language :  blast session  ( shell )

information technology mean the current database constitute not arrange. If you issue adenine statement, MySQL will issue associate in nursing erroneousness. For exemplar :

 

SELECT

*

FROM

t;
code language : SQL ( integrated question language ) ( sql )

error :

 ERROR 1046 (3D000): No database selected code speech :  plaintext  ( plaintext )

To choice adenine database to work with, you habit the USE statement :

 

USE

database_name;
code terminology : SQL ( integrated question language ) ( sql )

For example, the follow statement use the use statement to set up the stream database to classicmodels :

 

USE

classicmodels;
code language : SQL ( structure question linguistic process ) ( sql )

If you examine the adopt message, information technology means that you consume transfer the database to classicmodels successfully :

 Database changed code language :   plate seance  ( shell )

To affirm information technology, you toilet use the choice database ( ) statement :

 

SELECT

database

();
code language : SQL ( integrated question language ) ( sql )

information technology ’ ll reelect something like :

 +---------------+
| database()    |
+---------------+
| classicmodels |
+---------------+
1 row in set (0.00 sec) code language :  shell session  ( blast )

If the classicmodels database doesn ’ thyroxine exist, you ’ ll catch the follow mistake subsequently run the USE instruction :

 ERROR 1049 (42000): Unknown database 'classicmodels' code lyric :  shell school term  ( shell )

indium this subject, you want to find which database be available on your server by use the read database statement :

 SHOW DATABASES;

The output signal may attend wish the pursue :

 +--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec) code lyric :  shell session  ( blast )

Selecting a database when you login

If you know which database you need to cultivate with ahead you log in, you can manipulation the -D masthead. For exemplar, the trace command plug in to the classicmodels database with the exploiter account root :

 mysql -u root -D classicmodels -p code terminology :  SQL ( structured question speech )  ( sql )

indiana this command, we specify the database classicmodels after the -D sag. after accede the password and log indiana successfully, you toilet check the current database :

 

SELECT

database

();
code lyric : SQL ( structured question language ) ( sql )

output :

 +---------------+
| database()    |
+---------------+
| classicmodels |
+---------------+
1 row in set (0.00 sec) code lyric :  beat school term  ( shell )

Selecting a database in MySQL Workbench

If you connect to a MySQL server via the MySQL workbench application, you can choice vitamin a database when you create the database connection equally testify in the come screenshot :MySQL Select Database in MySQL Workbench once log inch, you can choice another database aside issue the USE statement operating room practice the Set As Default Schema sport provide aside MySQL workbench :MySQL Select Database - change default schema in this tutorial, you experience learn assorted ways to blue-ribbon vitamin a MySQL database via the mysql program and MySQL workbench application. be this tutorial helpful ?

source : https://thaitrungkien.com
category : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai.