Download presentation
Presentation is loading. Please wait.
Published byMilo Rogers Modified over 9 years ago
1
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 12 Managing Databases with Oracle
2
© 2002 by Prentice Hall 2 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, add- on products
3
© 2002 by Prentice Hall 3 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
4
© 2002 by Prentice Hall 4 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
5
© 2002 by Prentice Hall 5 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)
6
© 2002 by Prentice Hall 6 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
7
© 2002 by Prentice Hall 7 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.
8
© 2002 by Prentice Hall 8 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
9
© 2002 by Prentice Hall 9 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
10
© 2002 by Prentice Hall 10 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
11
© 2002 by Prentice Hall 11 Select Syntax Select field1, field2 From table_a, table_b;
12
© 2002 by Prentice Hall 12 Create Syntax Create Table tablename ( field1data_type(size) NOT NULL, field2data_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)
13
© 2002 by Prentice Hall 13 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;
14
© 2002 by Prentice Hall 14 Insert Syntax Insert into tablename (fieldID, field2) Values (fieldID.NextVal, ‘data content’);
15
© 2002 by Prentice Hall 15 Drop Syntax Drop Table tablename; Drop Sequence fieldID;
16
© 2002 by Prentice Hall 16 Indexes Indexes are used to enforce uniqueness and to enable fast retrieval of data. Create Unique Index fieldIndex on Table(field1, field2);
17
© 2002 by Prentice Hall 17 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;
18
© 2002 by Prentice Hall 18 Changing the Data… Update Syntax Update tablename Set field1 = ‘value_a’ Where field3 = value;
19
© 2002 by Prentice Hall 19 Check Constraint Provide a list of valid values or a valid range… Create Table tablename ( Field1datatype (size) Not Null, Field2datatype (size) Null Check (field2 in (‘value_a’, ‘value_b’)));
20
© 2002 by Prentice Hall 20 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;
21
© 2002 by Prentice Hall 21 Views Displaying the data from the database just the way a user wants it… Create View View1 As Select * From Tablename With Read Only;
22
© 2002 by Prentice Hall 22 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.
23
© 2002 by Prentice Hall 23 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
24
© 2002 by Prentice Hall 24 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 */
25
© 2002 by Prentice Hall 25 PL/SQL Control Structures FOR variable IN list_of_values LOOP Instructions END LOOP; IF condition THEN BEGIN Instructions END;
26
© 2002 by Prentice Hall 26 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’);
27
© 2002 by Prentice Hall 27 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
28
© 2002 by Prentice Hall 28 Trigger Example Create or Replace Trigger triggername Before Insert or Update of fieldname on tablename For Each Row Begin /* instructions */ End;
29
© 2002 by Prentice Hall 29 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.
30
© 2002 by Prentice Hall 30 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
31
© 2002 by Prentice Hall 31 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.
32
© 2002 by Prentice Hall 32 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
33
© 2002 by Prentice Hall 33 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
34
© 2002 by Prentice Hall 34 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.
35
© 2002 by Prentice Hall 35 Read-only Transaction Isolation An Oracle-only isolation level No inserting, updating, or deleting is permitted
36
© 2002 by Prentice Hall 36 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.
37
© 2002 by Prentice Hall 37 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
38
© 2002 by Prentice Hall 38 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.
39
© 2002 by Prentice Hall 39 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.
40
© 2002 by Prentice Hall 40 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.
41
© 2002 by Prentice Hall 41 Recovery of an Application Failure Oracle rolls back uncommitted changes.
42
© 2002 by Prentice Hall 42 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)
43
© 2002 by Prentice Hall 43 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)
44
© 2002 by Prentice Hall 44 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
45
© 2002 by Prentice Hall 45 David M. Kroenke Database Processing Eighth Edition Chapter 12 Managing Databases with Oracle
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.