Back
Database Administration Commands
SQL User, Role, and Index Management
1. User Management
Creating a User
CREATE USER 'Ratnesh'@'localhost' IDENTIFIED BY 'Ratnesh@123';
- This command creates a new MySQL user named
Ratneshwith access limited tolocalhost. - The user is authenticated with the password
Ratnesh@123.
Viewing Existing Users
SELECT User, Host FROM mysql.user;
- Displays a list of all users and their associated host permissions.
Dropping a User
DROP USER 'Ratnesh'@'localhost';
- Deletes the user
Ratneshfrom the MySQL server.
2. Role Management
Creating a Role
CREATE ROLE 'developer';
- Defines a new role named
developer.
Viewing Role Grants
SHOW GRANTS FOR 'developer';
- Lists all privileges assigned to the
developerrole.
Dropping a Role
DROP ROLE 'developer';
- Deletes the
developerrole from the database.
3. Granting and Revoking Privileges
Granting Privileges to a Role
GRANT SELECT, INSERT, UPDATE ON Student_Society.* TO 'developer';
- Grants the
developerrole permissions to select, insert, and update data in all tables of theStudent_Societydatabase.
Viewing Granted Privileges
SHOW GRANTS FOR 'developer';
- Displays the privileges assigned to the
developerrole.
Revoking Privileges
REVOKE UPDATE ON Student_Society.* FROM 'developer';
- Removes
UPDATEprivilege from thedeveloperrole while retaining other granted privileges.
4. Index Management
Creating a Unique Index
CREATE UNIQUE INDEX Ratnesh ON Student_Society.society (SocID);
- Creates a unique index named
Ratneshon theSocIDcolumn of thesocietytable within theStudent_Societydatabase.
Viewing Indexes
SHOW INDEX FROM Student_Society.society;
- Displays existing indexes on the
societytable.
Alternatively, use:
SELECT INDEX_NAME, COLUMN_NAME, NON_UNIQUE
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_NAME = 'society'
AND TABLE_SCHEMA = 'Student_Society';
- Retrieves index details from the
INFORMATION_SCHEMA.STATISTICStable.
Dropping an Index
DROP INDEX Ratnesh ON Student_Society.society;
- Removes the
Ratneshindex from thesocietytable.
Confirming Index Removal
SHOW INDEX FROM Student_Society.society;
- Checks if the index has been successfully removed.