Download CH 12 - Angelfire

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Relational model wikipedia , lookup

Open Database Connectivity wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Database Processing
Eighth Edition
Managing
Databases
with Oracle
1
Chapter 12
David M. Kroenke
© 2002 by Prentice Hall
What is Oracle?
• Oracle is the world’s most popular
DBMS that…
– Is extremely powerful and robust
– Runs on many different operating
systems
– Can be configured and tailored
– Operates with most, if not all, addon products
2
© 2002 by Prentice Hall
Oracle Complexity
• The power and flexibility of Oracle
makes it very complex:
– Installations are difficult
– The configuration options are
numerous
– System requirements are high
– System maintenance is complex
3
© 2002 by Prentice Hall
The Language of Oracle…
SQL Plus
• SQL Plus is used in Oracle to:
– Define the structure of a database
and the definition of the data
– Insert, delete, and modify data
– Define the behavior of the system
through stored procedures and
triggers
– Retrieve data and generate reports
4
© 2002 by Prentice Hall
Gaining Access to SQL Plus
• To gain access to SQL Plus, you will need a
username and password and possibly a host
string (depending on your system configuration)
• When Oracle is first installed, it
establishes several default accounts,
namely...
– internal/oracle (a privileged account)
– sys/change_on_install (a privileged
account)
– system/manager (a privileged account)
– scott/tiger (a non-privileged account)
5
© 2002 by Prentice Hall
Creating the Database
• Ways to create an Oracle database:
– Using SQL Plus
• Start button –> Programs –> Oracle –
OraHome81 –> Applications
Development –> SQL Plus
– Using Oracle’s Database Configuration
Assistant
• Start button –> Programs –> Oracle –
OraHome81 –> Database
Administration –> Database
Configuration Assistant
6
© 2002 by Prentice Hall
Entering SQL Plus
Commands
• The SQL Plus Buffer
– As a user types commands, the
commands are saved into the SQL
Plus buffer.
• The SQL Plus Editor
– Users may edit or alter SQL Plus
commands using a text editor.
7
© 2002 by Prentice Hall
SQL Plus Buffer Commands
• SQL Plus is not case sensitive (except
within quotation marks).
• List – displays the content of the SQL Plus
buffer
• List n – display line number n and changes
the current line number to n
• Change – performs a search/replace
operation for the current line number
• Semi-colon (;) or slash (/) executes
8
© 2002 by Prentice Hall
SQL Plus Editor
• The SQL Plus Edit command will
launch the SQL Plus text editor
• After the SQL statement is complete
and correct, exit the editor
• To execute the statement, type the
slash key (/) at the SQL prompt
• To retrieve an existing SQL file:
– SQL> Edit file1.sql
9
© 2002 by Prentice Hall
SQL Plus Commands
•
•
•
•
•
•
•
•
Desc – lists the fields in the specified table
Select – retrieve data
Create – create objects
Drop – delete objects
Alter – change objects
Insert – input data
Delete – delete data
Update – change data
10
© 2002 by Prentice Hall
Select Syntax
Select field1, field2
From table_a, table_b;
11
© 2002 by Prentice Hall
Create Syntax
Create Table tablename (
field1
data_type(size) NOT NULL,
field2 data_type (size) NULL);
Create Sequence tableID Increment
by 1 start with 1000;
(this command creates a counter that
automatically increments for each new record –
does not ensure uniqueness)
12
© 2002 by Prentice Hall
Alter Table Syntax
Alter Table tablename1
Add Constraint FieldPK Primary Key (Field1,
Field2);
Alter Table tablename2
Add Constraint FieldFK Foreign Key (Field1,
Field2) references tablename1 On Delete Cascade;
13
© 2002 by Prentice Hall
Insert Syntax
Insert into tablename
(fieldID, field2)
Values
(fieldID.NextVal, ‘data content’);
14
© 2002 by Prentice Hall
Drop Syntax
• Drop Table tablename;
• Drop Sequence fieldID;
15
© 2002 by Prentice Hall
Indexes
• Indexes are used to enforce
uniqueness and to enable fast
retrieval of data.
• Create Unique Index fieldIndex on
Table(field1, field2);
16
© 2002 by Prentice Hall
Changing the Table
Structures
• Alter Table tablename add field4 datatype
(size);
• Alter Table tablename Drop Column field2;
–you will permanently lose the data in field2
• Alter Table tablename Modify field3 not
null;
17
© 2002 by Prentice Hall
Changing the Data…
Update Syntax
Update tablename
Set field1 = ‘value_a’
Where field3 = value;
18
© 2002 by Prentice Hall
Check Constraint
Provide a list of valid values or a valid
range…
Create Table tablename (
Field1 datatype (size) Not Null,
Field2 datatype (size) Null
Check (field2 in (‘value_a’,
‘value_b’)));
19
© 2002 by Prentice Hall
Check Constraint
• Alter Table tablename Add Constraint
DateChk Check (DateField1 <= DateField2);
• Alter Table tablename Add Constraint
NumRange Check (Field1 Between 180 and
400);
• Alter Table tablename Drop Constraint
constraintname;
20
© 2002 by Prentice Hall
Views
• Displaying the data from the
database just the way a user wants
it…
Create View View1 As
Select * From Tablename
With Read Only;
21
© 2002 by Prentice Hall
PL/SQL
• Allowing SQL to act more like a
programming language.
• Row-at-a-time versus set-at-a-time.
• PL/SQL permits Cursors
• A stored procedure is a PL/SQL (or
other program) stored in the
database. A stored procedure may
have parameters.
22
© 2002 by Prentice Hall
PL/SQL Parameter Types
• IN – specifies the input parameters
• OUT – specifies the output
parameters
• IN OUT – a parameter that may be
an input or an output
23
© 2002 by Prentice Hall
PL/SQL Code
• Variables are declared following the
AS keyword
• The assignment operator is := as
follows
variable1 := ‘value’
• Comments in PL/SQL are enclosed
between /* and */ as follows…
/* This is a comment */
24
© 2002 by Prentice Hall
PL/SQL Control Structures
FOR variable IN list_of_values
LOOP
Instructions
END LOOP;
IF condition THEN
BEGIN
Instructions
END;
25
© 2002 by Prentice Hall
Saving, Compiling, and
Executing PL/SQL Code
• The last line in the PL/SQL procedure
should be a slash (/).
• The procedure must be saved to a file
• To compile the procedure, type the
keyword Start, followed by the procedure
filename
– START MyProg.SQL
• To see any reported errors, type SHOW
ERRORS;
• To execute the procedure type EXEC
MyProg (‘parameter1’, ‘parameter2’);
26
© 2002 by Prentice Hall
Triggers
• A trigger is a stored procedure that is
automatic invoked by Oracle when a
specified activity occurs
• A trigger is defined relative to the activity
which invoked the trigger
– BEFORE – execute the stored procedure
prior to the activity
– AFTER – execute the stored procedure
after the activity
– INSTEAD OF – execute the stored
procedure in lue of the activity
27
© 2002 by Prentice Hall
Trigger Example
Create or Replace Trigger triggername
Before Insert or Update of
fieldname on tablename
For Each Row
Begin
/* instructions */
End;
28
© 2002 by Prentice Hall
A Trigger Knows the Old and
New Values for Fields
• The variable :new.fieldname1 stores
the new information for fieldname1 as
entered by the user.
• The variable :old.fieldname1 stores
the information in fieldname1 prior to
the user’s request.
29
© 2002 by Prentice Hall
Activating a Trigger
• The trigger must be saved to a file
• To compile the trigger, type the
keyword Start, followed by the
trigger filename
– START MyTrigger.SQL
• To see any reported errors, type
SHOW ERRORS;
• If no errors were encountered, the
trigger is automatically activated
30
© 2002 by Prentice Hall
Data Dictionary
• The data dictionary contains information
that Oracle knows about itself… the
metadata.
• It includes information regarding just
about everything in the database including
the structure and definition of tables,
sequences, triggers, indexes, views, stored
procedures, etc.
• The data dictionary table names are stored
in the DICT table.
31
© 2002 by Prentice Hall
Concurrency Control
• Since Oracle only reads committed
changes, dirty reads and lost updates
are avoided
• Transaction isolation levels:
– Read Committed
– Serializable
– Read-only
– Explicit Locks
32
© 2002 by Prentice Hall
Read Committed Transaction
Isolation
• Reads may not be repeatable (2 reads may
result in 2 data values, based on timing of
updates and reads)
• Phantoms are possible (data from a read
may be deleted after the read occurred)
• Uses exclusive locks
• Deadlocks are possible and are resolved by
rolling-back one of the transactions
33
© 2002 by Prentice Hall
Serializable Transaction
Isolation
• Reads are always repeatable
• Phantoms are avoided
• Must issue the following command:
Set Transaction Isolation Level Serializable; or
Alter Sessions Set Isolation Level Serializable;
• Coordinates activities in submission order.
When this coordination detects
difficulties, the application program(s)
must intervene.
34
© 2002 by Prentice Hall
Read-only Transaction
Isolation
• An Oracle-only isolation level
• No inserting, updating, or deleting is
permitted
35
© 2002 by Prentice Hall
Explicit Locking
• Not recommended
• Oracle does not promote locks. As a
result, a table may have many, many
locks within it. Oracle manages these
locks transparently. Issuing explicit
locks may interfere with these
transparent locks.
36
© 2002 by Prentice Hall
Oracle Security
• Username and Password is used to manage
DBMS access
• Users may be assigned to one or more
profiles
• Oracle provides extensive resource
limitations and access rights. These
restrictions may be applied to users or
profiles.
• The SQL Grant operator provides
additional access rights
• The SQL Revoke operator remove access
rights
37
© 2002 by Prentice Hall
Backup/Recovery
• Committed changes are saved to
destination Tablespaces.
• Uncommitted changes are saved in the
Rollback Tablespace.
• Redo Log files save all changes made in the
Tablespaces.
• To start and/or recover from a system
failure, the Control Files are read.
38
© 2002 by Prentice Hall
Archivelog
• If Oracle is running in ARCHIVELOG
mode, backup copies are made of the
redo log files.
• Otherwise, the redo log files are
periodically overwritten with new
information.
39
© 2002 by Prentice Hall
Types of Failures
• Application Failure
– When a program bug is encountered or
when a program does not correctly
respond to current system conditions.
• Instance Failure
– When Oracle is unable to do what it
needs to do.
• Media Failure
– When a disk becomes inaccessible to
Oracle.
40
© 2002 by Prentice Hall
Recovery of an Application
Failure
• Oracle rolls back uncommitted
changes.
41
© 2002 by Prentice Hall
Recovery of an Instance
Failure
• Oracle would be restarted using the
following sequence…
– Read the Control File
– Restore system to last known valid
state
– Roll forward changes not in system
(replay the Redo Log Files)
42
© 2002 by Prentice Hall
Recovery from a Media
Failure
• Restore system from Backup
• Read the Control File
• Roll forward changes using the
Archive Log Files (from the
ARCHIVELOG)
• Roll forward changes from the on-line
Log Files (the most recent versions of
the Logs)
43
© 2002 by Prentice Hall
Types of Recoveries
• Consistent Backup
– After the restoration, delete all
uncommitted activities
– This ensures consistency, may lose
recent changes
• Inconsistent Backup
– After the restoration, all
uncommitted activities remain
44
© 2002 by Prentice Hall
Database Processing
Eighth Edition
Managing
Databases
with Oracle
45
Chapter 12
David M. Kroenke
© 2002 by Prentice Hall