Creating users in MySQL - Performance & Efficiency
When we create users in a database, it's important to know how the time it takes grows as we add more users.
We want to understand how the work changes when we create many users one after another.
Analyze the time complexity of the following code snippet.
-- Create a single user
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
-- Create multiple users in a loop (pseudo code)
-- For example, creating users from user1 to userN
-- This is a conceptual example, not exact SQL syntax
FOR i IN 1 TO N DO
CREATE USER CONCAT('user', i)@'localhost' IDENTIFIED BY 'password';
END FOR;
This code creates one user at a time, and when repeated, creates many users one after another.
- Primary operation: Creating a user with the CREATE USER command.
- How many times: Once for each user, repeated N times if creating N users.
Each user creation takes about the same time, so the total time grows as we add more users.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 user creations |
| 100 | 100 user creations |
| 1000 | 1000 user creations |
Pattern observation: The time grows directly with the number of users created.
Time Complexity: O(n)
This means the time to create users grows in a straight line as you add more users.
[X] Wrong: "Creating multiple users at once is as fast as creating one user."
[OK] Correct: Each user creation takes its own time, so doing many users adds up the time.
Understanding how time grows when creating users helps you explain database operations clearly and shows you can think about efficiency in real tasks.
"What if we created users in batches instead of one by one? How would the time complexity change?"
Practice
CREATE USER statement in MySQL?Solution
Step 1: Understand the purpose of
TheCREATE USERCREATE USERcommand is used to add a new user account to the MySQL server.Step 2: Differentiate from other user commands
Deleting users usesDROP USER, changing passwords usesALTER USER, and listing users requires querying system tables.Final Answer:
To add a new user who can access the database -> Option AQuick Check:
CREATE USERadds users [OK]
- Confusing CREATE USER with DROP USER
- Thinking CREATE USER changes passwords
- Assuming CREATE USER lists users
Solution
Step 1: Check the correct syntax for user creation
The correct syntax includes the username in quotes, the host in quotes after @, and the password set with IDENTIFIED BY.Step 2: Validate each option
CREATE USER 'alice'@'%' IDENTIFIED BY 'mypassword'; uses 'alice'@'%' which means any host, and IDENTIFIED BY 'mypassword' is correct. CREATE USER alice IDENTIFIED BY 'mypassword'; misses the host part. CREATE USER 'alice' IDENTIFIED WITH 'mypassword'; uses IDENTIFIED WITH which is incorrect here. CREATE USER 'alice'@'localhost' PASSWORD 'mypassword'; uses PASSWORD keyword incorrectly.Final Answer:
CREATE USER 'alice'@'%' IDENTIFIED BY 'mypassword'; -> Option CQuick Check:
Correct syntax includes user@host and IDENTIFIED BY [OK]
- Omitting the host part after username
- Using PASSWORD instead of IDENTIFIED BY
- Using IDENTIFIED WITH incorrectly
CREATE USER 'bob'@'localhost' IDENTIFIED BY 'secret';
Solution
Step 1: Analyze the command
The command creates a user named 'bob' who can connect only from 'localhost' with password 'secret'.Step 2: Understand host specification
The '@localhost' means the user can connect only from the local machine, not from other hosts.Final Answer:
A new user 'bob' is created who can connect only from localhost -> Option AQuick Check:
User@localhost limits connection to local machine [OK]
- Assuming user can connect from any host without '%' wildcard
- Thinking password is missing causes error
- Confusing CREATE USER with DROP USER
CREATE USER 'carol' IDENTIFIED BY 'pass123';
Solution
Step 1: Check syntax requirements for CREATE USER
MySQL requires specifying the host part after the username, like 'user'@'host'.Step 2: Identify the missing part
The command misses '@host' after 'carol', which causes a syntax error.Final Answer:
Missing host part after username -> Option BQuick Check:
User must be 'name'@'host' [OK]
- Omitting the host part
- Thinking quotes are not allowed for usernames
- Believing password must be numeric
Solution
Step 1: Create user with correct host and password
The user 'dave' must be created with host '192.168.1.100' and password 'safePass!'.CREATE USER 'dave'@'192.168.1.100' IDENTIFIED BY 'safePass!';Step 2: Grant correct permissions to the right user and host
CREATE USER 'dave'@'192.168.1.100' IDENTIFIED BY 'safePass!'; GRANT SELECT ON employees.* TO 'dave'@'192.168.1.100';grants SELECT on employees.* to 'dave'@'192.168.1.100', matching the user host.CREATE USER 'dave'@'192.168.1.100' IDENTIFIED BY 'safePass!'; GRANT SELECT ON employees TO 'dave'@'localhost';grants to 'dave'@'localhost', which is incorrect. Options B and C have wrong host or privileges.Final Answer:
CREATE USER 'dave'@'192.168.1.100' IDENTIFIED BY 'safePass!'; GRANT SELECT ON employees.* TO 'dave'@'192.168.1.100'; -> Option DQuick Check:
User host and grant host must match [OK]
- Granting permissions to wrong host
- Using '%' wildcard when specific IP is required
- Granting wrong privileges or missing database.*
Ready to prove it?
Pass the test and this pattern moves unknown
