← Back to MySQL Course | Chapter 1: Introduction & Basics | Lesson 6 of 8

MySQL Command Line

The MySQL command line is like texting the database directly. There are no buttons, just you typing a command and getting an instant reply.
Syntax
sql
mysql -u username -p
USE database_name;
SHOW TABLES;
statement;

Opening the Command Line

The mysql command-line client connects directly to the server without any GUI overhead, which makes it the fastest option for quick checks, scripting, or working over SSH on a remote server with no graphical tools installed.

Example: Opening the Command Line

sql
-- mysql -u root -p    (run from a terminal to open the client)
SELECT 1;

Executing Commands

Every statement at the prompt must end in a semicolon before MySQL will execute it — pressing Enter alone just starts a new line, which is why a forgotten semicolon leaves you stuck on a -> continuation prompt.

Example: Executing Commands

sql
SELECT 'Hello';

Navigating Databases

Running USE followed by a database name sets your active database for the session, so subsequent table references don't need to be fully qualified with the database name each time.

Example: Navigating Databases

sql
USE test_db;
SELECT * FROM users;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Formatting Console Output

Built-in functions like CONCAT, UPPER, or ROUND can reshape values as they're displayed without altering the stored data, which is handy for making raw terminal output more readable during quick investigations.

Example: Formatting Console Output

sql
SELECT CONCAT(UPPER(name), ' - ', ROUND(salary, 0)) AS summary FROM employees;

Closing the Session

Typing EXIT or QUIT closes the client connection cleanly and frees the server-side resources tied to your session — leaving connections open unnecessarily can eventually exhaust a server's max-connection limit.

Example: Closing the Session

sql
-- Type EXIT; or QUIT; at the prompt to close the client connection
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the ; at the end of a statement, so the prompt changes to -> and nothing runs.
  2. Writing mysql -uroot -p with the password directly on the command line, which exposes it in the shell history.
  3. Running queries without a USE first, so you get No database selected.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.