0
0
MySQLquery~10 mins

Database creation and selection in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Database creation and selection
Start
Write CREATE DATABASE statement
Execute CREATE DATABASE
Database created?
NoError: Fix and retry
Yes
Write USE statement
Execute USE statement
Database selected
Ready for further commands
First, create a new database with CREATE DATABASE. Then select it with USE to work inside it.
Execution Sample
MySQL
CREATE DATABASE school;
USE school;
This code creates a database named 'school' and then selects it for use.
Execution Table
StepSQL CommandActionResultNotes
1CREATE DATABASE school;Create database named 'school'SuccessDatabase 'school' created
2USE school;Select database 'school' for useSuccessCurrent database set to 'school'
3Ready for next commandsN/ADatabase 'school' is active
💡 Database 'school' created and selected successfully, ready for further commands.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Current DatabaseNoneNoneschoolschool
Key Moments - 2 Insights
Why do we need to run USE after CREATE DATABASE?
CREATE DATABASE only makes the database but does not select it. USE sets the current database to run commands inside it, as shown in execution_table step 2.
What happens if we try to create a database that already exists?
The CREATE DATABASE command will fail with an error. You must fix the name or drop the existing database first. This is implied in the flow where creation success is checked.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current database after step 1?
Amysql
Bschool
CNone
Dtest
💡 Hint
Check the 'Current Database' variable in variable_tracker after step 1.
At which step does the database become selected for use?
AStep 2
BStep 1
CStep 3
DBefore Step 1
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for step 2.
If the CREATE DATABASE command fails, what should you do next?
ARun USE command anyway
BFix the error and retry CREATE DATABASE
CDrop the database and then run USE
DNothing, the database is created automatically
💡 Hint
Refer to the concept_flow where failure leads to 'Error: Fix and retry'.
Concept Snapshot
CREATE DATABASE db_name;  -- creates a new database
USE db_name;              -- selects the database to work in
CREATE DATABASE only makes the database,
USE sets it as current for commands.
Always run USE after creating to select.
Full Transcript
To create and select a database in MySQL, first run CREATE DATABASE with your chosen name. This command makes the database but does not select it. Next, run USE with the database name to select it for your session. After USE, all commands run inside that database. If CREATE DATABASE fails, fix the error before continuing. This process ensures you have a database ready and selected for your work.