Database Users and Roles

All SQL topics
∙ Topic

Database Users and Roles

Database users and roles are used to manage access control in SQL. Users represent individual accounts, while roles group permissions for easier management.

📝Syntax
-- Create user
CREATE USER 'username'@'host' IDENTIFIED BY 'password';

-- Create role
CREATE ROLE role_name;

-- Grant permissions
GRANT SELECT, INSERT ON database.table TO role_name;
database-users-and-roles.sql
📝 Edit Code
👁 Preview
💡 This preview does not execute SQL; it’s for reading/editing the query.
💡What are Database Users?
  • 1Accounts that access the database.
  • 2Each user has specific permissions.
  • 3Can be restricted by host.
  • 4Used for authentication.
💡What are Roles?
  • 1Groups of permissions.
  • 2Assigned to multiple users.
  • 3Simplify permission management.
  • 4Improve security control.
💡How Users and Roles Work
  • 1Users are assigned roles.
  • 2Roles contain privileges.
  • 3Permissions flow from roles to users.
  • 4Centralized access control.
💡Privileges in SQL
  • 1SELECT - read data.
  • 2INSERT - add data.
  • 3UPDATE - modify data.
  • 4DELETE - remove data.
💡User Management Benefits
  • 1Improved security.
  • 2Simplified administration.
  • 3Better access control.
  • 4Scalable permission system.
💡Role Management Benefits
  • 1Easy permission assignment.
  • 2Reusable access groups.
  • 3Reduces human error.
  • 4Better security structure.
🏢Real-world
  • 1Managing employee database access.
  • 2Separating admin and user privileges.
  • 3Securing sensitive business data.
  • 4Controlling API database access.
  • 5Enterprise security management.
Common Mistakes
  • 1Giving all privileges to all users.
  • 2Not using roles properly.
  • 3Weak password policies.
  • 4Ignoring privilege revocation.
Best Practices
  • 1Use roles instead of individual permissions.
  • 2Follow least privilege principle.
  • 3Regularly review user access.
  • 4Use strong authentication methods.
Quick Summary
  • Users represent database accounts.
  • Roles group permissions for users.
  • Used for access control and security.
  • Follows least privilege principle.
  • Improves database security management.
🎯Interview Questions
Q1. What is a database user?
Answer: An account that accesses the database with specific permissions.
Q2. What is a role in SQL?
Answer: A collection of permissions assigned to users.
Q3. Why use roles?
Answer: To simplify permission management.
Q4. What is least privilege principle?
Answer: Giving users only required permissions.
Q5. Can roles be assigned to users?
Answer: Yes, roles are assigned to users for access control.
Quiz

What is the main purpose of roles in SQL?