MySQL CRUD Operations on Database

 

SQL - CREATE Database

The SQL CREATE DATABASE statement is used to create a new SQL database.

Syntax

The basic syntax of this CREATE DATABASE statement is as follows −

CREATE DATABASE DatabaseName;

Always the database name should be unique within the RDBMS.

Example

If you want to create a new database <testDB>, then the CREATE DATABASE statement would be as shown below −

SQL> CREATE DATABASE testDB;

Make sure you have the admin privilege before creating any database. Once a database is created, you can check it in the list of databases as follows −

SQL> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| AMROOD             |

| TUTORIALSPOINT     |

| mysql              |

| orig               |

| test               |

| testDB             |

+--------------------+

7 rows in set (0.00 sec)

SQL - DROP or DELETE Database

The SQL DROP DATABASE statement is used to drop an existing database in SQL schema.

Syntax

The basic syntax of DROP DATABASE statement is as follows −

DROP DATABASE DatabaseName;

Always the database name should be unique within the RDBMS.

Example

If you want to delete an existing database <testDB>, then the DROP DATABASE statement would be as shown below −

SQL> DROP DATABASE testDB;

NOTE − Be careful before using this operation because by deleting an existing database would result in loss of complete information stored in the database.

Make sure you have the admin privilege before dropping any database. Once a database is dropped, you can check it in the list of the databases as shown below −

SQL> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| AMROOD             |

| TUTORIALSPOINT     |

| mysql              |

| orig               |

| test               |

+--------------------+

6 rows in set (0.00 sec)

SQL - SELECT Database, USE Statement

When you have multiple databases in your SQL Schema, then before starting your operation, you would need to select a database where all the operations would be performed.

The SQL USE statement is used to select any existing database in the SQL schema.

Syntax

The basic syntax of the USE statement is as shown below −

USE DatabaseName;

Always the database name should be unique within the RDBMS.

Example

You can check the available databases as shown below −

SQL> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| AMROOD             |

| TUTORIALSPOINT     |

| mysql              |

| orig               |

| test               |

+--------------------+

6 rows in set (0.00 sec)

Now, if you want to work with the AMROOD database, then you can execute the following SQL command and start working with the AMROOD database.

SQL> USE AMROOD;

Comments

Popular posts from this blog

MySQL NULL Value

MySQL Join - Inner, Outer, Left, Right, Self Join