How To Create & Delete and Import & Export Databases in MySQL or MariaDB use command

Samsul Hoque
0

Open the MySQL Command Line.
 In order to delete a database in MySQL, you'll need to use the MySQL command line from your computer's Command Prompt (Windows) or Terminal (Mac) program.


Exporting the Database

The mysqldump console utility is used to export databases to SQL text files, making it relatively easy to transfer and move around. You will need the database name itself as well as the username and password to an account with privileges allowing at least full read only access to the database.
Export your database using the following command structure:

mysqldump -u username -p database_name > data-dump.sql
  • username is the username you can log in to the database with
  • database_name is the name of the database that will be exported
  • data-dump.sql is the file in the current directory that the output will be saved to

Create & Importing the Database

You will have to create the new database. This is where the contents of the dump file will be imported.
First, log in to the database as root or another user with sufficient privileges to create new databases:

mysql -u root -p

This will bring you into the MySQL shell prompt. Next, create a new database with the following command. In this example, the new database is called new_database:

CREATE DATABASE new_database;
You'll see this output confirming that it was created.

Output
Query OK, 1 row affected (0.00 sec)
Then exit the MySQL shell by pressing CTRL+D. From the normal command line, you can import the dump file with the following command:

mysql -u username -p new_database < data-dump.sql
  • username is the username you can log in to the database with
  • newdatabase is the name of the freshly created database
  • data-dump.sql is the data dump file to be imported, located in the current directory

Delete the Database

Enter the login command. Type in the following, then press  Enter

mysql -u root -p
  • If you don't have access to the root account, enter your own username in place of "root". This must be an account that has read/write privileges.
Enter your password when prompted. Type in the password you use to log into MySQL, then press  Enter.

View a list of your databases. Once MySQL opens, type in the following command and press  Enter to see a list of your MySQL databases: 

SHOW DATABASES;
Delete the database. Type in DROP DATABASE name; where name is your database's name, then press  Enter. For example, to delete a database named "Flowers", you would enter the following 

DROP DATABASE Sample_db;




Post a Comment

0Comments
Post a Comment (0)