⬅ Previous Topic
SQL Injection and SecurityNext Topic ⮕
Common Table Expressions in SQL⬅ Previous Topic
SQL Injection and SecurityNext Topic ⮕
Common Table Expressions in SQLImagine a school where everyone had keys to every room — from classrooms to the principal’s office. It would be chaos. The same goes for databases. User management and permissions allow you to assign just the right access to each user — no more, no less — ensuring both security and accountability.
-- Syntax (MySQL):
CREATE USER 'teacher1'@'localhost' IDENTIFIED BY 'SecurePass@123';
This creates a user named teacher1
who can connect from the local machine.
GRANT SELECT ON school.students TO 'teacher1'@'localhost';
GRANT SELECT, INSERT, UPDATE ON school.marks TO 'teacher1'@'localhost';
FLUSH PRIVILEGES;
SHOW GRANTS FOR 'teacher1'@'localhost';
GRANT SELECT ON school.students TO 'teacher1'@'localhost';
GRANT SELECT, INSERT, UPDATE ON school.marks TO 'teacher1'@'localhost';
REVOKE INSERT, UPDATE ON school.marks FROM 'teacher1'@'localhost';
This removes data modification rights but retains read-only access.
DROP USER 'teacher1'@'localhost';
This permanently removes the user from the system.
Permission | Description |
---|---|
SELECT | Read rows |
INSERT | Add new rows |
UPDATE | Modify existing rows |
DELETE | Remove rows |
ALL | Full access to a table or DB |
REVOKE
when permissions are no longer neededSHOW GRANTS
regularlyJust like in a school, not everyone needs access to every file cabinet. Managing SQL users and their permissions helps enforce clear boundaries. Done right, it protects your data, clarifies roles, and supports system stability.
⬅ Previous Topic
SQL Injection and SecurityNext Topic ⮕
Common Table Expressions in SQLYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.