Database creation and selection in MySQL - Time & Space Complexity
When we create or select a database, the system performs some work behind the scenes.
We want to understand how this work grows as the number of databases increases.
Analyze the time complexity of the following code snippet.
CREATE DATABASE example_db;
USE example_db;
This code creates a new database named 'example_db' and then selects it for use.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking if the database name already exists and registering the new database.
- How many times: This check happens once per database creation, not repeated in a loop.
As the number of databases grows, the system needs to check existing names to avoid duplicates.
| Input Size (number of databases) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of existing databases.
Time Complexity: O(n)
This means the time to create a database grows linearly with the number of databases.
[X] Wrong: "Creating a database always takes the same time no matter how many databases exist."
[OK] Correct: The system must check existing databases to avoid duplicates, so more databases mean more checks.
Understanding how simple commands scale helps you explain system behavior clearly and shows you think about efficiency.
"What if the system used a fast lookup like a hash table to check database names? How would the time complexity change?"
Practice
CREATE DATABASE command do in MySQL?Solution
Step 1: Understand the purpose of
This command is used to make a new database where you can store tables and data.CREATE DATABASEStep 2: Differentiate from other commands
Deleting databases usesDROP DATABASE, selecting usesUSE, and showing usesSHOW DATABASES.Final Answer:
It creates a new database to store data. -> Option CQuick Check:
CREATE DATABASE= create new database [OK]
- Confusing CREATE with USE command
- Thinking CREATE deletes databases
- Mixing CREATE with SHOW DATABASES
shop in MySQL?Solution
Step 1: Recall the command to select a database
In MySQL, the command to start using a database isUSE database_name;.Step 2: Check each option's syntax
OnlyUSE shop;is valid syntax. Others are incorrect commands.Final Answer:
USE shop; -> Option DQuick Check:
USEselects database [OK]
USE to pick your database [OK]- Using SELECT instead of USE
- Trying OPEN or CHOOSE commands (not valid)
- Missing semicolon at end
CREATE DATABASE testdb; USE testdb; SHOW TABLES;
Solution
Step 1: Create and select the database
The commandCREATE DATABASE testdb;makes a new database named 'testdb'. ThenUSE testdb;selects it for use.Step 2: Show tables in the selected database
SHOW TABLES;lists all tables in the current database. Since 'testdb' is empty, it will show an empty list but no error.Final Answer:
Lists all tables in the database 'testdb'. -> Option AQuick Check:
SHOW TABLESlists tables in current DB [OK]
- Thinking SHOW TABLES shows databases
- Assuming error because database is new
- Confusing CREATE DATABASE with CREATE TABLE
CREATE DATABASE mydb USE mydb;
Solution
Step 1: Check syntax of each command
MySQL commands must end with a semicolon. The first line misses a semicolon afterCREATE DATABASE mydb.Step 2: Verify other parts
The database name 'mydb' is valid,USEcan followCREATE DATABASE, and case does not matter for commands.Final Answer:
Missing semicolon after CREATE DATABASE statement. -> Option BQuick Check:
Semicolon ends commands [OK]
- Forgetting semicolon at end of statements
- Thinking database names must be uppercase
- Believing USE cannot follow CREATE DATABASE
sales and inventory, then select sales to work with. Which sequence of commands is correct?Solution
Step 1: Create both databases separately
Each database must be created with its ownCREATE DATABASEcommand ending with a semicolon.Step 2: Select the 'sales' database to use
After creating, useUSE sales;to select the 'sales' database for work.Final Answer:
CREATE DATABASE sales; CREATE DATABASE inventory; USE sales; -> Option AQuick Check:
Create each DB then USE the one you want [OK]
- Trying to create multiple databases in one command
- Using USE before creating the database
- Selecting wrong database after creation
Ready to prove it?
Pass the test and this pattern moves unknown
