Docsity

Oracle 12c Use Cases, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download Oracle 12c Use Cases and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

CREATE TABLESPACES AND ADD DATAFILES

Create Tablespace: SQL> create tablespace admintbs datafile '$ORACLE_BASE/oradata/ORCL/datafile/admintbs_1.dbf' size 50m; --Tablespace name is admintbs and datafile is admintbs1.dbs while size is 50 mb. SQL> DROP TABLESPACE INCLUDING CONTENTS AND DATAFILES; -- Drop Tablespace and datafiles SQL> alter tablespace admintbs add datafile '$ORACLE_BASE/oradata/ORCL/datafile/admintbs_2.dbf' size 50m; -- ADD datafile to Tablespace SQL> ALTER USER mohsin default tablespace admintbs; -- Change Default Tablespace of a USER SQL> ALTER DATABASE DATAFILE '$ORACLE_BASE/oradata/ORCL/datafile/admintbs_1.dbf' RESIZE 100M; -- RESIZE Datafile SQL> select username,default_tablespace from dba_users where username = ; -- Check default tablespace of a user; SQL> select tablespace_name from all_tables where owner = 'MOHSIN' and table_name = 'HR'; -- Check table location in tablespaces


MULTIPLEXING REDO LOGS

The below query is helpful to know the status of the groups: Sql> select * from v$log; To switch a log file: Sql> alter system switch logfile; By default Redolog files contains of three groups, 2 members each. Each memeber is replica to the other member, and placed in different location from the other so that in case of disk failure we might have the redo log files. Adding the redo log groups: Sql>alter database add logfile group 4 '/u01/app/oracle/oradata/ORCL/redo04.log' size 10m; Dropping the existing redo log groups: Sql>alter database drop logfile group 4; Clearing a redo log file: Sql>alter database clear logfile group 3; To Rename or Move log file: 1: Shu Immediate; 2: Startup mount; 3: Then move or rename file physical file and then enter this sql query: Sql> alter database rename file '/u01/app/oracle/oradata/ORCL/redo04.log' to '/u01/app/oracle/oradata/redo04.log'; Database altered. ` alter database mount;

MULTIPLEX CONTROL FILES

1: Shut Database down 2: Copy controlfile from existant location. 3: Create new dir and paste controlfile in it. 4: Startup database and Alter System: SQL> ALTER SYSTEM SET control_files = '/u01/app/oracle/oradata/ORCL/controlfile/o1_mf_k8g9n5x1_.ctl', '/u01/app/oracle/fast_recovery_area/ORCL/controlfile/o1_mf_k8g9n5y8_.ctl', '/u01/app/oracle/third_control_file/o1_mf_k8g9n5y8_.ctl' SCOPE=SPFILE; SQL> select * from v$controlfile;

ARCHIVE LOGS

To archive a filled redo log group manually, connect with administrator privileges. Ensure that the database is mounted but not open. Use the ALTER SYSTEM statement with the ARCHIVE LOG clause to manually archive filled redo log files. SQL> ALTER SYSTEM ARCHIVE LOG ALL;


Adjusting the Number of Archiver Processes The LOG_ARCHIVE_MAX_PROCESSES initialization parameter specifies the number of ARCn processes that the database initially invokes. The default is two processes.The LOG_ARCHIVE_MAX_PROCESSES parameter is dynamic, and can be changed using the ALTER SYSTEM statement. The database must be mounted but not open. SQL> ALTER SYSTEM SET LOG_ARCHIVE_MAX_PROCESSES=3;


The following example shows a setting of LOG_ARCHIVE_FORMAT: ALTER SYSTEM SET LOG_ARCHIVE_FORMAT='%t_%s_%r.arc' SCOPE=SPFILE; %t=thread %s=sequence number %r= reset log id


To Change archivelog Location: Create the new archive location:

oracle@oracle]$ mkdir - p /u01/app/oracle/archivelogs/ SQL> connect / as sysdba Change the destination of archive logs: SQL> alter system set log_archive_dest_n='LOCATION=/u01/app/oracle/archivelogs' scope=both; IF you want to multiplex archive logs then use: SQL> alter system set log_archive_dest_1='LOCATION=/u01/app/oracle/archivelogs' log_archive_dest_2='LOCATION=/u02/app/oracle/archivelogs' scope=both;


If the database is in NOARCHIVELOG it must be switched to ARCHIVELOG mode. SQL> shutdown immediate SQL> startup mount SQL> alter database archivelog; SQL> alter database open; Flashback must be enabled before any flashback operations are performed. SQL> alter database flashback on; With flashback enabled the database can be switched back to a previous point-in-time or SCN without the need for a manual incomplete recovery. In the following example a table is created, the database is then flashbacked to a time before the table was created. SQL> CREATE TABLE test(id number(9)); Flashback the database to previous SCN, to before the test table was created. To Flashback the Database, database must be in mount state. SQL> shutdown immediate SQL> startup mount SQL> flashback database to SCN(scn number); SQL> alter database open resetlogs; SQL> desc test; Table or view does't exist. To check previous SCN:

SQL > select * from v$ flashback_database_log; To flashback in timestamp: SQL> flashback table test to timestamp TO_TIMESTAMP('2022- 06 - 30 13:02:20', 'YYYY-MM-DD HH24:MI:SS'); Some other variations of the flashback database command include the following: flashback database to timestamp {my_date}; flashback database to before timestamp {my_date}; flashback database to scn {my_scn}; flashback database to before scn {my_scn};


RESTORE POINTS A restore point is a name assigned to a system change number (SCN) in Oracle. An SCN is a number that uniquely identifies each new change to an Oracle database; this number is incremented whenever users commit a new transaction to the database. SQL> create restore point before_changes; Existing restore points can be displayed using the V$RESTORE_POINT view: SQL> select name from v$restore_point; RESULT: NAME


BEFORE_CHANGES

INCONSISTENT BACKUP AND RESTORE

INCONSISTENT OR HOT BACKUP

  1. Take RMAN full backup : SQL> startup mount; SQL> alter database archivelog; SQL> alter database open; [oracle@PROD-SERVER ~]$ rman target / run { backup as compressed backupset full database tag ORCL_FULL filesperset 10 format '/u01/rman/L0_DB_%T_%d_%s_%p.bkp'; backup format '/u01/rman/L0_ARC_%T_%d_%s_%p.bkp' archivelog all; backup format '/u01/rman/CF_Level_0_%T_%d.bkp' tag CF current controlfile; } exit;
  2. Take pfile Backup : SQL> create pfile from spfile; File created.

[oracle@NEW-SERVER ~]$ rman target / connected to target database (not started) RMAN> startup nomount; RMAN> restore controlfile from '/u01/rman/Controlback_20210329_0kvqv13g_1_1'; RMAN> alter database mount; RMAN> report schema; RMAN> catalog start with '/u01/rman/'; Restore the Database : RMAN>

run { allocate channel ch1 device type disk; restore database; switch datafile all; switch tempfile all; release channel ch1; } Recover the Database : RMAN> list backup of archivelog all; RMAN> run { allocate channel ch1 device type disk; recover database until sequence 41; release channel ch1; } OR run { restore database;