View
231
Download
0
Tags:
Embed Size (px)
Citation preview
Database AdministrationDatabase Administration
Part 1Part 1
Chapter SixChapter Six
CSCI260 Database Applications
22
Chapter ObjectivesChapter Objectives
• Understand the need for and importance of Understand the need for and importance of database administrationdatabase administration
• Learn different ways of processing a databaseLearn different ways of processing a database
• Understand the need for concurrency control, Understand the need for concurrency control, security, and backup and recoverysecurity, and backup and recovery
• Learn typical problems that can occur when Learn typical problems that can occur when multiple users process a database multiple users process a database concurrentlyconcurrently
• Understand the use of locking and the Understand the use of locking and the problem of deadlock Understand the use of problem of deadlock Understand the use of locking and the problem of deadlocklocking and the problem of deadlock
33
Chapter Objectives Chapter Objectives (continued)(continued)• Learn the difference between optimistic and Learn the difference between optimistic and
pessimistic lockingpessimistic locking• Know the meaning of ACID transactionKnow the meaning of ACID transaction• Learn the four 1992 ANSI standard isolation Learn the four 1992 ANSI standard isolation
levelslevels• Understand the need for security and learn a Understand the need for security and learn a
generalized model of database securitygeneralized model of database security• Know the difference between DBMS and Know the difference between DBMS and
application securityapplication security• Know the difference between recovery via Know the difference between recovery via
reprocessing and recovery via reprocessing and recovery via rollback/rollforwardrollback/rollforward
44
Chapter Objectives Chapter Objectives (continued)(continued)• Understand the nature of the tasks required Understand the nature of the tasks required
for recovery using rollback/rollforwardfor recovery using rollback/rollforward• Know basic administrative and managerial Know basic administrative and managerial
DBA functionsDBA functions
55
Database Processing Database Processing EnvironmentEnvironment
• A database processing environment A database processing environment is complicated and multi-facetedis complicated and multi-faceted– Multiple usersMultiple users– Multiple queriesMultiple queries– Multiple formsMultiple forms– Multiple reportsMultiple reports– Multiple application programsMultiple application programs
66
Processing ConstraintsProcessing Constraints
• Enforcing referential integrityEnforcing referential integrity
• Cascading deletionCascading deletion
• Cascading modificationsCascading modifications
• Data type constraintsData type constraints
• Data size constraintsData size constraints
• Data value constraintsData value constraints
• Null constraintsNull constraints
• Uniqueness constraintsUniqueness constraints
77
Internet Application Internet Application ProcessingProcessing
• Internet Application Processing is Internet Application Processing is more complicated than traditional more complicated than traditional application processingapplication processing
• Specifically, with Internet Application Specifically, with Internet Application Processing …Processing …– The network becomes an integral part of The network becomes an integral part of
the applicationthe application
88
The Database Processing The Database Processing EnvironmentEnvironment
99
Stored ProceduresStored Procedures
• A A stored procedurestored procedure is a module is a module similar to subroutine or function that similar to subroutine or function that performs database actionsperforms database actions– Stored in the database itselfStored in the database itself– Can pass it parameters, receive resultsCan pass it parameters, receive results– Written in PL/SQL (oracle), TRANSACT-Written in PL/SQL (oracle), TRANSACT-
SQL (SQL Server)SQL (SQL Server)
1010
Stored ProceduresStored Procedures
1111
Stored ProceduresStored Procedures
1212
TriggersTriggers
• A A triggertrigger is a stored procedure that is is a stored procedure that is automatically invoked by the DBMS automatically invoked by the DBMS when a specified activity occurswhen a specified activity occurs– BEFORE, AFTER and INSTEAD OFBEFORE, AFTER and INSTEAD OF
1313
Control, Security and ReliabilityControl, Security and Reliability
• Possible to have many database Possible to have many database functions occurring at the same timefunctions occurring at the same time
• Three necessary database Three necessary database administration functionsadministration functions– Concurrency controlConcurrency control– SecuritySecurity– Backup and RecoveryBackup and Recovery
1414
Concurrency ControlConcurrency Control
• Concurrency controlConcurrency control ensures that one user’s ensures that one user’s actions do not adversely impact another actions do not adversely impact another user’s actionsuser’s actions
• At the core of concurrency is accessibility. At the core of concurrency is accessibility. In one extreme, data becomes inaccessible In one extreme, data becomes inaccessible once a user touches the data. This ensures once a user touches the data. This ensures that data that is being considered for that data that is being considered for update is not shown. In the other extreme, update is not shown. In the other extreme, data is always readable. The data is even data is always readable. The data is even readable when it is locked for update.readable when it is locked for update.
1515
Concurrency Control Concurrency Control (continued)(continued)• InterdependencyInterdependency
– Changes required by one user may impact Changes required by one user may impact othersothers
• ConcurrencyConcurrency– People or applications may try to update the People or applications may try to update the
same information at the same timesame information at the same time
• Record retentionRecord retention– When information should be discardedWhen information should be discarded
1616
Need for Atomic Need for Atomic TransactionsTransactions• A database operation typically involves A database operation typically involves
several transactions. These transactions several transactions. These transactions are atomic and are sometimes called are atomic and are sometimes called logical units of work (LUW).logical units of work (LUW).
• Before an operation is Before an operation is committedcommitted to the to the database, all LUWs must successfully database, all LUWs must successfully complete. If one or more LUW is complete. If one or more LUW is unsuccessful, a unsuccessful, a rollbackrollback is performed and is performed and no changes are saved to the database.no changes are saved to the database.
1717
Need for Atomic Need for Atomic TransactionsTransactions• Example: Sequence required when Example: Sequence required when
recording new order.recording new order.– 1. Change the customer record, increasing 1. Change the customer record, increasing
value of amount owed.value of amount owed.– 2. Change the salesperson record, 2. Change the salesperson record,
increasing the value of Commission Due.increasing the value of Commission Due.– 3. Insert new order record into the 3. Insert new order record into the
database.database.
If last one fails, roll back all threeIf last one fails, roll back all three
1818
1919
2020
Lost Update ProblemLost Update Problem
• If two or more users are If two or more users are attempting to update the same attempting to update the same piece of data at the same time, it piece of data at the same time, it is possible that one update may is possible that one update may overwrite another update.overwrite another update.
2121
Concurrent Processing Concurrent Processing ExampleExample
2222
Concurrent Processing Concurrent Processing ProblemProblem
2323
Concurrency IssuesConcurrency Issues
• Concurrency issues common – have Concurrency issues common – have standardized names…standardized names…
• Dirty readsDirty reads– The transaction reads a changed record that The transaction reads a changed record that
has not been committed to the databasehas not been committed to the database– Example: One transaction reads row Example: One transaction reads row
changed by a second transaction. Second changed by a second transaction. Second transaction later cancels (rollsback) its transaction later cancels (rollsback) its changeschanges
2424
Concurrency IssuesConcurrency Issues
• Inconsistent reads/Non-repeatable readInconsistent reads/Non-repeatable read– The transaction re-reads a data set and finds The transaction re-reads a data set and finds
that the data has changedthat the data has changed
• Phantom readsPhantom reads– The transaction re-reads a data set and finds The transaction re-reads a data set and finds
that a new record has been addedthat a new record has been added
Fix some of these issues with Fix some of these issues with Resource Resource LockingLocking
2525
Resource Locking Resource Locking
• To avoid concurrency issues, resource To avoid concurrency issues, resource locking will disallow transactions from locking will disallow transactions from reading, modifying, and/or writing to a reading, modifying, and/or writing to a data set that has been “data set that has been “lockedlocked””
• Prevent sharing of dataPrevent sharing of data
• Transactions can use a lock commandTransactions can use a lock command
2626
2727
Implicit versus Explicit Implicit versus Explicit Resource LockingResource Locking
• ImplicitImplicit locks are issued automatically locks are issued automatically by the DBMS based on an activityby the DBMS based on an activity
• ExplicitExplicit locks are issued by users locks are issued by users requesting exclusive rights to the datarequesting exclusive rights to the data
2828
Lock GranularityLock Granularity
• Size of lock has different impactsSize of lock has different impacts
• Large Granularity lock Large Granularity lock – e.g. lock entire tablee.g. lock entire table– easy for DBMS to administereasy for DBMS to administer– Frequently cause conflictsFrequently cause conflicts
• Smaller Granularity lockSmaller Granularity lock– E.g. lock rowE.g. lock row– Harder for DBMS to administer (lots of details)Harder for DBMS to administer (lots of details)– Fewer conflictsFewer conflicts
2929
Lock TypesLock Types
• Exclusive LockExclusive Lock– Locks an item from access of any typeLocks an item from access of any type– No other transaction can read or No other transaction can read or
change the datachange the data
• Shared lockShared lock– Locks an item from being changedLocks an item from being changed– Item can still be read, just can’t alter itItem can still be read, just can’t alter it
3030
Serializable Transactions Serializable Transactions (Two-Phased Locking)(Two-Phased Locking)
• Two-phased locking, whereby Two-phased locking, whereby locks are obtained as they are locks are obtained as they are neededneeded– A A growing growing phase, whereby the phase, whereby the
transaction continues to request transaction continues to request additional locksadditional locks
– A A shrinking shrinking phase, whereby the phase, whereby the transaction begins to release the transaction begins to release the lockslocks
3131
Serializable Transactions Serializable Transactions ExampleExample
• Order-entry transaction that Order-entry transaction that involves processing data in involves processing data in CUSTOMER, SALESPERSON and CUSTOMER, SALESPERSON and ORDER tables.ORDER tables.– Transaction issues locks on all three Transaction issues locks on all three
tablestables– Makes all the database changesMakes all the database changes– Releases all its locksReleases all its locks
3232
DeadlockDeadlock
• As a transaction begins to lock As a transaction begins to lock resources, it may have to wait for a resources, it may have to wait for a particular resource to be released by particular resource to be released by another transaction another transaction
• On occasions, two transactions may On occasions, two transactions may indefinitely wait on each another to indefinitely wait on each another to release resources. This condition is release resources. This condition is known as a known as a deadlockdeadlock or a deadly or a deadly embrace embrace
3333
Deadlock ExampleDeadlock Example
• User A wants to order some paperUser A wants to order some paper– If she can get the paper, she wants to If she can get the paper, she wants to
order pencilsorder pencils
• User B wants to order some pencils. User B wants to order some pencils. – If he can get the pencils, he will order If he can get the pencils, he will order
paperpaper
3434
3535
Optimistic versus Optimistic versus Pessimistic LockingPessimistic Locking• OptimisticOptimistic
Locking Locking – Read dataRead data– Process transactionProcess transaction– Issue updateIssue update– Look for conflictLook for conflict– If conflict occurred, If conflict occurred,
rollback and repeatrollback and repeat– Else commitElse commit
• PessimisticPessimistic
LockingLocking– Lock required Lock required
resourcesresources– Read dataRead data– Process transactionProcess transaction– Issue commitIssue commit– Release locksRelease locks
3636
3737
3838
Consistent TransactionsConsistent Transactions
• Consistent transactions are often Consistent transactions are often referred to by the acronym referred to by the acronym ACIDACID – AtomicAtomic– ConsistentConsistent– IsolatedIsolated– DurableDurable
3939
ACID: AtomicACID: Atomic
• A transaction consists of a series of A transaction consists of a series of steps. Each step must be successful steps. Each step must be successful for the transaction to be savedfor the transaction to be saved
• This ensures that the transaction This ensures that the transaction completes everything it intended to completes everything it intended to do before saving the changesdo before saving the changes
4040
ACID: ConsistentACID: Consistent
• No other transactions are permitted No other transactions are permitted on the records until the current on the records until the current transaction finishestransaction finishes
• This ensures that the transaction This ensures that the transaction integrity has integrity has statement level statement level consistency consistency among all recordsamong all records
4141
ACID: ConsistentACID: Consistent
• Example:Example:• UPDATE CUSTOMERUPDATE CUSTOMER
SET SET AreaCode = ‘425’AreaCode = ‘425’WHEREWHERE ZipCode = ‘14048’;ZipCode = ‘14048’;
• CUSTOMER table has 500,000 rows. 500 match CUSTOMER table has 500,000 rows. 500 match above zipcodeabove zipcode
• May take awhile to find them all – can other May take awhile to find them all – can other transactions update during this?transactions update during this?
• Statement level consistency – update will apply to Statement level consistency – update will apply to the set of rows as they existed at the time the the set of rows as they existed at the time the SQL Statement startedSQL Statement started
4242
ACID: IsolationACID: Isolation
• Within multiuser environments, Within multiuser environments, different transactions may be different transactions may be operating on the same data operating on the same data
• As such, the sequencing of As such, the sequencing of uncommitted updates, rollbacks, and uncommitted updates, rollbacks, and commits continuously change the commits continuously change the data contentdata content
4343
ACID: DurableACID: Durable
• A durable transaction is one in A durable transaction is one in which all committed changes are which all committed changes are permanentpermanent
• Even in the case of failureEven in the case of failure
4444
1992 ANSI SQL Isolation levels1992 ANSI SQL Isolation levels