In this article, I will walk you through exactly how to select a database in MariaDB, the nuances of the USE statement, and the best practices I follow to ensure I never accidentally run a DROP TABLE command against the wrong environment.
How to Select a Database in MariaDB
What Does “Selecting a Database” Mean?
Before we dive into the commands, let’s clarify the concept. When you connect to a MariaDB server, you are authenticating against the instance. This instance might host dozens of separate databases—one for Sales, one for HR, one for Staging, etc.
Selecting a database sets the default context for your current session. It tells MariaDB: “Until I say otherwise, assume every table name I type belongs to this specific database.”
Without selecting a database, you would have to use fully qualified names for every single query (e.g., SELECT * FROM Sales.Orders instead of just SELECT * FROM Orders). While fully qualified names have their place, setting a default context is the standard way I work for efficiency and readability.
Prerequisites
To follow along with the concepts in this tutorial, I assume you have the following:
- Access to a MariaDB Server: Whether it is installed locally on your laptop or hosted on a cloud provider like AWS or Azure.
- A Database User Account: You need a username and password.
- Existing Databases: The server must actually contain databases to select.
If you are unsure what is available on your server, I always recommend starting with a quick reconnaissance command:
SQL
SHOW DATABASES;
This will list everything you have permission to see. If the list is empty (or only shows information_schema), you might need to speak to your administrator about permissions.
Method 1: The Standard Approach – The USE Statement
This is the method I use 99% of the time. It is the SQL standard way to switch contexts and works in almost every command-line interface (CLI) and scripting tool.
The syntax is incredibly simple:
SQL
USE database_name;
How It Works
When you execute this command, MariaDB checks two things:
- Existence: Does
database_nameactually exist? - Privileges: Does your user account have permission to access it?
If both are true, the server silently switches your session context. From that point forward, any query you write is executed against that database.
Example Scenario
Imagine I am a developer named Jason working on an e-commerce platform. I log in and want to check the inventory.
SQL
-- Step 1: I check what is available
SHOW DATABASES;
-- Output might show: 'information_schema', 'mysql', 'performance_schema', 'shop_inventory'
-- Step 2: I select the database
USE shop_inventory;
-- Output: Database changed
Once you see the message Database changed, you are “inside” that database.
Method 2: Selecting at Connection (The CLI Shortcut)
If you are a command-line warrior like me, you probably want to save keystrokes. Why log in and then select the database if you can do it all at once?
When using the standard mariadb (or mysql) command-line client, you can specify the target database directly in your connection string. This is the method I prefer when writing automated Bash scripts or cron jobs.
Syntax:
Bash
mariadb -u username -p database_name
Alternative Syntax (using the flag):
Bash
mariadb -u username -p -D database_name
Why I Use This Method
- Speed: It saves me an extra step.
- Safety: It ensures I don’t accidentally start typing queries in the wrong default database (or worse, no database at all).
- Scripting: When I write Python or PowerShell scripts to perform backups, I almost always define the database in the connection string to avoid logic errors later in the code.
Method 3: Selecting in Graphical User Interfaces (GUIs)
I know many of you prefer visual tools like HeidiSQL, DBeaver, or MySQL Workbench. These tools abstract away the USE command, but the concept remains the same.
In HeidiSQL or DBeaver:
Usually, you will see a tree structure on the left side of your screen listing all databases.
- Single Click: Often just highlights the database details.
- Double Click: This is usually the equivalent of running the
USEcommand.
Visual Cue: In most GUIs, the active database will be highlighted in bold or have a distinct icon (like a green checkmark). I always train my junior developers to look for that bold text before hitting the “Run” button on a query.
Verifying Your Selection
This is a step that separates the rookies from the pros. How do you know which database is currently selected?
If you stepped away for a coffee and came back to an open terminal, you might forget where you were. Running a destructive command like DROP TABLE Users could be catastrophic if you think you are in dev_db but are actually in prod_db.
I always run this function to verify my location:
SQL
SELECT DATABASE();
Result:
- If a database is selected, it returns the name (e.g.,
shop_inventory). - If no database is selected, it returns
NULL.
I recommend adding this check to your troubleshooting mental checklist. If your queries are failing with “Table ‘x’ doesn’t exist,” run SELECT DATABASE() immediately. Nine times out of ten, you aren’t where you think you are.
Handling Errors and Troubleshooting
Even with a command as simple as USE, things can go wrong. Here are the common issues I encounter and how to solve them.
1. Error 1049: Unknown Database
Plaintext
ERROR 1049 (42000): Unknown database 'shipping_logs'
The Fix: You probably made a typo. Run SHOW DATABASES; to check the exact spelling. Remember that database names can be case-sensitive depending on the underlying operating system (more on that below).
2. Access Denied
Plaintext
ERROR 1044 (42000): Access denied for user 'jason'@'localhost' to database 'finance_secure'
The Fix: You exist as a user, but you don’t have privileges on this specific database. You need to ask an administrator (or use the root account) to GRANT you access.
3. The Case Sensitivity Trap
This is a “gotcha” that catches developers moving between Windows and Linux.
- Windows: Generally case-insensitive.
USE SalesandUSE salesoften work the same. - Linux: Generally case-sensitive. If the folder on the disk is named
Sales, typingUSE saleswill fail.
I always advise sticking to lowercase with underscores (snake_case) for all database names to avoid this headache entirely.
Comparison of Selection Methods
To help you decide which workflow fits your style, I’ve put together this quick reference table.
| Method | Syntax/Action | Best Use Case | specific Pros |
| SQL Statement | USE db_name; | Interactive sessions | Standard across all platforms; clear audit trail in scripts. |
| CLI Argument | mariadb ... db_name | Scripting / Automation | Faster; sets context immediately upon connection. |
| Dot Notation | SELECT * FROM db.table | Cross-database joins | No need to switch context; great for ad-hoc queries. |
| GUI Click | Double-click (Mouse) | Visual exploration | Intuitive; helpful when browsing schema structures. |
Best Practices
1. The “Explicit is Better” Rule
Even if I have selected a database using USE, if I am writing a critical stored procedure or a view, I often use the fully qualified name (database.table) anyway. This prevents the code from breaking if someone runs it from a different context.
2. Prompt Customization
If you use the command line terminal, you can configure your prompt to display the current database name.
Instead of seeing:
MariaDB [(none)]>
You can configure it to see:
MariaDB [shop_inventory]>
This visual reminder is a lifesaver.
3. Avoid “USE” in Application Code
If you are writing PHP, Python, or Java code to connect to MariaDB, try to avoid executing the USE statement as a query. Instead, configure the database name in your connection string (like in your .env file). This keeps the connection stateless and cleaner.
Conclusion
Selecting a database in MariaDB is the fundamental handshake between you and your data. Whether you prefer the explicit USE command, the efficiency of the command-line argument, or the visual comfort of a GUI, the goal is the same: establishing a secure and correct context for your work.
You may also like the following articles:
- How To Ping Maria DB Server Port From Ubuntu
- How to Delete a Database in MariaDB
- How to Create Database in MariaDB
- How to List Databases in MariaDB
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.