GRANT

2025-11-19 10:08:13  Updated

Purpose

You can use this statement as a system administrator to grant object privileges, system privileges, and roles to users.

Required privileges

You must have the privileges that you want to grant, and privilege delegation must be enabled. For example, if you want to grant the SELECT privilege on the tbl1 table to user2 as user1, user1 must have the SELECT privilege on the tbl1 table, and the privilege delegation of the SELECT privilege on tbl1 must be enabled.

Note

After a user is granted a privilege, the privilege takes effect only after the user is reconnected to OceanBase Database.

Syntax

/*Grant object privileges*/
GRANT {obj_all_col_priv [, obj_all_col_priv...]} 
    ON obj_clause 
    TO {grant_user [, grant_user...]} 
    [WITH GRANT OPTION]

/* Grant system privileges or roles */
GRANT obj_all_col_priv [, obj_all_col_priv...] 
    TO grantee_clause 
    [WITH ADMIN OPTION]

grantee_clause:  
      grant_user [, grant_user...] 
    | grant_user IDENTIFIED BY password

obj_all_col_priv:  
      role 
    | sys_and_obj_priv [(column_list)] 
    | ALL [PRIVILEGES] [(column_list)]

Parameters

Parameter Description
obj_all_col_priv The privilege to be granted. You can grant privileges to users or roles. To grant multiple privileges to a user, separate the privileges with commas (,). For more information about the privilege types, see Privilege types in Oracle mode.
obj_clause The object on which the privilege is to be granted. You can specify the object in the following ways:
  • Use *.* to grant the privilege on all objects, namely all databases and all tables (*.*).
  • Use db_name.* to grant the privilege on the specified database, table_name to grant the privilege on the specified table, or db_name.table_name to grant the privilege on the specified table in the specified database.
  • Use [DIRECTORY] relation_name to grant the privilege on the specified directory.
grant_user The user or role to which the privilege is to be granted. Valid values:
  • user [USER_VARIABLE]: a specific user.
  • CONNECT: the CONNECT role.
  • RESOURCE: the RESOURCE role.
  • PUBLIC: the PUBLIC role.
IDENTIFIED BY password The password for the user to which the privilege is to be granted. The password is in plaintext and is saved in ciphertext on the server after it is saved to the dba_users table. Enclose special characters in the password in double quotation marks (""). Special characters are ~!@#%^&*_-+=`|(){}[]:;',.?/.
WITH GRANT OPTION Specifies whether to enable privilege delegation. When privilege delegation is enabled, grant revocation extends to dependent users.
WITH ADMIN OPTION Specifies whether to enable role delegation. When role delegation is enabled, grant revocation does not cascade to dependent users or roles.
role The role to be granted. Valid values:
  • role_name: the name of a custom role.
  • DBA: the database administrator role, with full database administration privileges. A user with the DBA role can perform any database operation.
  • RESOURCE: the RESOURCE role.
  • CONNECT: the CONNECT role.
  • PUBLIC: the PUBLIC role.

Examples

  • Grant the CREATE VIEW privilege to user1, and enable privilege delegation for the privilege.

    obclient> GRANT CREATE VIEW TO user1 WITH ADMIN OPTION;
    
  • Grant the CONNECT role to user1, and change the password for user1.

    obclient> GRANT CONNECT TO user1 IDENTIFIED by '********';
    

    Check the password of user1 in the dba_users table. The password is updated to the new one.

  • Grant the COMMENT ANY TABLE privilege to role1.

    GRANT COMMENT ANY TABLE TO role1;
    

References

  • For more information about how to view user privileges, see View user privileges.

  • For more information about how to view roles and role privileges, see View roles.

  • You can query the information about the created user in the dba_users table.

Contact Us