Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SQL Dictionary
  3. GRANT / REVOKE

GRANT / REVOKE

Grants privileges to database users (GRANT) or revokes them (REVOKE). Used for security management.

Syntax

-- Grants a privilege to a user.
GRANT privilege ON database_name.table_name TO 'user_name'@'host_name';

-- Grants a privilege along with the ability to delegate it to other users.
GRANT privilege ON database_name.table_name TO 'user_name'@'host_name' WITH GRANT OPTION;

-- Revokes a privilege from a user.
REVOKE privilege ON database_name.table_name FROM 'user_name'@'host_name';

-- Shows the privileges granted to a user (MySQL).
SHOW GRANTS FOR 'user_name'@'host_name';

Privilege List

PrivilegeDescription
SELECTAllows reading data.
INSERTAllows inserting data.
UPDATEAllows updating data.
DELETEAllows deleting data.
CREATEAllows creating tables and databases.
DROPAllows dropping tables and databases.
ALTERAllows modifying table definitions.
INDEXAllows creating and dropping indexes.
ALL PRIVILEGESGrants all privileges. Use this for administrator users.
WITH GRANT OPTIONAlso grants the ability to pass the received privileges on to other users.

Sample Code

-- Grants SELECT, INSERT, UPDATE, and DELETE to the application user.
GRANT SELECT, INSERT, UPDATE, DELETE
    ON shop_db.*
    TO 'app_user'@'localhost';

-- Creates a read-only report user.
GRANT SELECT
    ON shop_db.*
    TO 'report_user'@'%';

-- Grants access to a specific table only.
GRANT SELECT
    ON shop_db.products
    TO 'public_user'@'%';

-- Grants all privileges to the admin user.
GRANT ALL PRIVILEGES
    ON shop_db.*
    TO 'admin_user'@'localhost'
    WITH GRANT OPTION;

-- Revokes the SELECT privilege from the report user.
REVOKE SELECT
    ON shop_db.*
    FROM 'report_user'@'%';

-- Shows the privileges granted to the application user (MySQL).
SHOW GRANTS FOR 'app_user'@'localhost';

-- Applies privilege changes immediately (MySQL).
FLUSH PRIVILEGES;

Output

-- Example output of: SHOW GRANTS FOR 'app_user'@'localhost';
-- +-------------------------------------------------------------------------+
-- | Grants for app_user@localhost                                           |
-- +-------------------------------------------------------------------------+
-- | GRANT USAGE ON *.* TO `app_user`@`localhost`                           |
-- | GRANT SELECT, INSERT, UPDATE, DELETE ON `shop_db`.* TO `app_user`@`localhost` |
-- +-------------------------------------------------------------------------+

Syntax by Database

PostgreSQL uses a similar syntax to MySQL, but does not use the @host_name notation for specifying users. It supports role-based privilege management.

-- Grants privileges to a user (PostgreSQL).
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;

-- Shows the privileges granted to a user (PostgreSQL).
SELECT * FROM information_schema.role_table_grants WHERE grantee = 'app_user';

In Oracle, each user maps one-to-one with a schema. There are two types of privileges: system privileges and object privileges.

-- Grants object privileges (Oracle).
GRANT SELECT, INSERT ON shop_db.products TO app_user;

-- Grants system privileges (Oracle).
GRANT CREATE SESSION, CREATE TABLE TO app_user;

-- Shows the privileges granted to a user (Oracle).
SELECT * FROM DBA_TAB_PRIVS WHERE GRANTEE = 'APP_USER';

SQL Server uses schema-level privilege management, and role-based management with built-in roles such as db_datareader and db_datawriter is common.

-- Grants privileges on a table (SQL Server).
GRANT SELECT, INSERT, UPDATE, DELETE ON dbo.products TO app_user;

-- Adds a user to a built-in role (SQL Server).
ALTER ROLE db_datareader ADD MEMBER report_user;

-- Shows the privileges granted to a user (SQL Server).
EXEC sp_helpuser 'app_user';

SQLite is a file-based database and does not support the GRANT or REVOKE commands. Access control is managed through the file system's permissions.

Overview

GRANT and REVOKE are classified as DCL (Data Control Language). In production environments, follow the Principle of Least Privilege: grant only the permissions an application actually needs. For example, grant only SELECT to an API user that reads data, and only SELECT, INSERT, UPDATE, and DELETE to a user that writes data.

In MySQL, users are identified in the format user_name@host_name. % matches any host, while localhost allows local connections only. From a security standpoint, grant the minimum necessary privileges to any user accessible from outside.

For transaction control, see 'TRANSACTION / COMMIT / ROLLBACK'.

If you find any errors or copyright issues, please .