- •Contents
- •Preface
- •Why Do We Need Databases?
- •What’s Up in the Kingdom?
- •A Database—That’s Our Solution!
- •Summary
- •Database Terms
- •Relational Databases
- •Data Extraction Operations
- •Types of Data Models
- •Questions
- •The Relational Database Prevails!
- •Summary
- •Answers
- •The E-R Model
- •Normalizing a Table
- •What Is the E-R Model?
- •How to Analyze the E-R Model
- •Normalizing a Table
- •Steps for Designing a Database
- •Answers
- •Using SQL
- •Searching for Data Using a SELECT Statement
- •Using Aggregate Functions
- •Joining Tables
- •Creating a Table
- •SQL Overview
- •Searching for Data Using a SELECT Statement
- •Creating Conditions
- •Aggregate Functions
- •Searching for Data
- •Joining Tables
- •Creating a Table
- •Summary
- •Answers
- •What Is a Transaction?
- •What Is a Lock?
- •Database Security
- •Speeding Things Up with Indexing
- •Disaster Recovery
- •Properties of Transactions
- •When Disaster Strikes
- •Indexes
- •Optimizing a Query
- •Summary
- •Answers
- •Databases in Use
- •Databases and the Web
- •Distributed Databases
- •Stored Procedures and Triggers
- •Databases on the Web
- •What Is a Distributed Database?
- •Partitioning Data
- •Preventing Inconsistencies with a Two-Phase Commit
- •Database Replication
- •Further Application of Databases
- •Summary
- •Answers
- •Closing Remarks
- •References
- •Index
- •About the Author
- •Updates
- •More Manga Guides
What Is a Lock? |
|
|
In a database, |
when they |
|
operations by |
||
access the |
||
many users are |
||
database |
||
controlled so |
||
concurrently. |
||
that nothing goes |
||
|
||
wrong... |
|
I see.
You mean “lock" as in "lock and key"?
For that purpose, a method called a lock is used.
Exactly.
You lock data to prevent it from being erroneously processed.
Data
Data
Let me explain using the previous example.
You've drawn so many diagrams!
That's news to me.
Flip
Flip
Let's Operate a Database! 131
Andy locks the data before |
When Becky tries to perform her |
performing a series of |
operations, she must wait until |
operations. |
Andy is finished. |
30 |
30 |
|
|
apples |
|||
apples |
|
|
|
|
|
|
|
|
|
I’m |
|
|
|
waiting in |
|
|
|
a queue! |
|
|
Waiting for Andy to |
|
|
|
|
||
|
finish processing. |
||
add 10. |
|
|
|
40 apples |
40 |
|
|
apples |
|||
|
|||
|
add 10. |
Becky can only perform |
50 apples |
her operations after |
|
Andy is through with his |
|
operations. |
|
|
50 apples |
As a result, the database yields the value 50, as it
should.
Oh, boy! Cain, I'm impressed.
Yeah, yeah.
132 Chapter 5
Database
So, I understand that |
That finalization is |
|
called a commit |
||
operations on the |
||
operation. |
||
database are finalized |
||
Read |
||
when each transaction |
|
|
is processed |
|
|
correctly. |
Write |
Although a lock has its |
So we use different |
types of locks |
|
own role in a database, |
depending on the |
locking should not |
situation. |
be overused. It can |
|
hinder the purpose of a |
|
database: sharing data |
|
with a lot of people. |
|
How do we do that?
For example, you can |
|
but cannot |
|
While a shared lock |
perform |
||
use a shared lock |
|||
is applied, other users |
a write |
||
for a read operation |
|||
can read the data... |
operation |
||
when it is the only |
|||
|
on it. |
||
operation needed. |
|
||
|
|
||
|
|
Shared |
|
|
|
lock |
|
|
Shared |
|
|
|
lock |
|
Write?
Read?
Let's Operate a Database! 133
When performing a write operation, a user applies an exclusive lock.
Exclusive
lock
When an exclusive |
|
lock is applied, |
|
other users cannot |
|
read or write data. |
Oh, my. |
Exclusive
lock
Read? Write?
I see that there |
|
|
|
are different |
|
|
|
types of locks. |
|
When a lock is used to |
|
|
|
control two or more |
|
|
transactions, that is called |
||
|
|
concurrency control. |
|
That |
|
|
|
makes |
|
|
|
sense. |
|
|
|
|
as many users as |
|
|
In a database, |
possible to use a |
By the way, |
|
database at one time |
Cain! |
||
concurrency |
|||
while preventing |
|
||
control allows |
data conflicts from |
|
|
|
occurring. |
Yes, Tico?
134 Chapter 5
You must have studied a lot!
No, not really.
Is that |
Isn't he |
your |
dependable, |
impression? |
Ruruna? |
I suppose so…
Now go on!
All right, I will.
Let me get |
In some cases, concurrency |
|
back to the |
||
control with a lock may cause |
||
previous |
||
a problem. |
||
topic. |
||
|
Suppose Andy has |
And suppose Becky |
|
has applied an |
||
applied an exclusive |
||
exclusive lock on |
||
lock on the apple |
||
the strawberry |
||
data. |
||
data. |
||
|
Andy |
Becky |
|
For example…
Apple data |
Strawberry data |
Uh-huh.
Next, Andy may |
and Becky may |
|
try to apply an |
try to apply an |
|
exclusive lock on |
exclusive lock |
|
strawberry data, |
on apple data. |
|
locked |
locked |
|
|
||
Apple data |
Strawberry |
|
data |
||
|
What would happen then?
Well…
Since each of them |
neither one can |
must wait for the |
proceed with |
lock applied by the |
any operation. |
other user to be |
Is that it? |
released, |
|
Let me think...
They cannot do anything.
Apples
Exactly!
S trawberries
This situation, |
For example, you |
|
which is called |
|
|
a deadlock, |
can look for |
When you cancel |
cannot be |
transactions that |
a transaction, |
solved unless |
have been queued |
it’s called a |
one of the |
for a certain length |
rollback. |
locks is |
of time... |
|
released. |
|
|
C A
NC EL
and cancel them! |
Rollback? |
|
136 Chapter 5
You mean you can |
Yes! For example, if a |
cancel all the |
transaction to "discount fruits |
operations in a |
priced less than or equal to |
transaction at |
150G" has failed, |
once? |
|
|
operations for apples and |
|
strawberries must be canceled |
|
altogether, right? |
|
I see. |
So the database |
|
If anything has |
behaves as if no |
|
|
operation had |
|
occurred during |
been performed |
|
a transaction to |
at all? |
|
disable finalization, |
|
|
|
|
|
then a rollback |
|
|
is performed |
|
|
instead of |
|
Yes, sort of. |
a commit |
|
operation. |
|
|
|
Operation
Operation
That's right.
A transaction always ends with a commit or rollback operation.
All right! Now, Tico, dear, the next topic is…
There are no half measures, in other words.
Let's Operate a Database! 137
