Lab - Manual Dbe 2023-24 Final
Lab - Manual Dbe 2023-24 Final
*Journal completion and timely submission are the pre-requisites for any practical evaluation.
Babu Madhav Institute of Information Technology
Course Credits: 04 Total Hours: 48 [Lectures: 04, Tutorial: 00, Practical: 02]
Prerequisites: Database Management Systems, Relational Database Management Systems
Course Objectives: To make the student acquaint with the internal operations and architecture of
database server, concurrency management, server configuration, network
architecture, database security and auditing, database performance, backup, and
recovery.
Programme PO1: Knowledge: Apply the fundamental knowledge of information technology
Outcomes: along with analytical, problem-solving and designing. Also to provide
communication skill for life-long learning in chosen field.
PO2: Problem Analysis and Solution: Identify, Analyse and provide the solution
for emerging real world problems with the help of theoretical and practical
understanding of tools and technologies.
PO3: Core Competence: To cultivate professionalism, ethics, and aesthetic to
become competent leader to serve the community.
PO4: Preparation: To Prepare the student for higher studies, research and
development and social upliftment. Also to provides skills which help students
to work and recognized themselves as an individual and as a team player.
Course Outcomes: CO1: Understand database architecture and analyse the process of server,
instances and storage structure
CO2: Manage database storage and concurrency
CO3: Know the concept of network architecture
CO4: Manage users and logs including the security aspects as well as the
parameters related to the log
CO5: Manage database performance and database tuning
CO6: Acquaint with different types of backup and recovery techniques.
Practical List
Concepts:
Nature of Handwritten
submission:
Learning Recourses:
The STARTUP command allows you to control the stage of the database instance.
1) NOMOUNT stage
In the NOMOUNT stage, Oracle carries the following steps:
First, search for a server parameter file in the default location. You can override the default
behavior by using the SPFILE or PFILE parameters in the STARTUP command.
Next, read the parameter file to get the values of the initialization parameters.
Then, allocate the system global area (SGA) based on the initialization parameter settings.
After that, start the Oracle background processes such as SMON, PMON, and LGWR.
Finally, open the alert log and trace files and record all explicit parameters to the alert log in the
valid parameter syntax.
At the NOMOUNT stage, Oracle does not associate the database with the instance.
2) MOUNT stage
In the MOUNT stage, Oracle associates a database with an instance. In other words, the instance mounts
the database.
The instance carries the following steps to mount a database:
First, get the name of the database control files specified in the CONTROL_FILE initialization
parameter.
Second, open the control files.
Third, find the name of data files and the online redo log files.
When a database is mounted, the database is only available to database administrators, not all users.
3) OPEN stage
In the OPEN stage, Oracle performs the following actions:
First, open the online data files in tablespaces other than the undo tablespaces.
Then, select an undo tablespace. The instance uses default undo tablespace if an undo tablespace is
specified in the UNDO_TABLESPACE initialization parameter. Otherwise, it will select the first
available undo tablespace.
Finally, open the online redo log files.
When Oracle opens a mounted database, the database is available for normal operations.
The following picture illustrates the Oracle database startup process:
For example, to start up a database instance in the NOMOUNT stage, you use the following command:
STARTUP NOMOUNT;
Code language: SQL (Structured Query Language) (sql)
To bring the database to the next stage, you use the ALTER DATABASE statement. For example, this
statement brings the database from the NOMOUNT to the MOUNT stage:
ALTER DATABASE MOUNT;
Code language: SQL (Structured Query Language) (sql)
Oracle STARTUP command example
First, launch the SQL*Plus program and log in to the Oracle Database as the SYS user.
Second, issue the SHUTDOWN IMMEDIATE command to shut down the database:
shutdown immediate;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
Database closed.
Database dismounted.
ORACLE instance shut down.
Code language: SQL (Structured Query Language) (sql)
Third, start the database instance at the OPEN stage:
startup
Code language: SQL (Structured Query Language) (sql)
Here is the output:
status
FROM
v$instance;
Code language: SQL (Structured Query Language) (sql)
Now, the database is open and available for normal operations.
INSTANCE_NAME STATUS
---------------- ------------
orcl OPEN
Oracle SHUTDOWN
Summary: in this tutorial, you will learn how to use the Oracle SHUTDOWN statement to shut down the
Oracle Database.
Use the SHUTDOWN IMMEDIATE command to shut down the Oracle Database gracefully:
SHUTDOWN IMMEDIATE
Code language: SQL (Structured Query Language) (sql)
Introduction to the Oracle SHUTDOWN statement
To shut down a currently running Oracle Database instance, you use the SHUTDOWN command as follows:
SHUTDOWN [ABORT | IMMEDIATE | NORMAL | TRANSACTIONAL [LOCAL]]
Code language: SQL (Structured Query Language) (sql)
Let’s examine each option of the SHUTDOWN command.
SHUTDOWN NORMAL
The SHUTDOWN NORMAL option waits for the current users to disconnect from the database before
shutting down the database. The database instance will not accept any further database
connection. The SHUTDOWN NORMAL does not require an instance recovery on the next database startup.
The NORMAL is the default option if you don’t explicitly specify any option.
Therefore SHUTDOWN and SHUTDOWN NORMAL commands have the same effect.
The SHUTDOWN or SHUTDOWN NORMAL is not really practical because you practically cannot wait for all
users to come back to their desks and disconnect from the database.
SHUTDOWN TRANSACTIONAL
The SHUTDOWN TRANSACTIONAL waits for all uncommitted transactions to complete before shutting down
the database instance. This saves the work for all users without requesting them to log off.
The database instance also does not accept any new transaction after a SHUTDOWN TRANSACTIONAL .
When completing all transactions, the database instance disconnects all the currently connected users from
the database and shuts down.
The SHUTDOWN TRANSACTIONAL does not require any instance recovery procedure on the next database
startup.
The optional LOCAL mode waits for only local transactions to complete, not all the transactions. Then it
shuts down local instance. This option is useful in some cases e.g., a scheduled outage maintenance.
SHUTDOWN ABORT
The SHUTDOWN ABORT is not recommended and only used on some occasions. The SHUTDOWN
ABORT has a similar effect as you unplug the power of the server. The database will be in an inconsistent
state. Therefore, you should never use the SHUTDOWN ABORT command before backing up the database. If
you try to do so, you may not be able to recover the backup.
It is recommended to use the SHUTDOWN ABORT only when you want to shut down the database
instantaneously. For example, if you know a power shutdown is going to happen in a minute or you
experience some problems when starting up a database instance.
The SHUTDOWN ABORT proceeds with the fastest possible shutdown of the database. However, it requires
instance recovery on the next database startup.
SHUTDOWN IMMEDIATE
The SHUTDOWN IMMEDIATE is the most common and practical way to shut down the Oracle database.
The SHUTDOWN IMMEDIATE does not wait for the current users to disconnect from the database or current
transactions to complete.
During the SHUTDOWN IMMEDIATE, all the connected sessions are disconnected immediately, all
uncommitted transactions are rolled back, and the database completely shuts down.
After issuing the SHUTDOWN IMMEDIATE statement, the database will not accept any new connection. The
statement will also close and dismount the database.
Unlike the SHUTDOWN ABORT option, the SHUTDOWN IMMEDIATE option does not require an instance
recovery on the next database startup.
The following table illustrates the differences between the shutdown modes:
Shutdown Modes A I T N
Shutdown Modes:
A = ABORT
I = IMMEDIATE
T = TRANSACTIONAL
N = NORMAL
Notes
To issue the SHUTDOWN statement, you must connect to the database as SYSDBA, SYSOPER, SYSBACKUP,
or SYSDG. If the current database is a pluggable database, the SHUTDOWN statement will close the
pluggable database only. The consolidated instance will continue to run. On the other hand, if the current
database is a CDB, the SHUTDOWN statement will close the CDB instance.
Oracle SHUTDOWN statement example
First, launch SQL*Plus:
> sqlplus
Code language: SQL (Structured Query Language) (sql)
Second, log in to the Oracle database using the SYS user:
Enter user-name: sys as sysdba
Enter password: <sys_password>
Code language: SQL (Structured Query Language) (sql)
Third, check the current status of the Oracle instance:
SQL> select instance_name, status from v$instance;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
INSTANCE_NAME STATUS
---------------- ------------
orcl OPEN
Code language: SQL (Structured Query Language) (sql)
Fourth, issue the SHUTDOWN IMMEDIATE command:
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
Code language: SQL (Structured Query Language) (sql)
In this tutorial, you have learned how to use the Oracle SHUTDOWN statement to shut down the Oracle
Database.
PROBLEM STATEMENTS
SN Problem Statements
1. When the Oracle Database starts an instance, it goes through the 3 stages. Describe the operations
performed in all three stages giving diagram.
10. An instance is started with NOMOUNT option. How do you bring the database into mount stage?
11. An instance is started with MOUNT option. How do you bring the database into open stage?
14. List the name and the current status of the database instance.
d) CREATE LISTENER
5. Which command is used to start the Oracle Enterprise Manager Database Control?
a) EMCTL START DBCONTROL
b) EMCTL STOP DBCONTROL
c) EMCTL START AGENT
d) EMCTL STOP AGENT
Answers:
1. STARTUP
2. SHUTDOWN
3. STARTUP LISTENER
4. SHUTDOWN LISTENER
5. EMCTL START DBCONTROL
4. How does Oracle associate a database with an instance during the MOUNT stage?
During the MOUNT stage, Oracle associates a database with an instance by getting the name of
the control files, opening the control files, and finding the data files and online redo log files.
5. What actions are performed during the OPEN stage of the startup process?
During the OPEN stage, Oracle opens the online data files, selects an undo tablespace, and opens
the online redo log files.
10. How does the SHUTDOWN IMMEDIATE option differ from the SHUTDOWN ABORT option?
The SHUTDOWN IMMEDIATE option shuts down the database immediately, disconnects all
sessions, rolls back uncommitted transactions, and does not require instance recovery on the
next startup.
12. What does the SHUTDOWN statement do when the current database is a pluggable database?
If the current database is a pluggable database, the SHUTDOWN statement will close the
pluggable database only. The consolidated instance will continue to run.
13. How can you check the current status of the Oracle instance?
You can check the current status of the Oracle instance by querying the v$instance view.
Concepts:
Nature of Handwritten
submission:
Learning Recourses:
Oracle Tablespace
Summary: in this tutorial, you will learn about the Oracle tablespace and how Oracle uses tablespaces to
logically store the data in the database.
What is an Oracle Tablespace
Oracle divides a database into one or more logical storage units called tablespaces.
Each tablespace consists of one or more files called datafiles. A datafile physically stores the data objects of
the database such as tables and indexes on disk.
In other words, Oracle logically stores data in the tablespaces and physically stores data in datafiles
associated with the corresponding tablespaces.
The following picture illustrates the relationship between a database, tablespaces, and datafiles:
Note that the SYSTEM tablespace must always be online because it contains the data dictionary that must be
available to Oracle.
Normally, a tablespace is online so that its data is available to users. However, you can take a tablespace
offline to make data inaccessible to users when you update and maintain the applications.
In case of some errors such as hardware failures, Oracle automatically takes an online tablespace offline. Any
attempt to access data in offline tablespace will result in an error.
Read-Only Tablespaces
The read-only tablespaces allow Oracle to avoid performing backup and recovery of large, static parts of a
database. Because Oracle doesn’t update the files of a read-only tablespace, you can store the files on the
read-only media.
Oracle allows you to remove objects such as tables and indexes from a read-only tablespace. However, it
does not allow you to create or alter objects in a read-only tablespace.
When you create a new tablespace, it is in the read-write mode. To change a tablespace to read-only
tablespace, you use the ALTER TABLESPACE command with the READ ONLY option.
file_name,
bytes / 1024/ 1024 MB
FROM
dba_data_files;
Code language: SQL (Structured Query Language) (sql)
Here are all the tablespaces in the current database:
The CREATE TABLESPACE is quite complex with many options, you can find more information from the
Oracle CREATE TABLESPACE page.
Tablespaces and CREATE TABLE statement
When you create a new table, Oracle automatically places the table in the default tablespace of
the user which you use to create the table. However, you can explicitly specify the tablespace to which the
table belongs as shown in the following query:
CREATE TABLE table_name(
...
)
TABLESPACE tablespace_name;
Code language: SQL (Structured Query Language) (sql)
Note that you must have privilege on the tablespace that you specify in the CREATE TABLE statement.
Consider the following example.
First, create a new table called t1 whose tablespace is tbs1:
CREATE TABLE t1(
id INT GENERATED ALWAYS AS IDENTITY,
c1 VARCHAR2(32)
) TABLESPACE tbs1;
Code language: SQL (Structured Query Language) (sql)
Second, insert 10,000 rows into the t1 table:
BEGIN
FOR counter IN 1..10000 loop
INSERT INTO t1(c1)
VALUES(sys_guid());
END loop;
END;
/
Code language: SQL (Structured Query Language) (sql)
Third, check the free space of the tbs1 tablespace by querying from the dba_free_space view:
SELECT
tablespace_name,
bytes / 1024 / 1024 MB
FROM
dba_free_space
WHERE
tablespace_name = 'TBS1';
Code language: SQL (Structured Query Language) (sql)
Fourth, insert 10,000 rows into the t1 table, Oracle will issue an error due to insufficient storage in the
tablespace:
BEGIN
FOR counter IN 1..10000 loop
INSERT INTO t1(c1)
VALUES(sys_guid());
END loop;
END;
/
Code language: SQL (Structured Query Language) (sql)
Here is the error message:
ORA-01653: unable to extend table OT.T1 by 8 in tablespace TBS1
Code language: SQL (Structured Query Language) (sql)
To fix this, you can resize the tablespace using the following ALTER DATABASE statement:
ALTER DATABASE
DATAFILE 'tbs1.dbf'
RESIZE 10m;
Code language: SQL (Structured Query Language) (sql)
If you insert 10,00 rows into the t1 table again, it should work.
The second way to avoid this issue, when creating a new tablespace, you can use the AUTOEXTEND ON clause
as follows:
CREATE TABLESPACE tbs1
DATAFILE 'tbs1.dbf'
SIZE 1m
AUTOEXTEND 20m;
Code language: SQL (Structured Query Language) (sql)
Note that you cannot drop the SYSTEM tablespace and only can drop the SYSAUX tablespace when you
started the database in the MIGRATE mode.
You need to have the DROP TABLESPACE system privilege to execute the DROP TABLESPACE statement. To
drop the SYSAUX tablespace, you need to have the SYSDBA system privilege.
Oracle DROP TABLESPACE statement examples
Let’s take some examples of using the DROP TABLESPACE statement.
1) Using Oracle DROP TABLESPACE to remove an empty tablespace example
First, create a new tablespace named tbs1:
CREATE TABLESPACE tbs1
DATAFILE 'tbs1_data.dbf'
SIZE 10m;
Code language: SQL (Structured Query Language) (sql)
Second, use the DROP TABLESPACE to remove the tbs1 tablespace:
DROP TABLESPACE tbs1;
Code language: SQL (Structured Query Language) (sql)
2) Using Oracle DROP TABLESPACE to remove a non-empty tablespace example
First, create a new tablespace named tbs2:
CREATE TABLESPACE tbs2
DATAFILE 'tbs2_data.dbf'
SIZE 5m;
Code language: SQL (Structured Query Language) (sql)
Second, create a new table t2 in the tablespace tbs2:
CREATE TABLE t2 (
c1 INT
) TABLESPACE tbs2;
Code language: SQL (Structured Query Language) (sql)
Third, use the DROP TABLESPACE statement to drop the tbs2 tablespace:
DROP TABLESPACE tbs2;
Code language: SQL (Structured Query Language) (sql)
Oracle issued the following error:
ORA-01549: tablespace not empty, use `INCLUDING CONTENTS` option
Code language: SQL (Structured Query Language) (sql)
To drop the tbs2 tablespace, we need to use the INCLUDING CONTENTS option:
DROP TABLESPACE tbs2
INCLUDING CONTENTS;
Code language: SQL (Structured Query Language) (sql)
Oracle issued the following message indicating that the tablespace has been dropped:
Tablespace dropped.
Code language: SQL (Structured Query Language) (sql)
3) Using Oracle DROP TABLESPACE to remove a tablespace whose tables are referenced by referential
constraints
First, create two tablespaces named tbs3 and tbs4:
CREATE TABLESPACE tbs3
DATAFILE 'tbs3_data.dbf'
SIZE 5m;
RESIZE size;
Code language: SQL (Structured Query Language) (sql)
Consider the following example.
First, create a new tablespace called tbs11:
CREATE TABLESPACE tbs11
DATAFILE 'tbs11.dbf'
SIZE 1m;
Code language: SQL (Structured Query Language) (sql)
Next, create a new table called t2 that uses tbs11 as the tablespace:
CREATE TABLE t2(
c INT PRIMARY KEY
) TABLESPACE tbs11;
Code language: SQL (Structured Query Language) (sql)
Then, query the size of the tablespace tbs11:
SELECT
tablespace_name,
bytes / 1024 / 1024 MB
FROM
dba_free_space
WHERE
tablespace_name = 'TBS11';
Code language: SQL (Structured Query Language) (sql)
The following illustrates the output:
TABLESPACE_NAME MB
--------------- ----------
TBS11 .9375
Code language: SQL (Structured Query Language) (sql)
After that, use the ALTER DATABASE to extend the size of the datafile of the tablespace to 15MB:
ALTER DATABASE
DATAFILE 'tbs11.dbf'
RESIZE 15m;
Code language: SQL (Structured Query Language) (sql)
Finally, query the size of the tbs11 tablespace:
SELECT
tablespace_name,
bytes / 1024 / 1024 MB
FROM
dba_free_space
WHERE
tablespace_name = 'TBS11';
Code language: SQL (Structured Query Language) (sql)
Here is the output:
TABLESPACE_NAME MB
---------------- ----------
TBS11 14.9375
Code language: SQL (Structured Query Language) (sql)
As you can see, the size of the tablespace tbs11 has been extended to 15MB.
Note that Oracle does not allow you to add a datafile to a bigfile tablespace, therefore, you only can
use ALTER DATABASE DATAFILE RESIZE command.
status
FROM
dba_temp_files;
Code language: SQL (Structured Query Language) (sql)
PROBLEM STATEMENTS
2. When creating a tablespace, what three main choices a DBA has to make?
4. The syntax for creating a tablespace is given below. Explain the keywords given in bold letter.
CREATE
[ SMALLFILE | BIGFILE ]
TABLESPACE tablespace_name
DATAFILE 'filename'
[ SIZE integer [ K | M | G | T | P | E ] ]
[ MAXSIZE { UNLIMITED | } [ K | M | G | T | P | E ] } ]
5. Create a new tablespace named tbs1 with data file D:\DBA\tbs1_data.dbf of size 1MB.
6. Create a new table table1 (Id integer) that belongs to tablespace tbs1.
10. How do you bring the tablespce tbs1 in read write mode?
12. Remove the tablespace tbs1 in such a way that all contents also get deleted along with data files.
13. Remove the tablespace tbs1 in such a way that all contents also get deleted but not the data files.
14. What happen when you attempt to remove a tablespace that has objects without specifying the
INCLUDING CONTENTS option?
17. Create a tablespace tbs1. Create a table table1 that belongs to tablespace tbs1. Now remove
tablespace tbs1 by giving following command: DROP TABLESPACE tbs1;
18. Add a new data file D:\DBA\tbs1_data2.dbf of size 10MB and allow the data file to extend
automatically when it is required.
19. What happens when you create a user without specifying a temporary tablespace?
9. Which command is used to move a table from one tablespace to another in Oracle?
10. Which command is used to change the default tablespace for a user in Oracle?
Answers:
1. a) CREATE TABLESPACE
2. b) ALTER TABLESPACE
3. c) DROP TABLESPACE
4. d) RENAME TABLESPACE
5. a) ADD DATAFILE
6. a) RESIZE DATAFILE
7. c) DROP DATAFILE
8. d) RENAME DATAFILE
9. a) MOVE TABLE
10. a) ALTER USER
3. Can you control the storage size allocated for the database data using tablespaces?
Yes, tablespaces allow you to control the storage size allocated for the database data. You can specify the
size of the tablespace when creating it.
There are multiple ways to extend a tablespace in Oracle. You can add a new datafile to the tablespace
using the ALTER TABLESPACE statement or resize the existing datafile using the ALTER DATABASE
statement.
12. How can you remove a tablespace from the database in Oracle?
To remove a tablespace from the database in Oracle, you can use the DROP TABLESPACE statement.
However, you need to ensure that the tablespace is empty or specify the INCLUDING CONTENTS option to
remove its contents.
14. How can you check the default temporary tablespace in Oracle?
You can check the default temporary tablespace in Oracle by querying the DATABASE_PROPERTIES view
and filtering for the DEFAULT_TEMP_TABLESPACE property.
Keywords/ CREATE USER, DROP USER, ALTER USER, CREATE ROLE, GRANT, PRIVILEGES
Concepts:
Nature of Handwritten
submission:
Learning Recourses:
Summary: in this tutorial, you will learn how to use the Oracle CREATE USER statement to create a new user
in the Oracle database.
The CREATE USER statement allows you to create a new database user which you can use to log in to the
Oracle database.
In this syntax:
IDENTIFIED BY password
Specify a password for the local user to use to log on to the database. Note that you can create an external or
global user, which is not covered in this tutorial.
DEFAULT TABLESPACE
Specify the tablespace of the objects such as tables and views that the user will create.
If you skip this clause, the user’s objects will be stored in the database default tablespace if available,
typically it is USERS tablespace; or the SYSTEM tablespace in case there is no database default tablespace.
QUOTA
Specify the maximum of space in the tablespace that the user can use. You can have multiple QUOTA clauses,
each for a tablespace.
Use UNLIMITED if you don’t want to restrict the size in the tablespace that user can use.
PROFILE profile
A user profile limits the database resources or password that the user cannot exceed. You can assign a profile
to a newly created user. If you skip this clause, Oracle will assign the DEFAULT profile to the user.
PASSWORD EXPIRE
Use the PASSWORD EXPIRE if you want to force the user to change the password for the first time the user
logs in to the database.
Use ACCOUNT LOCK if you want to lock user and disable access. On the other hand, specify ACCOUNT
UNLOCK to unlock user and enable access.
To execute the CREATE USER statement, you must have the CREATE USER system privilege. Once you create
the new user, the privilege domain of the user will be empty. Therefore, if you want to the user to be able to
login to the database, you should grant the CREATE SESSION system privilege to the user.
1) Using Oracle CREATE USER statement to create a new local user example
This example uses the CREATE USER statement to create a new local user named john with the
password abcd1234:
Oracle issues the following output indicating that user john has been created successfully.
To find a list of users with the OPEN status, you query the information from the dba_users:
SELECT
username,
default_tablespace,
profile,
authentication_type
FROM
dba_users
WHERE
account_status = 'OPEN';
Code language: SQL (Structured Query Language) (sql)
As you can see from the output, user john has a default tablespace as USERS, profile as DEFAULT, and log in
to the database using a PASSWORD.
ERROR: ORA-01045:
user JOHN lacks CREATE SESSION privilege; logon denied
To enable the user john to log in, you need to grant the CREATE SESSION system privilege to the user john by
using the following statement:
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
Code language: SQL (Structured Query Language) (sql)
2) Using Oracle CREATE USER statement to create a new local user with password expired example
First, use the CREATE USER statement to create a new user jane:
SELECT
username,
default_tablespace,
profile,
authentication_type
FROM
dba_users
WHERE
account_status = 'OPEN';
Code language: SQL (Structured Query Language) (sql)
Third, grant the CREATE SESSION privilege to the user jane so that you can use this user to log in the Oracle
database.
Finally, use the user jane to log in to the database via the SQL*plus program:
Oracle requested for changing the password for jane, you must provide the new password and confirm it
before you can log in:
The following illustrates the basic syntax of the DROP USER statement:
In this syntax, you need to specify the user that you want to drop after the DROP USER keywords.
If the user whose schemas contain objects such as views and tables, you need to delete all schema objects of
the user first and then drop the user.
Deleting all schema objects of the users first before removing the user is quite tedious. Therefore, Oracle
provides you with the CASCADE option.
If you specify the CASCADE option, Oracle will remove all schema objects of the user before deleting the user.
If the schema objects of the dropped user are referenced by objects in other schemas, Oracle will invalidate
these objects after deleting the user.
If a table of the dropped user is referenced by materialized views in other schemas, Oracle will not drop these
materialized views. However, the materialized views can no longer be refreshed because the base table
doesn’t exist anymore.
Note that Oracle does not drop roles created by the user even after it deletes the user.
Notice that if you attempt to delete the user SYS or SYSTEM, your database will corrupt.
1) Using Oracle DROP USER to remove a user that has no schema object
First, log in to the Oracle database using the user ot using SQL*Plus:
Third, drop the user foo using the DROP USER statement:
User dropped.
Code language: SQL (Structured Query Language) (sql)
Because the user foo has no schema objects, you could delete it without specifying the CASCADE option.
2) Using Oracle DROP USER to delete a user that has schema objects
First, create a new user called bar and grant the CREATE SESSION and CREATE TABLE system privileges to the user:
GRANT
CREATE SESSION,
CREATE TABLE
TO bar;
Code language: SQL (Structured Query Language) (sql)
Fourth, go back to the session of the user ot and drop user bar:
Fifth, end the user bar’s session first using the exit command:
exit
Code language: SQL (Structured Query Language) (sql)
And issue the DROP USER statement again in the user ot session:
You could not delete the user bar without specifying CASCADE because the user bar has the table t1 as a schema
object.
Seventh, use the DROP USER statement with CASCADE option to delete the user bar:
Oracle could delete the user bar and also the table t1.
The ALTER USER statement allows you to change the authentication or database resource characteristics of a
database user.
Generally speaking, to execute the ALTER USER statement, your account needs to have the ALTER USER system
privilege. However, you can change your own password using the ALTER USER statement without having
the ALTER USER system privilege.
Let’s create a user named dolphin and grant the CREATE SESSION system privilege to dolphin:
1) Using Oracle ALTER USER statement to change the password for a user
The following example uses the ALTER USER statement to change the password for the user dolphin:
The user dolphin should be able to authenticate to the Oracle Database using the new password xyz123
If you use the user dolphin to log in to the Oracle Database, you should see a message indicating that the user
is locked:
Now, the user dolphin should be able to log in to the Oracle Database.
When you use the user dolphin to log in to the database, Oracle issues a message indicating that the password
has expired and requests for the password change as follows:
4) Using Oracle ALTER USER statement to set the default profile for a user
This statement returns the profile of the user dolphin:
SELECT
username,
profile
FROM
dba_users
WHERE
username ='DOLPHIN';
Code language: SQL (Structured Query Language) (sql)
When you create a new user without specifying a profile, Oracle will assign the DEFAULT profile to the user.
5) Using Oracle ALTER USER statement to set default roles for a user
Currently, the user dolphin has no assigned roles as shown in the output of the following query when
executing from the dolphin’s session:
First, create a new role called rescue from the user OT‘s session:
Third, use the user dolphin to log in to the Oracle Database. The default role of the user dolphin is rescue now.
ROLE
---------
RESCUE
Code language: SQL (Structured Query Language) (sql)
Fourth, create another role called super and grant all privileges to this role:
Seventh, disconnect the current session of the user dolphin and log in to the Oracle Database again. The
default role of the user dolphin should be super as shown in the output of the following query:
ROLE
---------
SUPER
To create a new role, you use the CREATE ROLE statement. The basic syntax of the CREATE ROLE statement is as
follows:
In this syntax:
First, specify the name of the role that you want to create.
Second, use IDENTIFIED BY password option to create a local role and indicate that the user, who was
granted the role, must provide the password to the database when enabling the role.
Third, use NOT IDENTIFIED to indicate that the role is authorized by the database and the user, who was
granted this role, don’t need a password to enable the role.
After a role is created, it is empty. To grant privileges to a role, you use the GRANT statement:
In addition, you can use the GRANT statement to grant privileges of a role to another role:
First, create a new role named mdm (master data management) in the sample database:
Third, create a new user named alice and grant the CREATE SESSION privilege to alice:
Go back to the first session and grant alice the mdm role:
Go to the alice’s session and enable role using the SET ROLE statement:
To query all roles of the current user, you use the following query:
ROLE
------
MDM
Code language: SQL (Structured Query Language) (sql)
Now, alice can manipulate data in the master data tables such as customers and employees.
2) Using Oracle CREATE ROLE to create a role with IDENTIFIED BY password example
First, create a new role named order_entry with the password xyz123:
Next, grant object privileges on the orders and order_items tables to the order_entry role:
After that, log in as alice and enable the order_entry role by using the SET ROLE statement:
SET ROLE
order_entry IDENTIFIED BY xyz123,
mdm;
Code language: SQL (Structured Query Language) (sql)
Finally, use the following statement to get the current roles of alice:
ROLE
-------------
MDM
ORDER_ENTRY
PROBLEM STATEMENTS
SN Problem Statements
1. Create a user user1 with password user1_pass. This password must expires one’s the user logged in
for first time. The default tablespace for this user should be user1_tbs. This user’s account should be
unlocked immediately after user creation.
2. Create a user user2 with password user2_pass. This password should not expires. The default
tablespace for this user should be user1_tbs. This user’s account should be locked immediately after
user creation.
4. Try to connect the newly created user user1 with database. What output you get? Justify it.
5. How do you grant CREATE SESSION system privilege to the user user1?
6. What is privilege? What are the 2 types of privileges? Give some examples of each.
11. How do you allows user1 to select data from any table in any schema in the database?
12. How to you allow user2 to insert and update records on table1 which is created by user1.
13. Explain revoke statement with syntax. What is the significance of with admin option in revoke
statement?
14. How do you revoke the insert and update privileges from user2?
16. How do you get back the privileges from user1 so that he can not select data from any table in any
schema in the database?
17. How do you revoke the privileges form user2 to insert and update records on table1 which is created
by user1.
19. Do a user requires to have ALTER USER system privilege to change it’s own password?
32. Create a user user3 with password user3_pass. The password should not expires. The default
tablespace for this user should be user3_tbs. Also assign a temporary tablespace temp_tbs to this
user. This user’s account should be locked immediately after user creation. Allow unlimited space on
user3_tbs to this user.
38. Modify user3 so that its password get expired when user logged in next time.
3. Which statement is true about the DROP USER command in Oracle Database?
A) It removes the user's privileges from all objects in the database.
B) It deletes the user and all objects owned by the user from the database.
C) It deletes only the user account but retains all the objects owned by the user.
D) It is not allowed in Oracle Database.
4. To grant specific privileges on a table to another user, which command is used in Oracle Database?
A) GRANT OBJECT
B) GRANT ACCESS
C) GRANT PERMISSIONS
D) GRANT
5. What is the purpose of the WITH ADMIN OPTION clause in the GRANT command?
A) It grants administrative privileges to the user.
B) It allows the user to grant the same privilege to other users.
C) It grants all privileges available in the database to the user.
D) It revokes all administrative privileges from the user.
6. Which command is used to revoke a previously granted privilege from a user in Oracle Database?
A) REVOKE
B) DENY
C) REMOVE
D) DELETE PRIVILEGE
7. When granting privileges to a user, which option allows the user to use the specified privileges only on
objects owned by the user?
A) WITH ADMIN OPTION
B) WITH GRANT OPTION
C) WITH OBJECT OPTION
D) WITH LOCAL OPTION
8. Which command is used to view the privileges granted to a specific user in Oracle Database?
A) SHOW USER PRIVILEGES
B) LIST USER PRIVILEGES
C) DESCRIBE USER PRIVILEGES
D) SELECT * FROM USER_SYS_PRIVS
9. Which command is used to change the password for a user in Oracle Database?
A) CHANGE USER PASSWORD
B) ALTER USER PASSWORD
C) UPDATE USER PASSWORD
D) PASSWORD USER
10. When using the CREATE USER command, which option specifies the default tablespace for the user?
A) DEFAULT TABLESPACE
B) TABLESPACE DEFAULT
C) USER TABLESPACE
D) USER DEFAULT TABLESPACE
11. Question 3: Which command is used to remove a user from the Oracle Database without deleting the
user's objects?
A) DROP USER
B) DELETE USER
C) REMOVE USER
D) DISABLE USER
12. What happens when you use the CASCADE clause with the DROP USER command in Oracle Database?
A) It drops all users associated with the current user.
B) It drops all objects owned by the user without deleting the user account.
C) It drops all objects in the database without deleting any user accounts.
D) It drops all users and objects associated with the current user.
13. Which command is used to modify the quota for a specific tablespace for a user in Oracle Database?
A) ALTER QUOTA
B) MODIFY TABLESPACE QUOTA
C) ALTER USER QUOTA
D) SET TABLESPACE QUOTA
14. Question 6: To revoke all privileges from a user in Oracle Database, which command is used?
A) REVOKE ALL
B) DELETE ALL PRIVILEGES
C) DROP ALL PRIVILEGES
D) REVOKE ALL PRIVILEGES
15. What is the purpose of the IDENTIFIED BY clause in the CREATE USER command?
A) To specify the user's password.
B) To specify the user's tablespace.
C) To specify the user's privileges.
D) To specify the user's role.
16. Question 8: Which command is used to grant a role to a user in Oracle Database?
A) GRANT ROLE
B) ADD ROLE
C) GRANT USER ROLE
D) SET ROLE
Answers:
1. C) CREATE USER
2. D) To modify user account settings.
3. B) It deletes the user and all objects owned by the user from the database.
4. D) GRANT
5. B) It allows the user to grant the same privilege to other users.
6. A) REVOKE
7. B) WITH GRANT OPTION
8. D) SELECT * FROM USER_SYS_PRIVS
9. B) ALTER USER PASSWORD
10. A) DEFAULT TABLESPACE
11. A) DROP USER
12. B) It drops all objects owned by the user without deleting the user account.
13. C) ALTER USER QUOTA
14. D) REVOKE ALL PRIVILEGES
15. A) To specify the user's password.
16. A) GRANT ROLE
CREATE USER username IDENTIFIED BY password [DEFAULT TABLESPACE tablespace] [QUOTA {size |
UNLIMITED} ON tablespace] [PROFILE profile] [PASSWORD EXPIRE] [ACCOUNT {LOCK | UNLOCK}];
2. How can you set the default tablespace for a user while creating them?
The "QUOTA" clause specifies the maximum space in the tablespace that the user can use.
By using the GRANT statement followed by the type of privilege and the user or role to which the
privilege is granted.
5. What is the purpose of the CASCADE option in the DROP USER statement?
The CASCADE option allows you to delete a user and all their schema objects.
6. How can you change a user's password using the ALTER USER statement?
By using the ALTER USER statement followed by the "IDENTIFIED BY" clause and the new password.
Roles help manage privileges more efficiently by grouping related privileges and granting them to
users.
8. How can you force a user to change their password on the first login?
10. How can you view the privileges granted to a user in Oracle Database?
11. What is the purpose of the "WITH GRANT OPTION" clause in the GRANT command?
12. When using the DROP USER statement, what happens to the user's schema objects by default?
They are also dropped along with the user unless you use the CASCADE option.
13. How can you set a role's password when creating it with the CREATE ROLE statement?
14. What does the "NOT IDENTIFIED" option do in the CREATE ROLE statement?
It indicates that the role is authorized by the database and doesn't require a password to be enabled.
15. How can you enable a role for a user in Oracle Database?
16. What is the purpose of the ALTER USER statement in Oracle Database?
The ALTER USER statement is used to modify the authentication or database resource characteristics
of a database user.
17. How can you lock a user account in Oracle Database using the ALTER USER statement?
18. What option can you use to unlock a locked user account in Oracle Database?
19. What is the significance of the PASSWORD EXPIRE option in the ALTER USER statement?
It forces the user to change the password for the first time they log in to the database.
20. How can you change the default profile for a user in Oracle Database?
By using the "ALTER USER" statement followed by the "DEFAULT ROLE" clause and the desired profile
name.
21. What happens when you use the NOT IDENTIFIED option in the CREATE ROLE statement?
It indicates that the role is authorized by the database and doesn't require a password to be enabled.
22. How can you grant all privileges to an existing user in Oracle Database?
By using the "GRANT ALL PRIVILEGES" statement followed by the user or role name.
Roles help manage privileges more efficiently by grouping related privileges and granting them to
users or other roles.