1.WEEK2 Privilege Role
1.WEEK2 Privilege Role
The root and every pluggable database (PDB) is considered a container. PDBs isolate
data and operations so that from the perspective of a user or application, each PDB
appears as if it were a traditional non-CDB.
A PDB is a portable collection of schemas, schema objects, and nonschema objects that
appears to an Oracle Net client as a non-CDB. All Oracle databases before Oracle
Database 12c were non-CDBs.
OS Authentication
Open the command prompt and then type:
$> sqlplus / as sysdba
--
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
SQL>
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
SQL>
SQL>
SQL> -- EZCONNECT
SQL> CONN sys/password@localhost:1521/orcl as sysdba
Connected.
SQL> CONN sys/password@localhost/orcl as sysdba
Connected.
SQL> -- tnsnames.ora
SQL> CONN sys/password@orcl as sysdba
Connected.
Session altered.
SQL> SHOW CON_NAME
Session altered.
SQL> -- EZCONNECT
SQL> connect adminpdb4/pdbpassword@localhost:1521/orclpdb4
username/password@hostname:port/service_name
SQL> -- tnsnames.ora
SQL> CONN system/password@orclpdb4 --you have to create a service naming in Oracle
Net Manager
Connected.
SQL>
4. Your connection is displayed in the Connections tab on the left side and a SQL worksheet is
opened automatically.
5.
6. Connect to the orclpdb4 pluggable database:
7. Connect to the orclpdb4 pluggable database using system or sys user (of orclpdb4)
Creating a Database User:
username password
Do exercise 1.
V. About Privileges and Roles
Authorization permits only certain users to access, process, or alter data; it also creates
limitations on user access or actions.
The limitations placed on (or removed from) users can apply to objects such as schemas,
entire tables, or table rows.
A user privilege is the right to run a particular type of SQL statement, or the right to access
an object that belongs to another user, run a PL/SQL package, and so on. The types of
privileges are defined by Oracle Database.
Roles are created by users (usually administrators) to group together privileges or other
roles. They are a way to facilitate the granting of multiple privileges or roles to users.
System privileges. These privileges allow the grantee to perform standard administrator tasks in the
database. Restrict them only to trusted users
Object privileges. Each type of object has privileges associated with it.
User roles. A role groups several privileges and roles, so that they can be granted to and revoked from
users simultaneously.
1. System privileges
A system privilege is the right to perform a particular action or to perform an action on any object
of a particular type. Objects include tables, views, materialized views, synonyms, indexes,
sequences, cache groups, replication schemes and PL/SQL functions, procedures and packages.
Privilege Description
ALTER ANY PROCEDURE Enables a user to alter any PL/SQL procedure, function or package in the
database.
ALTER ANY SEQUENCE Enables a user to alter any sequence in the database.
2. Object privileges
An object privilege is the right to perform a particular action on an object or to access another
user's object. Objects include tables, views, materialized views, indexes, synonyms, sequences,
cache groups, replication schemes and PL/SQL functions, procedures and packages.
3. Roles
A role groups several privileges and roles, so that they can be granted to and revoked from users
simultaneously. A role must be enabled for a user before it can be used by the user.
a. Creating a Role
b. Dropping a Role
1. GRANT
Use of the ADMIN Option to Enable Grantee Users to Grant the Privilege
The WITH ADMIN OPTION clause can be used to expand the capabilities of a privilege
grant.
User michael is able to not only use all of the privileges implicit in
the new_dba role, but he can also grant, revoke, and drop the new_dba role as
deemed necessary.
You can grant object privileges to users and roles, and enable the grantee to grant the
privilege to other users.
The following example grants the READ, INSERT, and DELETE object privileges for all
columns of the emp table to the users jfee and tsmith.
To grant all object privileges on the salary view to user jfee, use the ALL keyword as
shown in the following example:
Specify WITH GRANT OPTION to enable the grantee to grant the object privileges to other users and
roles.
The WITH GRANT OPTION clause with the GRANT statement can enable a grantee to grant
object privileges to other users.
User adams possesses the GRANT ANY OBJECT PRIVILEGE system privilege. He does not
possess any other grant privileges. He issues the following statement:
GRANT SELECT ON HR.EMPLOYEES TO blake WITH GRANT OPTION;
If you examine the DBA_TAB_PRIVS view, then you will see that hr is shown as the grantor of
the privilege:
FROM DBA_TAB_PRIVS
Now assume that user blake also has the GRANT ANY OBJECT PRIVILEGE system. He
issues the following statement:
In this case, when you query the DBA_TAB_PRIVS view again, you see that blake is shown as
being the grantor of the privilege:
The following statement grants the INSERT privilege on the acct_no column of
the accounts table to user psmith:
GRANT INSERT (acct_no) ON accounts TO psmith;
In the following example, object privilege for the ename and job columns of the emp table
are granted to the users jfee and tsmith:
2. REVOKE
Any user with the ADMIN option for a system privilege or role can revoke the privilege or role
from any other database user or role. The revoker does not have to be the user that originally
granted the privilege or role. Users with GRANT ANY ROLE can revoke any role.
The following statement revokes all object privileges for the dept table that you originally
granted to the human_resource role:
To list all the column-specific privileges that have been granted, you can use the following query:
SELECT GRANTEE, TABLE_NAME, COLUMN_NAME, PRIVILEGE
FROM DBA_COL_PRIVS;
If DBA access isn’t possible or necessary, it is also possible to slightly modify the above
queries to view the privileges solely for the current user.
This is done by alternatively querying USER_ versions of the above DBA_ views. Thus,
instead of looking at DBA_SYS_PRIVS we’d query USER_SYS_PRIVS, like so:
SELECT * FROM USER_SYS_PRIVS;
The SESSION_ROLES and SESSION_PRIVS data dictionary views list the current privilege domain of a
database session.
EXERCISE
I. Exercise 1: Do the following things using sqlplus and write the results
into your answer sheet.
1. Open sqlplus and connect to sys user.
2. Show the connection name of current container.
3. Create a pluggable database (PDB) and open this PDB.
4. Connect to the sys user (or ADMIN user of this PDB) in the above PDB.
5. Show the connection name of current container.
6. In this PDB, create 3 user: user1, user2, user3.
II. Exercise 2: Do the following things using sqlplus and write the results
into your answer sheet.
7. Connect to user1.
8. Connect to sys user (or ADMIN user) of PDB above.
9. Create a role named manager.
10.Grant CREATE SESSION, CREATE TABLE to manager role WITH ADMIN
OPTION.
11. Grant manager role to user1.
12.Connect to user1.
13.Create a TEST table (ID NUMBER, NAME VARCHAR2(100))
14. Grant manager role to user2.
15. Grant CREATE SESSION privilege to user2;
16. Connect to user 2, create a table.
17. From user2, grant CREATE SESSION privilege to user3.
18. Connect to SYS user of this PDB.
19. Grant manager role to user3 WITH ADMIN OPTION.
20. Connect to user3.
21.Grant manager role to user2.
22.From user3, how can you create a table in user2 schema?
23.From user3, query the roles and privilege of current user.
24.Use SQLDeveloper to connect to ADMIN USER of this PDB.
III. Exercise 3: Using sqlplus to do the following things and write out the
results:
1. Start SQL*Plus and connect to the Database.
2. CREATE a PLUGGABLE DATABASE (PDB) and open this PDB.
3. Using sys user to connect to the above PDB.
4. Show connection name.
5. Creating a tablespace.
6. CREATE 4 USERs: user1, user2, user3, user4, default tablespace and
quota 1M for each user on the tablespace above
7. Grant privilege to user1 and user4 so that these users can connect to the
database.
8. Show privilege of user1.
9. Create a programing role and grant privilege CREATE SESSION, CREATE
TABLE to this role.
10. Grant programing role to user2 with admin option.
11. Show the privileges of user2.
12. Connect to the database using user2.
13. From user2, create the employees table: ID, NAME, SALARY,
DESCRIPTION.
14. From user2, insert 2 rows to employees table.
15. From user2, grant programing role to user3 and grant update (name,
salary) on employees table to user3.
16. From user3, grant programing role to user1.
17. From user3, show privilege of user3.
18. From user3, query data from employees table.
19. From sys, grant select on employees to user1.
20. Show the privileges of user1.
21. From sys user, grant all on employee to user1.
22. From sys user, grant create sequence to user3
23. From user3, create table students (id, fullname, birthday)
24. From user3, Create sequence named student_seq
25. From user3, insert data into students table with ID is generated from the
sequence.
26. How can user4 insert data into students table of user3?
27. How can user4 delete data from students table of user3?