Database AdministrationUnit 410 min read
User Roles, Privileges, Authentication & Security in DBMS
Unit 4 of Database Administration explores how to manage database users, assign permissions, enforce authentication, and secure data against unauthorized access or breaches, with real-world examples from Nepalese banks and apps.
TAKEAWAYS:
- Database users, roles, and privileges define who can access what data and perform which operations (SELECT, INSERT, DROP).
- Authentication (passwords, certificates, biometrics) verifies user identity, while authorization enforces access control via GRANT/REVOKE.
- Security threats (SQL injection, privilege escalation, data leaks) require encryption, auditing, and least-privilege principles.
- Views and stored procedures act as security layers by restricting direct table access or exposing only necessary logic.
- Real-world tie: Ncell’s customer portal uses role-based access (e.g., agents vs. admins) to limit data exposure.
- Exam focus: Know how to write
GRANT,REVOKE, andCREATE ROLEcommands for TU/PU questions.
1. Database Users and Roles: Who Gets Access?
Databases are not open books—only authorized users should read, write, or delete data. Users are database entities (not OS users) with unique credentials, while roles group privileges for efficiency.
erDiagram
USER ||--o{ ROLE : "has"
ROLE ||--o{ PRIVILEGE : "grants"
PRIVILEGE }|--|| OBJECT : "applies_to"
USER {
string username
string password_hash
string auth_method
}
ROLE {
string role_name
string description
}
PRIVILEGE {
string operation
string object_type
}
OBJECT {
string object_name
string object_type
}ER diagram showing the relationship between users, roles, privileges, and database objectsHow Users and Roles Work
classDiagram
class User {
+username
+password_hash
+auth_method
}
class Role {
+role_name
+privileges[]
}
class Privilege {
+operation (SELECT/INSERT/UPDATE/DROP)
+object (table/view/procedure)
}
User "1" --> "*" Role : "assigned_to"
Role "1" --> "*" Privilege : "grants"- Users are created via
CREATE USER(e.g.,CREATE USER 'sales_clerk' IDENTIFIED BY 'SecurePass123';). - Roles bundle privileges (e.g.,
CREATE ROLE 'report_reader'). - Privileges are tied to objects (tables, views) and operations (e.g.,
GRANT SELECT ON orders TO report_reader;).
Worked Example: Ncell’s Billing System
Ncell’s backend database has:
- Users:
billing_agent,customer_service,admin. - Roles:
view_bills: CanSELECTfromcustomer_bills.update_payments: CanINSERT/UPDATEintopayment_records.
- Privileges:
GRANT SELECT ON customer_bills TO view_bills; GRANT INSERT, UPDATE ON payment_records TO update_payments;
Why? Agents only see bills; admins manage all data.
2. Authentication: Proving You’re Who You Claim
Authentication answers: "Are you really User X?" Methods include:
| Method | Example | Security Level | Used By |
|---|---|---|---|
| Password | CREATE USER 'user' IDENTIFIED BY 'pass'; |
Low-Medium | Small apps (eSewa) |
| Certificate | TLS/SSL client certs | High | Banks (NMB) |
| Biometrics | Fingerprint/face scan | Very High | Android Pay |
| Multi-Factor (MFA) | OTP + password | High | Khalti, Daraz |
Real Picture:
Password Policies Matter
Weak passwords (e.g., 123456) are exploited in SQL injection attacks. Enforce:
- Minimum 12 characters.
- Mix of uppercase, numbers, symbols.
- Expiry (e.g., every 90 days).
3. Authorization: What You’re Allowed to Do
Authorization answers: "What can User X do?" Controlled via:
- GRANT: Assigns privileges.
GRANT ALL PRIVILEGES ON accounts TO bank_teller; - REVOKE: Removes privileges.
REVOKE DELETE ON loans FROM junior_staff; - Default Privileges: Set for new roles/users.
CREATE ROLE 'auditor' WITH DEFAULT TABLE PRIVILEGES SELECT;
Least Privilege Principle
Give users only what they need. Example:
- A traffic officer in Kathmandu’s database only needs to
SELECTfromfines_issuedandUPDATEpayment_status. - Never grant
DROP TABLEto app users—only DBAs.
4. Security Threats and Mitigations
| Threat | How It Works | Mitigation |
|---|---|---|
| SQL Injection | Malicious SQL via input fields | Use prepared statements |
| Privilege Escalation | Exploiting over-permissive roles | Audit roles with GRANT OPTION |
| Data Leakage | Unauthorized SELECT * queries |
Column-level permissions |
| Denial of Service (DoS) | Flooding DB with queries | Rate limiting, connection pools |
sequenceDiagram
participant User
participant App
participant Database
User->>App: Input: '123 OR 1=1'
App->>Database: EXECUTE('SELECT * FROM accounts WHERE id = ' + input)
Database-->>App: All accounts returned (SQL Injection)
alt Mitigation
App->>Database: PREPARE('SELECT * FROM accounts WHERE id = ?')
App->>Database: EXECUTE(123)
Database-->>App: Only id=123 account
endSQL injection attack and its mitigation using parameterized queriesReal Example: Daraz’s Cart System
- Threat: A hacker injects
DROP TABLE cart_items;via the "Add to Cart" field. - Fix: Daraz’s DB uses parameterized queries:
-- Safe: Parameters are escaped PREPARE add_to_cart (INT) AS INSERT INTO cart_items VALUES (?, NOW()); EXECUTE add_to_cart(12345);
5. Advanced Security: Views and Stored Procedures
Views as Security Layers
A view hides columns/tables. Example:
CREATE VIEW customer_orders AS
SELECT customer_id, order_id, order_date
FROM orders
WHERE status = 'completed';
- Benefit: Users can’t see
credit_card_numbersin the base table.
Stored Procedures for Controlled Access
Instead of letting users run:
DELETE FROM accounts WHERE customer_id = 123;
Force them to use a procedure:
CREATE PROCEDURE safe_withdrawal(IN customer_id INT, IN amount DECIMAL)
BEGIN
IF amount > (SELECT balance FROM accounts WHERE customer_id = customer_id) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Insufficient funds';
ELSE
UPDATE accounts SET balance = balance - amount WHERE customer_id = customer_id;
END IF;
END;
Why? Prevents accidental/deadly DELETE or UPDATE mistakes.
6. Auditing and Monitoring
Track who did what and when:
-- Enable auditing in Oracle
AUDIT SELECT, INSERT ON accounts BY user_id;
-- Check audit logs
SELECT * FROM dba_audit_trail;
Real Use Case: NEPSE’s Trading System
- Audit Trail: Logs every
BUY/SELLorder with timestamp and user ID. - Forensics: If a fraudulent trade occurs, admins trace the user via logs.
7. Encryption: Protecting Data at Rest and in Transit
| Type | Example | Command/Tool |
|---|---|---|
| Data at Rest | Encrypt tables | ALTER TABLE accounts ENCRYPTION ON; (SQL Server) |
| Data in Transit | TLS/SSL for connections | CREATE USER 'app' IDENTIFIED BY 'certificate'; |
| Column-Level | Sensitive fields (SSN, cards) | ALTER TABLE customers ADD COLUMN ssn VARCHAR(255) ENCRYPTED; |
Real Picture:
In the Real World
Khalti’s Payment Gateway
- Idea Used: Multi-factor authentication (MFA) and role-based access.
- How: Merchants get a
merchant_rolewithSELECTontransactionsbut noUPDATErights. Users verify payments via OTP + fingerprint.
NMB Bank’s Loan System
- Idea Used: Stored procedures and least privilege.
- How: Loan officers run
approve_loan(customer_id, amount)instead of directUPDATEonloan_status. The procedure checks credit scores first.
Pathao’s Driver App
- Idea Used: Views to hide sensitive data.
- How: Drivers see only
ride_id,pickup_location, andfarevia a view, not thedriver_salarytable.
Exam Tip
Command-Based Questions (30% weight)
- Write exact
GRANT,REVOKE, andCREATE ROLEcommands. Example:"Grant SELECT on
employeestohr_rolebut revoke DELETE." Answer:GRANT SELECT ON employees TO hr_role; REVOKE DELETE ON employees FROM hr_role;
- Write exact
Scenario Analysis (40% weight)
- Describe how to secure a real system (e.g., "How would you restrict a Daraz order clerk from viewing customer addresses?").
- Key Points:
- Use roles (
order_clerk_role). - Grant only
SELECTonorder_itemsandUPDATEonorder_status. - Create a view hiding
customer_address.
- Use roles (
Threat Mitigation (20% weight)
- Match threats (SQL injection, privilege escalation) to fixes (prepared statements, auditing).
- Example Question:
"A bank’s DB was hacked via SQL injection. Suggest 2 fixes." Answer:
- Use prepared statements for all queries.
- Enable column-level permissions (e.g.,
GRANT SELECT (account_id, balance) ON accounts TO teller;).
Diagrams (10% weight)
- Draw a role-privilege matrix or authentication flow (e.g., MFA steps).
- Example:
sequenceDiagram User->>DB: Enters username/password DB->>User: Requests OTP User->>DB: Sends OTP DB->>User: Grants access if OTP matches
Final Checklist Before Exam:
- Know
GRANT/REVOKEsyntax for tables/views/procedures. - Understand least privilege vs. default privileges.
- Recall 3 authentication methods and their use cases.
- Practice writing a stored procedure for a real scenario (e.g., bank transfer).
- Sketch a role-privilege diagram for a given system (e.g., hospital DB).
In the real world
- Khalti’s Payment Gateway: Uses multi-factor authentication (MFA) where users must enter both a password and a one-time pin (OTP) sent to their phone to authorize transactions, preventing unauthorized access.
- Ncell’s Customer Portal: Implements role-based access control (RBAC) where agents can only view customer bills (
view_billsrole) while admins have full control (adminrole), limiting data exposure to least privilege. - Daraz’s Cart System: Protects against SQL injection by using parameterized queries for all user inputs, ensuring malicious SQL cannot alter or delete data.
Based on the TU BIM syllabus for Database Administration (IT276), unit 4.
Discussion
Loading…