create user and grant privileges in oracle code example

Example 1: oracle sql create user

-- Oracle - User / schema creation
CREATE USER MY_USER IDENTIFIED BY "MyPassword"
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP PROFILE DEFAULT ACCOUNT UNLOCK;
GRANT CONNECT TO MY_USER;           -- To connect to DB
GRANT RESOURCE TO MY_USER;          -- Access / modify
GRANT MY_ROLE TO MY_USER;           -- Grants a role (to be defined)
ALTER USER MY_USER DEFAULT ROLE "RESOURCE","CONNECT","MY_ROLE";
-- Proxy access
ALTER USER MY_USER GRANT CONNECT THROUGH MY_OTHER_USER;
-- Checks privileges:
SELECT PRIVILEGE FROM DBA_SYS_PRIVS WHERE GRANTEE = MY_USER;

Example 2: oracle user privileges

-- List privileges, including roles, for MY_USER
SELECT GRANTEE,
       PRIVILEGE    AS PRIVILEGE_ROLE,
       NULL         AS OWNER,
       NULL         AS TABLE_NAME,
       NULL         AS COLUMN_NAME,
       ADMIN_OPTION AS PRIVILEGE,
       NULL         AS GRANTABLE
FROM all_SYS_PRIVS
WHERE GRANTEE = 'MY_USER'
UNION ALL
SELECT R.GRANTEE, R.GRANTED_ROLE AS PRIVILEGE_ROLE, P.OWNER, P.TABLE_NAME, 
	P.COLUMN_NAME, PRIVILEGE, P.GRANTABLE
FROM all_ROLE_PRIVS R
         LEFT JOIN ROLE_TAB_PRIVS P ON P.ROLE = R.GRANTED_ROLE
WHERE R.GRANTEE = 'MY_USER';

Tags:

Sql Example