0% found this document useful (0 votes)
1 views31 pages

Dba Report

This document is a comprehensive lab report on Database Administration (CSC 414) from Tribhuvan University's Institute of Science and Technology. It includes various assignments covering topics such as user management, database startup and shutdown modes, creating and managing database objects, backup and recovery processes, and PL/SQL programming. The report details practical exercises and commands related to Oracle database management, ensuring a thorough understanding of database administration concepts.

Uploaded by

bibekkshetri055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views31 pages

Dba Report

This document is a comprehensive lab report on Database Administration (CSC 414) from Tribhuvan University's Institute of Science and Technology. It includes various assignments covering topics such as user management, database startup and shutdown modes, creating and managing database objects, backup and recovery processes, and PL/SQL programming. The report details practical exercises and commands related to Oracle database management, ensuring a thorough understanding of database administration concepts.

Uploaded by

bibekkshetri055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Tribhuvan University

Institute of Science and Technology


Amrit Science Campus, Thamel

A Lab Report On
Database Administration (CSC 414)

Submitted by: Submitted to:


Department of Computer Science & IT
Abishes Shrestha
Symbol No.: 25610/077
LAB ASSIGNMENT 1

1. Locate the tablespace, data files, control files, online redo log files and parameter file
(spfile) in your database using different dynamic performance views.

-Tablespace and datafiles

-Contorl File

-Online redolog file

-Parameter file(spfile)
2. Practice different startup and shutdown modes.

Mode Startup Shutdown Use-case


STARTUP Starts the instance
Used for recovery
NOMOUNT only
operations
STARTUP MOUNT Starts the instance, Used for database
mounts the database recovery
STARTUP OPEN Normal startup,
Starts the instance,
opens the database
mounts, and opens
the database
STARTUP Starts the instance and Maintenance mode
RESTRICT restricts access for DBAs only
STARTUP FORCE Use when the
Forcefully shuts
database is hung
down and restarts the
database

SHUTDOWN Graceful shutdown, Graceful shutdown,


NORMAL waits for users all sessions finish
SHUTDOWN Fast shutdown, rolls Faster shutdown, no
IMMEDIATE back transactions waiting for users
SHUTDOWN Forced shutdown, no Emergency
ABORT transaction rollback shutdown, instance
recovery needed
SHUTDOWN Waits for ongoing Graceful shutdown
TRANSACTIONAL transactions to finish after transactions
complete

3. User Management

a. User creation

i. Create three new database users named user1, user2, and user3
ii. Each new user is identified by the password 12345.
iii. Their default tablespace is USERS.
iv. Their temporary tablespace is TEMP.
v. The quota is 20K on USERS.
vi. The profile is the Oracle database default profile.
vii. Delete the user with and without having object
2. Deleting users: Delete user without removing their objects and with their objects

b.Granting Privileges
i.Mention different system and object privileges
Object Privileges: These are granted on specific objects like tables, views, or procedures. Examples
include SELECT, INSERT, UPDATE, DELETE, EXECUTE, etc.
System Privileges: These are granted to enable users to perform administrative and system-wide
tasks, such as creating tables, logging in, and altering users. Examples include CREATE SESSION,
CREATE TABLE, ALTER USER, DROP USER, etc.
ii. Alter the user named User1 to assign an unlimited quota on the USERS tablespace.

iii. Attempt to commect to the database as user User1. Why can't you connect? Grant User1 the
privilege needed to connect to the system.
>The reason why User1 cannot connect is that they likely do not have the CREATE SESSION
privilege. By default, Oracle does not grant this privilege to users when they are created.
GRANTING PRIVELEGES

iv.Creating a table by a user – insert row data and query the table to list the rows you inserted
to verify their existence.
v.Granting SELECT privilege and test the effect. Also do practice of passing object
privileges with grant option.
vi.Granting INSERT privileges and inserts some rows from another user.
vii.Grant update privilege on table for specified column only

vii.Grant system privileges to the user and pass the system privileges to other user with admin option
and observe the impact.
sql>>grant create user to user1 with admin option;
c.Role
i.Create a Role and grant privileges to roles and grant roles to the users.
ii.Grant role to other role and assign role to user and test the impact
iii.Revoke system/object privileges granted to roles and users
d.Data dictionary views to list the privileges
List the system privileges, object privileges and roles granted/received by the user

e.Profile
i. Create Profile with following parameter and assign the profile to the user
SESSIONS_PER_USER 2
FAILED_LOGIN_ATTEMPTS 3
IDLE_TIME 2 (in minutes)
CONNECT_TIME 5 (in minutes)
PASSWORD_LIFE_TIME 60
PASSWORD_GRACE_TIME 10
PASSWORD_REUSE_TIME 1
PASSWORD_LOCK_TIME 7
PASSWORD_REUSE_MAX 3;
ii. Apply the profile by setting the resource_limit parameter true
iii Alter the profile and check the impact of it
iv Delete the profile and check the user profile assigned after profile deleted
Lab Assignment 2
1) Creating oracle objects:
a) Table:
i. Create table with different datatype with primary key
ii. Add foreign key and check constraint

iii.Rename table and renaming table column

iv.Modify the datatype of a column


v.Add and drop new field in a table

vi.Insert data in a table using dynamic input


vii.Truncate and drop the table
viii.Flashback the table from recycle bin
b) Index create and drop

c) Synonym (what is synonym and write the advantage of it)


A synonym i is an alias or alternative name for a database object such as a table, view, sequence,
procedure, or even other synonyms. It allows users to access an object without needing to reference its
full object name (including the schema name) or object type (e.g., TABLE, VIEW, etc.).
Simplify Access, Security, Encapsulation, Cross-Database Access

d) Sequence (what is sequence? why it is used?)


A sequence is a database object that generates a series of unique numbers. It is commonly used to
generate unique values, typically for use as primary key values. Each time you request a value from
the sequence, it generates the next value in the series (usually in an incremental manner).
e) View (what is view? Write advantage of using views)
A View is a virtual table that provides a way to access and query data from one or more tables in a
customized format without actually storing the data. It is a stored query that can simplify complex
queries, encapsulate business logic, and provide an abstraction layer over the underlying tables.
Simplified Queries, Data Security, Data Integrity, Abstraction
LAB ASSIGNMENT 3
1. What is multiplexing and write command to Multiplex the Redo Log Files for your
database.
-Multiplexing in Oracle is the process of creating multiple copies of redo log files to
ensure data integrity in case one of the log files becomes unavailable or corrupted.
a. Show the groups, members, status and other related information about redo logs
and determine where to locate the files for each group.

b. Add an additional Redo Log File to each of the two existing Redo Log Groups for
your database

c. Add an additional Redo Log Group with two Redo Log Files in the group
d. Query both V$LOG and V$LOGFILE to verify the result

e. Switch the log file manually

f. Delete one of the redo log group


g. Delete one of the member from existing group

g. Rename the log file of existing group


1) First rename the file with the help of OS.

2) Then record the change in oracle database.


j.Observe the different status of the redo logs

2. Add a new control file (Multiplex the Control File)

A. Locate the control files in your database

B. Add a second control file.


C. Startup the database. Query the V$CONTROLFILE view to display the
names/locations of your control files
1) Copy an existing control file to a new location, using operating system.

2) Edit the CONTROL_FILES parameter in the database initialization parameter file to add
the new control file name, or to change the existing control file name.
3) Shutdown database
4) Startup database.
3. Create static parameter file (pfile) from dynamic parameter file (spfile)
LAB ASSIGNMENT 4

1. Tablespace: What is tablespace? How it is related with datafile?

A tablespace is a logical storage unit in Oracle Database that groups related logical
structures together. It is a container that stores database objects such as tables,
indexes, and other schema objects. Each tablespace is made up of one or more
datafiles, which are physical files that reside on the filesystem or disk storage.

 A tablespace is a logical container.


 A datafile is a physical file on disk that stores the actual data.
 A tablespace can consist of one or more datafiles, and each datafile is associated with
a specific tablespace.
 Datafiles are the physical manifestation of the tablespace and contain the data of
objects stored in that tablespace.

a. create tablespace

b. rename tablespace

c. add data file


d. rename datafile

1) First take the tablespace offline.


2) Rename the datafile at the operating system level.
3) Update the datafile name in Oracle db.
4) Bring the tablespace online.
e. datafile resize

f. drop datafile

g. drop tablespace.

2.Write steps to bring the database in archivelog mode


1. Enable archivelog mode (if not already enabled) like above.
2. Setup Fast Recovery Area.
3. Configure archivelog destination to FRA and multiplex in another destination.
3.Write steps to multiplex the archive redologs.
1. Enable archivelog mode (if not already enabled) like above.
2. Setup Fast Recovery Area.
3. Configure archivelog destination to FRA and multiplex in another destination.
LAB ASSIGNMENT 5
BACKUP AND RECOVERY
First the database must be in archivelog mode.
1.Write a command to take a RMAN backup of all database in CDB.
BACKUP DATABASE PLUS ARCHIVELOG;

2.Write a command to take a backup of specific pluggable database.

RMAN>BACKUP PLUGGABLE DATABASE pdb_name;

3.Write a command to take partial backup at tablespace and datafile level.


RMAN>BACKUP TABLESPACE tablespace_name;
RMAN>BACKUP DATAFILE
'C:\app\taman\product\21c\oradata\XE\my_tablespace01.dbf';

4.Write a command to restore and recover the database in following scenario:

a.All Datafile loss


RMAN>RESTORE DATABASE;
RMAN>RECOVER DATABASE;
RMAN>ALTER DATABASE OPEN RESETLOGS;

b.One of the user defined datafile loss

RESTORE DATAFILE 'location\tablespace_datafile.dbf';


RECOVER DATAFILE 'location\tablespace_datafile.dbf';
ALTER DATABASE OPEN;
c.Control file loss

RMAN> SET DBID 320066378;


RMAN> RESTORE CONTROLFILE FROM AUTOBACKUP;

d.Parameter file loss


RMAN> STARTUP FORCE NOMOUNT;
RMAN> RESTORE SPFILE FROM AUTOBACKUP RECOVERY
AREA='E:\Flash_Recovery_Area' db_name=xe;
RMAN> RESTORE SPFILE TO '/tmp/spfileTEMP.ora' FROM AUTOBACKUP;
RMAN> STARTUP FORCE PFILE=/tmp/init.ora; # startup with /tmp/spfileTEMP.ora

•If you want to restore the SPFILE from the autobackup, you must first set the DBID
for your database, and then use the RESTORE SPFILE FROM AUTOBACKUP
command.
RMAN> SET DBID 320066378;
RMAN> RESTORE SPFILE FROM AUTOBACKUP RECOVERY
AREA='E:\Flash_Recovery_Area' db_name=xe;

5.Write a command to take a logical backup and recovery using datapump utility at following
level:
a.Full backup and recovery

-expdp system/password@db10g full=Y directory=TEST_DIR dumpfile=DB10G.dmp


logfile=expdpDB10G.log
-impdp system/password@db10g full=Y directory=TEST_DIR dumpfile=DB10G.dmp
logfile=impdpDB10G.log

b.Table backup and recovery


-expdp system/123@db10g tables=EMP,DEPT directory=TEST_DIR
dumpfile=EMP_DEPT.dmp logfile=expdpEMP_DEPT.log
-impdp system/123@db10g tables=EMP,DEPT directory=TEST_DIR
dumpfile=EMP_DEPT.dmp logfile=impdpEMP_DEPT.log

c.Schema level backup and recovery

-expdp system/123@db10g schemas=SCOTT directory=TEST_DIR dumpfile=SCOTT.dmp


logfile=expdpSCOTT.log
-impdp system/123@db10g schemas=SCOTT directory=TEST_DIR dumpfile=SCOTT.dmp
logfile=impdpSCOTT.log
LAB ASSIGNMENT 6

1.Write a procedure to fetch all the table names inside schema using cursor.
SET SERVEROUTPUT ON

CREATE OR REPLACE PROCEDURE get_all_tables_in_schema


IS
CURSOR c_tables IS
SELECT tname
FROM tab;
v_table_name VARCHAR2(100);
BEGIN

OPEN c_tables;
LOOP
FETCH c_tables INTO v_table_name;
EXIT WHEN c_tables%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Table Name: ' || v_table_name);
END LOOP;
CLOSE c_tables;
END get_all_tables_in_schema;
/

BEGIN
get_all_tables_in_schema;
END;
/
2.Write a PLSQL block to demonstrate the conditional statements (if, if ese if).

accept age_input number prompt 'Enter your age: ';

declare
age number;

begin
age := &age_input;
if age > 20 then
dbms_output.put_line('you can drive a car');
elsif age >= 18 then
dbms_output.put_line('you can drive a car with a parental guid');
else
dbms_output.put_line('you cannot drive a car');
end if;
end;
/

3.Write a PLSQL block to demonstrate the iterative statements (simple loop, while loop, and
for loop).

--while loop
declare
cursor std_cursor is select * from student where gpa >= 3;
std std_cursor%rowtype;

begin
open std_cursor;
fetch std_cursor into std;

while std_cursor%found loop


dbms_output.put_line('Name:'||std.name||' GPA'||std.gpa);
fetch std_cursor into std;
end loop;
close std_cursor;

end;
/

--simple loop
declare
cursor std_cursor is select * from student where gpa < 3.8;
std std_cursor%rowtype;

begin
open std_cursor;
fetch std_cursor into std;

loop
dbms_output.put_line('Name:'||std.name||' GPA'||std.gpa);
fetch std_cursor into std;
exit when std_cursor%notfound;

end loop;
end;
/.
-- for loop
declare
cursor std_cursor is select * from student where gpa > 3;
begin
for std in std_cursor loop
dbms_output.put_line('Name:'||std.name||' GPA'||std.gpa);
end loop;
end;
/

4.Write a function to return the specific value form table.

create or replace function a_student


return number
is
c number;
begin
select count(*) into c from student where gpa >= 3.8;
return c;
end;
/

begin
dbms_output.put_line('No of A students in table='||a_student());
end;
/

5. Write a trigger to maintain a log(audit) on insert or update or delete on some table.

CREATE OR REPLACE TRIGGER display_department_changes


BEFORE UPDATE OR INSERT OR DELETE ON department
FOR EACH ROW
BEGIN
-- For UPDATE operation, show both old and new values
IF UPDATING THEN
dbms_output.put_line('Old dep_name: ' || :OLD.name);
dbms_output.put_line('New dep_name: ' || :NEW.name);
END IF;

-- For INSERT operation, show new values (no old values)


IF INSERTING THEN
dbms_output.put_line('New dep_name(inserted): ' || :NEW.name);
END IF;
-- For DELETE operation, show old values (no new values)
IF DELETING THEN
dbms_output.put_line('Old dep_name(deleted): ' || :OLD.name);
END IF;
END;
/

6. Write a program to demonstrate the package.


Define package specification.
1) Procedure to insert data on table,
2) Procedure to delete the data from table.
3) Procedure to update the data on table.
4) Function to return some value from table.
5) Procedure to list all records inside the table.
6) Trigger to maintain the log table on update of some field in a table.

--package specification creation


CREATE OR REPLACE PACKAGE mypackage AS
-- Procedure to insert data into the department table
PROCEDURE insert_data(p_dep_id NUMBER, p_name VARCHAR2);

-- Procedure to delete data from the department table


PROCEDURE delete_data(p_dep_id NUMBER);

-- Procedure to update data in the department table


PROCEDURE update_data(p_dep_id NUMBER, p_new_name VARCHAR2);

-- Function to return the department name by dep_id


FUNCTION get_department_name(p_dep_id NUMBER) RETURN VARCHAR2;
-- Procedure to list all records in the department table
PROCEDURE list_all_departments;

-- Trigger to log updates in the department table


PROCEDURE log_update(p_dep_id NUMBER, p_old_name VARCHAR2, p_new_name
VARCHAR2);
END mypackage;
/

--Implement package body.


CREATE OR REPLACE PACKAGE BODY mypackage AS

-- Procedure to insert data into the department table


PROCEDURE insert_data(p_dep_id NUMBER, p_name VARCHAR2) IS
BEGIN
INSERT INTO department (dep_id, name)
VALUES (p_dep_id, p_name);
COMMIT;
END insert_data;

-- Procedure to delete data from the department table


PROCEDURE delete_data(p_dep_id NUMBER) IS
BEGIN
DELETE FROM department WHERE dep_id = p_dep_id;
COMMIT;
END delete_data;

-- Procedure to update data in the department table


PROCEDURE update_data(p_dep_id NUMBER, p_new_name VARCHAR2) IS
BEGIN
UPDATE department
SET name = p_new_name
WHERE dep_id = p_dep_id;
END update_data;

-- Function to return the department name by dep_id


FUNCTION get_department_name(p_dep_id NUMBER) RETURN VARCHAR2 IS
v_name VARCHAR2(50);
BEGIN
SELECT name
INTO v_name
FROM department
WHERE dep_id = p_dep_id;
RETURN v_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 'Department Not Found';
END get_department_name;

-- Procedure to list all records in the department table


PROCEDURE list_all_departments IS
CURSOR dept_cursor IS
SELECT dep_id, name FROM department;
v_dep_id department.dep_id%TYPE;
v_name department.name%TYPE;
BEGIN
OPEN dept_cursor;
LOOP
FETCH dept_cursor INTO v_dep_id, v_name;
EXIT WHEN dept_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Dep ID: ' || v_dep_id || ' Name: ' || v_name);
END LOOP;
CLOSE dept_cursor;
END list_all_departments;

-- Procedure to log updates in the department table


PROCEDURE log_update(p_dep_id NUMBER, p_old_name VARCHAR2, p_new_name
VARCHAR2) IS
BEGIN
INSERT INTO department_log (dep_id, old_name, new_name, change_time)
VALUES (p_dep_id, p_old_name, p_new_name, SYSDATE);
END log_update;

END mypackage;
/

--creating trigger that records log in department_log table


CREATE OR REPLACE TRIGGER department_update_trigger
AFTER UPDATE ON department
FOR EACH ROW
BEGIN
-- Log the update by calling the package procedure
mypackage.log_update(:OLD.dep_id, :OLD.name, :NEW.name);
DBMS_OUTPUT.PUT_LINE(:OLD.dep_id ||' : '|| :OLD.name ||'-->' || :NEW.name);
END;
/
Table department_log creation

Making packages and using

You might also like