We can delete an existing database in MySQL using the Drop database
statement.
Drop a database in MySQL using command-line
- Login into the command-line using your credentials.
- Type the command:
show databases;
To see all the databases available.
- To drop a particular database, type the command
drop database <database-name>;
For example:
drop database ccapp;
- If a database does not exist and we try to drop it, MySQL throws an error shown below,
Example:
drop database somedb;
- To avoid this error, we can use the syntax: This command is used to check the database first if it exists then delete the database otherwise it will throw a warning if the database doesn't exist.
drop database if exists <database-name>;
Example:
drop database if exists somedb;
Using MySQL Workbench
- Login into the MySQL Workbench using your credentials.
- Type the command:
show databases;
- To drop a particular database, type the command
drop database <database-name>;
Example:
drop database somedb2;
- If a database does not exist and we try to drop it, MySQL throws an error.
Example:
drop database practicedb;
- To avoid this error, we can use the syntax:
drop database if exists <database-name>;
Example:
drop database if exists practicedb;