Lab 1 - Glennb.us

advertisement
MSCD640
Lab 1
Name: _________________________________________
Date: __________________________________________
----------------------------------------------------------------------------------The purpose of this lab is to become familiar with the following types of basic DBA
tasks:
 Connecting to your database as SYS
 Starting/stopping database
 Toggling archivelog mode
 Viewing information in the data dictionary regarding your database
 Managing tablespaces and datafiles
 Managing control files and online redo logs
Read Chapters 1 through 5 in the book before starting on this lab.
These labs focus on how to perform DBA tasks from the SQL*Plus command prompt.
These labs do not show how to perform tasks from Enterprise Manager. I prefer that
students learn basic DBA tasks from SQL*Plus for the following reasons:
 Not all environments have Enterprise Manager installed, therefore you’ll need to
know how to perform basic DBA tasks from the SQL command prompt.
 Performing the tasks manually from SQL gives you a better understanding of the
underlying SQL used to accomplish DBA tasks.
 If you can perform DBA tasks from SQL, then you’ll have no problems
understanding how to perform the same tasks in Enterprise Manager.
To get an “A” grade on this lab:
 Answer all questions.
 Provide output of steps that require you to perform a task. The output can be a
spool file, cut and paste from the screen to a word document, screen shots. I need
to see something to verify that you ran the labs. I’m expecting you to turn in a
word document that contains each question and also the corresponding answers to
questions asked and output from SQL statements.
----------------------------------------------------------------------------------Lab 1, Part 1: Understanding Your Database Environment
Read Chapters 1-3 in the book.
----------------------------------------------------------------------------------1. From the operating system prompt, verify that the proper OS variables have been set.
11g book: pages 25-29
12c book: pages 25-29
echo $ORACLE_SID
echo $ORACLE_HOME
echo $LD_LIBRARY_PATH
Also, how much free disk space does the server have?
df -h
----------------------------------------------------------------------------------2. What are the differences between SYSDBA and SYSOPER privileges?
11g book: pages 5-6
12c book: pages 5-6
----------------------------------------------------------------------------------3. Explain OS authentication and how that allows you to connect to the database as SYS
without providing a password.
11g book: pages 41-42
12c book: page 42
----------------------------------------------------------------------------------4. Startup your database.
11g book: pages 40-44
12c book: pages 42-44
Connect to your database with sysdba privileges.
sqlplus / as sysdba
SQL> show user;
SQL> startup;
SQL> select name from v$database;
----------------------------------------------------------------------------------5. Shutdown your Oracle database.
sqlplus / as sysdba
SQL> show user;
SQL> select name from v$database;
SQL> shutdown immediate;
----------------------------------------------------------------------------------6. Describe the difference between a database and an instance.
11g book: page 45
12c book: pages 45-46
----------------------------------------------------------------------------------7. For the rest of the labs, it is assumed that you now know how to start and stop your
database. Before running SQL scripts, you’ll need to first start your database. If your
database is not started, and you attempt to run SQL scripts, you’ll receive an error
message similar to this:
ORA-01034: ORACLE not available
Re-start your database. What version of Oracle are you using? Are you using the
Enterprise Edition or the Standard Edition?
SQL> select * from v$version;
What is the characterset for your database;
SQL> select * from database_properties where description like 'Character%';
----------------------------------------------------------------------------------8. What database features are enabled in your database? If running from SQL*Plus, you
may want to enable some formatting so that the output fits on your screen:
set lines 132
col name form a40
col version form a15
select name, version, first_usage_date
from dba_feature_usage_statistics;
----------------------------------------------------------------------------------9. What type of initialization file does your database use? Is it an spfile or an init.ora file?
11g book: page 30
12c book: pages 30-31
SQL> show parameter spfile
What are the advantages/disadvantages of using an spfile versus and init.ora file?
----------------------------------------------------------------------------------10. What is the default permanent tablespace for your database?
11g book: page 34
12c book: page 34
SELECT *
FROM database_properties
WHERE property_name = 'DEFAULT_PERMANENT_TABLESPACE';
----------------------------------------------------------------------------------11. What is the default temporary tablespace for your database?
SELECT *
FROM database_properties
WHERE property_name = 'DEFAULT_TEMP_TABLESPACE';
----------------------------------------------------------------------------------12. Are you using automatic undo management for your UNDO tablespace?
select name, value
from v$parameter
where name in ('undo_management','undo_tablespace');
----------------------------------------------------------------------------------13. Does your database use a password file?
11g book: page 39
12c book: pages 40-41
SQL> select * from v$pwfile_users;
What is the advantage of using a password file?
----------------------------------------------------------------------------------14. As a percentage, how much free space in your tablespaces?
COL pct_free FORMAT 999
SELECT (f.bytes/a.bytes)*100 pct_free,'% free',a.tablespace_name
FROM
(SELECT NVL(SUM(bytes),0) bytes, x.tablespace_name
FROM dba_free_space y, dba_tablespaces x
WHERE x.tablespace_name = y.tablespace_name(+)
AND x.contents != 'TEMPORARY' AND x.status != 'READ ONLY'
AND x.tablespace_name NOT LIKE 'UNDO%'
GROUP BY x.tablespace_name) f,
(SELECT SUM(bytes) bytes, tablespace_name
FROM dba_data_files
GROUP BY tablespace_name) a
WHERE a.tablespace_name = f.tablespace_name
ORDER BY 1;
----------------------------------------------------------------------------------15. Where are your database files located? (datafiles, online redo logs, and control files)
11g book: page 81
12c book: page 89
Run the following script while connected as sys to your database:
REM *****************************************************
REM * File : freesp.sql
REM *****************************************************
SET PAGESIZE 100 LINES 132 ECHO OFF VERIFY OFF FEEDB OFF SPACE 1 TRIMSP ON
COMPUTE SUM OF a_byt t_byt f_byt ON REPORT
BREAK ON REPORT ON tablespace_name ON pf
COL tablespace_name FOR A17 TRU HEAD 'Tablespace|Name'
COL file_name
FOR A45 WRA HEAD 'Filename'
COL a_byt
FOR 9,990.999 HEAD 'Allocated|GB'
COL t_byt
FOR 9,990.999 HEAD 'Current|Used GB'
COL f_byt
FOR 9,990.999 HEAD 'Current|Free GB'
COL pct_free
FOR 990.0 HEAD 'File %|Free'
COL pf
FOR 990.0 HEAD 'Tbsp %|Free'
COL seq NOPRINT
DEFINE b_div=1073741824
-COL db_name NEW_VALUE h_db_name NOPRINT
COL db_date NEW_VALUE h_db_date NOPRINT
SELECT name db_name, TO_CHAR(sysdate,'YYYY_MM_DD') db_date FROM v$database;
-SPO &&h_db_name._&&h_db_date..lis
PROMPT Database: &&h_db_name, Date: &&h_db_date
-SELECT 1 seq, b.tablespace_name, nvl(x.fs,0)/y.ap*100 pf, b.file_name file_name,
b.bytes/&&b_div a_byt, NVL((b.bytes-SUM(f.bytes))/&&b_div,b.bytes/&&b_div) t_byt,
NVL(SUM(f.bytes)/&&b_div,0) f_byt, NVL(SUM(f.bytes)/b.bytes*100,0) pct_free
FROM dba_free_space f, dba_data_files b
,(SELECT y.tablespace_name, SUM(y.bytes) fs
FROM dba_free_space y GROUP BY y.tablespace_name) x
,(SELECT x.tablespace_name, SUM(x.bytes) ap
FROM dba_data_files x GROUP BY x.tablespace_name) y
WHERE f.file_id(+) = b.file_id
AND x.tablespace_name(+) = y.tablespace_name
and y.tablespace_name = b.tablespace_name
AND f.tablespace_name(+) = b.tablespace_name
GROUP BY b.tablespace_name, nvl(x.fs,0)/y.ap*100, b.file_name, b.bytes
UNION
SELECT 2 seq, tablespace_name, j.bf/k.bb*100 pf, b.name file_name, b.bytes/&&b_div a_byt,
a.bytes_used/&&b_div t_byt, a.bytes_free/&&b_div f_byt,
a.bytes_free/b.bytes*100 pct_free
FROM v$temp_space_header a, v$tempfile b
,(SELECT SUM(bytes_free) bf FROM v$temp_space_header) j
,(SELECT SUM(bytes) bb FROM v$tempfile) k
WHERE a.file_id = b.file#
ORDER BY 1,2,4,3;
-COLUMN name FORMAT A60 HEAD 'Control Files'
SELECT name FROM v$controlfile;
-COL member FORMAT A50 HEAD 'Redo log files'
COL status FORMAT A9 HEAD 'Status'
COL archived FORMAT A10 HEAD 'Archived'
COL sum_bytes FORMAT 999,999,999,990 HEAD 'Meg Bytes'
-SELECT a.group#, a.member, b.status, b.archived, SUM(b.bytes)/1024/1024 sum_bytes
FROM v$logfile a, v$log b
WHERE a.group# = b.group#
GROUP BY a.group#, a.member, b.status, b.archived
ORDER BY 1, 2;
-SPO OFF;
----------------------------------------------------------------------------------16. In Oracle Database 12c, what is the optimal way to manage the memory that Oracle
uses?
11g book: page 31
12c book: pages 31-21
SQL> show parameter memory;
Is your database configured to use the latest Oracle Database memory management
parameters?
Are the memory management parameters modifiable with the “ALTER SYSTEM” SQL
statement? Run the following SQL to verify:
col value form a20
col name form a30
select name, value, issys_modifiable from v$parameter where name like '%memory%';
----------------------------------------------------------------------------------17. Toggle your database in and out of archive log mode, as the SYS user:
11g book: pages 426-425
12c book: pages 117-124
SQL> show user;
SQL> select name from v$database;
Are you connected to the correct database?
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter database open;
Where are your archive logs going to be written to? (run the following statement to
verify):
SQL> archive log list;
Look carefully at the output of the archive log list statement. What is the difference
between these two database features: “Database log mode” and “Automatic archival”.
Turn off archivelog mode:
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database noarchivelog;
SQL> archive log list;
SQL> shutdown immediate;
----------------------------------------------------------------------------------18. Display the value of the initialization parameter of log_archive_start:
SQL> show parameter log_archive_start;
Do you need to worry about this parameter? Or has it been deprecated?
SQL> select name, isdeprecated from v$parameter where name='log_archive_start';
----------------------------------------------------------------------------------19. How many log file groups does your database have? How many log file members per
group?
SQL> select group#, member from v$logfile;
Switch your logfiles several times:
SQL> alter system switch logfile;
SQL> alter system switch logfile;
SQL> alter system switch logfile;
----------------------------------------------------------------------------------20. How often have your online redo logs switched recently?
select count(*), to_char(first_time,'YYYY:MM:DD:HH24')
from v$log_history
group by to_char(first_time,'YYYY:MM:DD:HH24')
order by 2;
----------------------------------------------------------------------------------21. Where is the alert log located for your database?
SQL> show parameter background_dump_dest
What is the actual name of your alert log? What is the alert log used for?
-----------------------------------------------------------------------------------
22. From the operating system prompt, view the contents of the alert.log:
cat <path and name of your alert.log file>
What do the last 10 lines of the alert.log show?
----------------------------------------------------------------------------------23. Is your database using a Fast Recovery Area (FRA)?
SQL> show parameter db_recovery
What does it show?
What is the Fast Recovery Area?
----------------------------------------------------------------------------------24. Customize your SQL prompt.
11g book: page 54
12c book: page 58
SQL> SET SQLPROMPT '&_USER.@&_CONNECT_IDENTIFIER.> '
What does your SQL prompt now show?
----------------------------------------------------------------------------------25. What are the top resource consuming SQL statements in your database?
select * from(
select
sql_text
,buffer_gets
,disk_reads
,sorts
,cpu_time/1000000 cpu_sec
,executions
,rows_processed
from v$sqlstats
order by cpu_time DESC)
where rownum < 11;
----------------------------------------------------------------------------------26. What users exist in your database?
11g book: page 117
12c book: page 74
SELECT
username
,account_status
,lock_date
,created
FROM dba_users
ORDER BY username;
----------------------------------------------------------------------------------Lab 1, Part 2: Tablespaces and Datafiles
Read Chapter 4 in the book.
----------------------------------------------------------------------------------27. Create a new tablespace with a datafile located in the same directory as other datafiles
in your database. You’ll have to modify this script. If you’re unsure of the location of
your datafiles, see the output from the step where you report on the physical layout of
your database (or “select name from v$datafile;”).
11g book: page 73
12c book: page 80
create tablespace app_data
datafile '<directory to your datafiles for your database>/app_data01.dbf' size 10m;
Below is an example, in the below script, the directory path is:
/640/oracle/product/11.2.0/oradata/E64001
The prior line is the directory path for the datafiles in the E64001 database (my database
for this class). You’ll have to modify that path to match the name of your database
(E64nnn).
The name of the datafile is:
app_data01.dbf
create tablespace app_data
datafile '/640/oracle/product/11.2.0/oradata/E64001/app_data01.dbf' size 10m;
If you get this error:
ORA-01119: error in creating database file
...
ORA-27040: file create error, unable to create file
That most likely means that the directory path you’ve entered does not exist on the
database server. You need to ensure that you use the same directory path as other
datafiles in your database (select name from v$datafile;).
After successfully creating the tablespace, verify that the tablespace was created by
running this script:
SQL> select tablespace_name, extent_management, segment_space_management from
dba_tablespaces;
What are the advantages of using a locally managed tablespace?
Note: As of 12c, you can only use locally managed tablespaces. So this it’s a bit of a moot
point discussing the advantages/disadvantages of locally managed, as it’s the only choice
now. You previously had the choice of making a tablespace dictionary managed or
locally managed. Going forward, it’s all locally managed.
What are the advantages of using automatic segment space management?
----------------------------------------------------------------------------------28. Rename the tablespace you created in the prior step.
11g book: page 76
12c book: page 82
SQL> alter tablespace app_data rename to test_data;
Verify:
SQL> select tablespace_name, extent_management, segment_space_management from
dba_tablespaces;
----------------------------------------------------------------------------------29. Alter the tablespace to read only mode:
11g book: page 77
12c book: page 84
SQL> alter tablespace test_data read only;
Try to create a table in the read only tablespace:
SQL> create table test1 (test_id number) tablespace test_data;
What happened?
----------------------------------------------------------------------------------30. Alter the tablespace back to read write.
SQL> alter tablespace test_data read write;
Try to create a table in that tablespace:
SQL> create table test1 (test_id number) tablespace test_data;
SQL> desc test1;
----------------------------------------------------------------------------------31. Alter the size of the TEST_DATA tablespace by modifying the size of its datafile.
11g book: page 82
12c book: page 90
SQL> alter database datafile ‘<directory path and datafile name>’ resize 20m;
Verify the size has changed:
SQL> select name, bytes from v$datafile;
If you’re unsure of the directory path and datafile name:
SQL> select name from v$datafile;
Here’s an example of altering the size of a datafile for my database (E64001):
SQL> alter database datafile
'/640/oracle/product/11.2.0/oradata/E64001/app_data01.dbf' resize 20m;
You’ll have to modify the prior command to match your database name (E64nnn).
If you get this error:
ORA-01516: nonexistent log file, data file, or temporary file
Then most likely the directory path and filename you’ve entered do not exist in your
database. Verify the directory path and name via:
SQL> select name from v$datafile;
----------------------------------------------------------------------------------32. Add a datafile named app_data02.dbf to the TEST_DATA tablespace.
11g book: pages 82-83
12c book: pages 90-921
SQL> alter tablespace test_data add ‘<your directory path and name>’ size 10m;
In the prior line of code, you’ll need to provide the directory path and name for your
database.
For example for my database (E64001), I would add a datafile as shown:
SQL> alter tablespace test_data add datafile
'/640/oracle/product/11.2.0/oradata/E64001/app_data02.dbf' size 10m;
Verify the file was added:
SQL> select name, bytes from v$datafile;
If you receive an error similar to this:
ORA-01119: error in creating database file
...
ORA-27040: file create error, unable to create file
That means the directory path you’ve entered does not exist in your database.
----------------------------------------------------------------------------------33. Drop the tablespace that you added.
11g book: page 78
12c book: page 84
SQL> drop tablespace test_data including contents and datafiles;
----------------------------------------------------------------------------------34. Create a bigfile tablespace.
11g book: page 81
12c book: page 87
create bigfile tablespace inv_big_data
datafile '<your database directory path>/inv_big_data01.dbf' size 10m;
In the prior script, you’ll need to modify it so the directory path is the same as the other
datafiles in your database. For example my datafiles for my database (E64001) are in this
directory:
/640/oracle/product/11.2.0/oradata/E64001
Therefore, if I want to create a datafile in that directory (for the E64001 database), I
would do so as follows:
create bigfile tablespace inv_big_data
datafile '/640/oracle/product/11.2.0/oradata/E64001/inv_big_data01.dbf' size 10m;
You’ll have to modify the above so that it matches the directory path for your database.
Verify that a bigfile tablespace was created:
SQL> select tablespace_name, bigfile from dba_tablespaces;
What are the pros/cons to a bigfile tablespace?
----------------------------------------------------------------------------------Lab 1, Part 3: Managing Control Files and Online Redo Logs
Read Chapter 5 in the book.
----------------------------------------------------------------------------------35. Where are your controlfiles and what are they named?
11g book: page 93
12c book: page 102
SQL> select name from v$controlfile;
----------------------------------------------------------------------------------36. What is the control_files parameter set to?
SQL> show parameter control_files
----------------------------------------------------------------------------------37. How many online redo log groups are in your database?
11g book: page 100
12c book: page 109
col member form a50
-select a.group#,a.member,b.status,b.archived,bytes
from v$logfile a, v$log b
where a.group# = b.group#
order by 1,2;
How many groups are there?
How many members are in each group?
What size are the online redo log file members?
----------------------------------------------------------------------------------38. Add an online redo log group to your database. Create two members in the group.
Make sure you use the same size as your current online redo logs.
11g book: page 105
12c book: page 113
alter database add logfile group 4
('<directory path/filename>') SIZE <put in the correct size>M;
In the prior line of code, put in an appropriate directory path and filename for your
database. For example for my database E64001:
alter database add logfile group 4
('/640/oracle/product/11.2.0/oradata/E64001/redo04a.rdo') SIZE 50M;
For your database, you’ll have to modify that so it has the number of your database:
alter database add logfile group 4
('/640/oracle/product/11.2.0/oradata/E64nnn/redo04a.rdo') SIZE 50M;
----------------------------------------------------------------------------------39. Add a logfile member to the group you added in the prior step.
11g book: pages 106-107
12c book: page 115
alter database add logfile member '<directory path/filename>'
to group 4;
In the prior line of code, put in an appropriate directory path and filename for your
database. For example for my database:
alter database
add logfile member '/640/oracle/product/11.2.0/oradata/E64001/redo04b.rdo' to group 4;
For your database, you’ll have to modify the prior command for your database number:
alter database
add logfile member '/640/oracle/product/11.2.0/oradata/E64nnn/redo04b.rdo' to group 4;
Verify that the new group has two members:
col member form a50
-select a.group#,a.member,b.status,b.archived,bytes
from v$logfile a, v$log b
where a.group# = b.group#
order by 1,2;
What is the advantage to having two members in one online redo log group?
----------------------------------------------------------------------------------40. Make sure you shutdown your database. This frees up server resources so that others
can complete their work.
SQL> shutdown immediate;
-----------------------------------------------------------------------------------
Download