SQL USE Database Statement

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

The SQL USE DATABASE statement plays a vital role in identifying a specific database from a collection of multiple databases in the system. This helps enable the execution of various operations exclusively within the chosen database. These operations include actions like creating tables, inserting data, updating existing data, and deleting data.

In scenarios involving multiple databases in the SQL environment, the USE DATABASE command assumes a pivotal role by facilitating the targeting of a precise database for subsequent operations.

In other words, the SQL USE DATABASE statement is like picking a specific book from a shelf full of books. Once you pick that book, you can read, write, or draw things inside it. It's like having a magnifying glass for a single book on a shelf crowded with books. In this article, we will delve into the SQL USE DATABASE statement, covering its syntax and exploring a handful of examples.

USE in SQL Syntax

Let's take a look at how the USE DATABASE statement is written in SQL:

In this case, the term DatabaseName refers to the specific name of the database we want to select.

The system makes sure that each database name is special and only used once within the relational database management system (RDBMS). This ensures that databases are uniquely identified and prevents naming conflicts within the system.

Sharpen Your Fundamentals with Free Learning

USE in SQL Example

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Basic Usage of the SQL USE DATABASE Statement

The basic usage of the SQL USE DATABASE statement involves designating a specific database for your actions. Before diving into a practical example, it's important to ensure that the database you intend to work with has been created using the CREATE DATABASE query.

For instance, consider a scenario where we want to manage employee information within a database. Let's start by creating a new database called EmployeeDB:

Once the database is created, we can use the USE command to select it:

By executing the above command, we switch our focus to the EmployeeDB database, enabling us to carry out operations exclusively within this database's context.

Before Using Any Database Using the Use Command, It’s Necessary to First Create It

First, we'll start by creating a new database using the SQL command:

After creating the database, we can see the list of all available databases like this:

The result will show the names of various databases and now, we'll use the following query to choose myDatabase as the one we want to work with:

Once we switch to the myDatabase, we can carry out different tasks. For example, let's create a new table named Employee:

Next, we'll insert some records into the Employee table using SQL INSERT statements:

To verify our operations, we can retrieve the records from the Employee table using the SQL SELECT statement:

The output will show the details of the employees:

IDMameSalary
1John Doe50000.00
2Jane Smith60000.00
3Michael Johnson55000.00

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

USE DATABASE Statement on Non-Existing Database

If you try to choose a database that hasn't been created yet, you'll run into a problem. In this example, we're attempting to switch to a database with the name DatabaseName, but it hasn't been set up.

When you run the query above, you'll see a message like this:

Output:

This message indicates that the system doesn't recognize the name you provided because the database doesn't exist.

To resolve this, you need to create the database if it doesn't exist already and then re-run the USE Statement to switch to the newly created database.

Note: It's a good practice to ensure that the database you want to use exists before attempting to switch to it. Verifying the database's existence can help avoid unnecessary errors and ensure a smooth database selection process.

Conclusion

  • The SQL USE DATABASE statement is essential for targeting specific databases and executing actions within them.
  • With clear syntax like USE DatabaseName; selecting a desired database becomes straightforward.
  • Through practical examples, we saw how to create, switch, and perform operations within a chosen database.
  • When dealing with multiple databases, the ability to direct operations to a specific database simplifies maintenance tasks.
  • Selecting a database explicitly reduces the risk of accidental data changes or deletions in unrelated databases
  • Handling errors when attempting to select non-existent databases emphasizes the importance of verification.