SQL DROP DATABASE
SQL DROP DATABASE Statement
The SQL DROP DATABASE
statement is used to delete an existing database and all of its tables and data. This operation is irreversible and should be used with caution, as it will permanently remove the database and all the information it contains.
Syntax
DROP DATABASE database_name;
DROP DATABASE
: This is the SQL keyword used to delete a database.database_name
: This specifies the name of the database you want to delete.
Example
Let's go through a complete example that includes creating a database, creating a table, inserting data into the table, and then dropping the database.
Step 1: Creating a Database
This step involves creating a new database named example_db
.
CREATE DATABASE example_db;
In this example, we create a database named example_db
.
Step 2: Using the New Database
This step involves selecting the newly created database to use it for subsequent operations.
USE example_db;
This command selects example_db
as the current database.
Step 3: Creating a Table
In this step, we create a table named employees
within the newly created database.
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100),
hire_date DATE
);
Here, we define the employees
table with columns for id
, first_name
, last_name
, email
, and hire_date
. The id
column is set as the primary key and will auto-increment.
Step 4: Inserting Data into the Table
This step involves inserting some sample data into the employees
table.
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('John', 'Doe', 'john.doe@example.com', '2023-01-01');
INSERT INTO employees (first_name, last_name, email, hire_date) VALUES ('Jane', 'Smith', 'jane.smith@example.com', '2023-02-01');
Here, we insert two rows of data into the employees
table.
Step 5: Dropping the Database
This step involves deleting the example_db
database along with all its tables and data.
DROP DATABASE example_db;
This command will permanently delete the example_db
database and all the data it contains. Use this command with caution, as it cannot be undone.