Password policies in MySQL - Time & Space Complexity
When checking password policies in a database, we want to know how the time to verify passwords changes as more passwords or rules are involved.
We ask: How does the work grow when we check many passwords or apply multiple rules?
Analyze the time complexity of the following code snippet.
-- Check if a password meets policy rules
SELECT password
FROM users
WHERE LENGTH(password) >= 8
AND password REGEXP '[A-Z]'
AND password REGEXP '[a-z]'
AND password REGEXP '[0-9]'
AND password NOT REGEXP '[ ]';
This code checks each user's password against several rules: minimum length, contains uppercase, lowercase, digit, and no spaces.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each password against multiple pattern rules.
- How many times: Once per password in the users table.
As the number of users grows, the database checks more passwords one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Checks 10 passwords against all rules |
| 100 | Checks 100 passwords against all rules |
| 1000 | Checks 1000 passwords against all rules |
Pattern observation: The work grows directly with the number of passwords; doubling passwords doubles the checks.
Time Complexity: O(n)
This means the time to check passwords grows in a straight line with the number of passwords.
[X] Wrong: "Checking multiple rules makes the time grow much faster than the number of passwords."
[OK] Correct: Each password is checked against all rules in a fixed number of steps, so the total time grows mainly with how many passwords there are, not the number of rules.
Understanding how password checks scale helps you design systems that stay fast even as users grow. This skill shows you can think about real-world database performance.
"What if we added a loop to check each password against a list of 10 different forbidden words? How would the time complexity change?"
Practice
validate_password_policy variable?Solution
Step 1: Understand the variable name
The variablevalidate_password_policyrelates to password validation rules.Step 2: Identify its function
It sets rules to ensure passwords are strong and secure.Final Answer:
To set rules for creating strong passwords -> Option AQuick Check:
Password policy = Set rules for strong passwords [OK]
- Confusing password policy with user privileges
- Thinking it manages database storage
- Assuming it controls query cache
Solution
Step 1: Recall how to view system variables
MySQL usesSHOW VARIABLES LIKE 'variable_name';to display variable values.Step 2: Match the variable name
The variable for password policy isvalidate_password_policy, so the correct syntax isSHOW VARIABLES LIKE 'validate_password_policy';Final Answer:
SHOW VARIABLES LIKE 'validate_password_policy'; -> Option CQuick Check:
SHOW VARIABLES LIKE 'validate_password_policy' [OK]
- Using SELECT on a variable name
- Trying non-existent commands like SHOW PASSWORD POLICY
- Using GET VARIABLES which is invalid syntax
SET GLOBAL validate_password_length = 10;
SHOW VARIABLES LIKE 'validate_password_length';
What will be the output of the
SHOW VARIABLES command?Solution
Step 1: Understand the SET GLOBAL command
The command sets the global password length requirement to 10 characters.Step 2: SHOW VARIABLES output
TheSHOW VARIABLES LIKE 'validate_password_length';will display the current value, which is now 10.Final Answer:
Variable_name: validate_password_length, Value: 10 -> Option AQuick Check:
SET GLOBAL changes variable, SHOW displays updated value [OK]
- Expecting default value instead of updated
- Confusing variable names
- Assuming syntax error from SET GLOBAL
SET GLOBAL validate_password_policy = 'STRONG';
But MySQL returned an error. What is the likely cause?
Solution
Step 1: Check variable type for validate_password_policy
This variable expects an integer value (0,1,2) representing policy levels, not strings.Step 2: Understand error cause
Using a string like 'STRONG' causes a type mismatch error.Final Answer:
The value should be an integer, not a string -> Option DQuick Check:
Policy level = integer, not string [OK]
- Using string values instead of integers
- Mistyping variable name
- Confusing SET GLOBAL with SET SESSION
Solution
Step 1: Understand password length requirement
You want at least 12 characters, sovalidate_password_lengthmust be set to 12.Step 2: Understand policy levels
Policy level 2 (STRONG) requires mixed case, numbers, and special characters.Step 3: Match variables to requirements
Settingvalidate_password_policy = 2and length to 12 enforces all requirements.Final Answer:
SET GLOBAL validate_password_length = 12; SET GLOBAL validate_password_policy = 2; -> Option BQuick Check:
Length 12 + policy 2 = strong password rules [OK]
- Using policy 0 or 1 which are weaker
- Setting length less than required
- Confusing policy levels with string values
Ready to prove it?
Pass the test and this pattern moves unknown
