Notes

`USE` Statement in DBMS (MySQL) [ English ]

< Prev Next >

Introduction

A database server can contain multiple databases. Before performing operations such as creating tables, inserting data, or retrieving records, a user must first select the database in which they want to work.

The USE statement is used to select a specific database and make it the current working database. Once a database is selected, all SQL operations will be performed within that database unless another database is selected.

Definition

The USE statement is used to select or switch to a particular database in the database server so that all subsequent SQL queries are executed in that database.

Syntax

USE database_name;

How It Works

When the USE command is executed:

  1. The DBMS checks whether the specified database exists.
  2. If the database exists and the user has permission, it becomes the active database.
  3. All subsequent commands such as CREATE TABLE, INSERT, SELECT, etc., will apply to that database.

Example

USE student_db;

After executing this command, student_db becomes the current database.

Now any table created will be stored inside this database.

Example:

CREATE TABLE students (
    id INT,
    name VARCHAR(50),
    course VARCHAR(50)
);

The students table will be created inside the student_db database.


Checking the Current Database

To check which database is currently selected, the following query can be used:

SELECT DATABASE();

Example Output:

DATABASE()
student_db

This shows that student_db is the currently active database.

Important Notes

Key Points

Summary

The USE statement is an important command in DBMS that allows users to select the database they want to work with. By setting a database as the current working database, users can easily perform operations such as creating tables, inserting data, and retrieving records within that specific database.

< Prev Next >