To create a new database, use the CREATE DATABASE
statement followed by the name of the database.
Syntax:
CREATE DATABASE database_name;
Example:
-- Create a new database named 'test_db'
CREATE DATABASE test_db;
To list all available databases in the SQL server, use the SHOW DATABASES
command.
Syntax:
SHOW DATABASES;
Example:
-- List all databases
SHOW DATABASES;
To delete an existing database, use the DROP DATABASE
statement followed by the name of the database. Be cautious, as this action is irreversible and will delete all data within the database.
Syntax:
DROP DATABASE database_name;
Example:
-- Delete the database named 'test_db'
DROP DATABASE test_db;
To avoid errors when trying to drop a database that may not exist, you can use the IF EXISTS
clause.