ODBS
ODBS
Q1.
Time A B
T1 READ(DT) ------
T2 DT=DT+500 ------
T3 WRITE(DT) ------
T4 ------ READ(DT)
T5 ------ COMMIT
T6 ROLLBACK ------
Transaction A reads the value of data DT as 1000 and modifies it to 1500 which gets stored
in the temporary buffer. The transaction B reads the data DT as 1500 and commits it and the
value of DT permanently gets changed to 1500 in the database DB. Then some server errors
occur in transaction A and it wants to get rollback to its initial value, i.e., 1000 and then the
dirty read problem occurs.
Unrepeatable Read Problem
The unrepeatable read problem occurs when two or more different values of the same data
are read during the read operations in the same transaction.
Example: Consider two transactions A and B performing read/write operations on a data
DT in the database DB. The current value of DT is 1000: The following table shows the
read/write operations in A and B transactions.
Time A B
T1 READ(DT) ------
Time A B
T2 ------ READ(DT)
T3 DT=DT+500 ------
T4 WRITE(DT) ------
T5 ------ READ(DT)
Transaction A and B initially read the value of DT as 1000. Transaction A modifies the
value of DT from 1000 to 1500 and then again transaction B reads the value and finds it to be
1500. Transaction B finds two different values of DT in its two different read operations.
Time A B
T1 READ(DT) ------
T2 ------ READ(DT)
T3 DELETE(DT) ------
T4 ------ READ(DT)
Transaction B initially reads the value of DT as 1000. Transaction A deletes the data DT
from the database DB and then again transaction B reads the value and finds an error saying
the data DT does not exist in the database DB.
T1 READ(DT) ------
T2 DT=DT+500 ------
T3 WRITE(DT) ------
T4 ------ DT=DT+300
T5 ------ WRITE(DT)
T6 READ(DT) ------
Transaction A initially reads the value of DT as 1000. Transaction A modifies the value of
DT from 1000 to 1500 and then again transaction B modifies the value to 1800. Transaction A
again reads DT and finds 1800 in DT and therefore the update done by transaction A has been
lost.
Time A B
T1 READ(DT1) ------
T2 add=0 ------
T3 add=add+DT1 ------
T4 ------ READ(DT2)
T5 ------ DT2=DT2+500
T6 READ(DT2) ------
T7 add=add+DT2 ------
Transaction A reads the value of DT1 as 1000. It uses an aggregate function SUM which
calculates the sum of two data DT1 and DT2 in variable add but in between the value of DT2
get changed from 2000 to 2500 by transaction B. Variable add uses the modified value of DT2
and gives the resultant sum as 3500 instead of 3000.
b. Explain ACID properties in transaction management.
Ans: Transactions refer to the single logical units of work that access and (possibly) modify
the contents present in any given database. We can access the transactions using the read and
write operations.
If we want to maintain database consistency, then certain properties need to be followed in
the transactions known as the ACID (Atomicity, Consistency, Isolation, and Durability)
properties. Let us discuss them in detail.
A – Atomicity
Atomicity means that an entire transaction either takes place all at once or it doesn’t occur
at all. It means that there’s no midway. The transactions can never occur partially. Every
transaction can be considered as a single unit, and they either run to completion or do not get
executed at all. We have the following two operations here:
—Commit: In case a transaction commits, the changes made are visible to us. Thus,
atomicity is also called the ‘All or nothing rule’.
—Abort: In case a transaction aborts, the changes made to the database are not visible to
us.
Consider this transaction T that consists of T1 and T2: Transfering 100 from account A to
account B.
In case the transaction fails when the T1 is completed but the T2 is not completed (say,
after write(A) but before write(B)), then the amount has been deducted from A but not added
to B. This would result in a database state that is inconsistent. Thus, the transaction has to be
executed in its entirety in order to ensure the correctness of the database state.
C – Consistency
Consistency means that we have to maintain the integrity constraints so that any given
database stays consistent both before and after a transaction. If we refer to the example
discussed above, then we have to maintain the total amount, both before and after the
transaction.
Total after T occurs = 400 + 300 = 700.
Total before T occurs = 500 + 200 = 700.
Thus, the given database is consistent. Here, an inconsistency would occur when T1
completes, but then the T2 fails. As a result, the T would remain incomplete.
I – Isolation
Isolation ensures the occurrence of multiple transactions concurrently without a database
state leading to a state of inconsistency. A transaction occurs independently, i.e. without any
interference. Any changes that occur in any particular transaction would NOT be ever visible
to the other transactions unless and until this particular change in this transaction has been
committed or written to the memory.
The property of isolation ensures that when we execute the transactions concurrently, it
will result in such a state that’s equivalent to the achieved state that was serially executed in a
particular order.
Let A = 500, B = 500
Let us consider two transactions here- T and T”
Suppose that T has been executed here till Read(B) and then T’’ starts. As a result, the
interleaving of operations would take place. And due to this, T’’ reads the correct value of A
but incorrect value of B.
T’’: (X+B = 50, 000+500=50, 500)
Thus, the sum computed here is not consistent with the sum that is obtained at the end of
the transaction:
T: (A+B = 50, 000 + 450 = 50, 450).
It results in the inconsistency of a database due to the loss of a total of 50 units. The
transactions must, thus, take place in isolation. Also, the changes must only be visible after we
have made them on the main memory.
D – Durability
The durability property states that once the execution of a transaction is completed, the
modifications and updates on the database gets written on and stored in the disk. These persist
even after the occurrence of a system failure. Such updates become permanent and get stored
in non-volatile memory. Thus, the effects of this transaction are never lost.
Q2.
A greater part of the Open-Source project’s development life cycle comes under
this model because it is designed to fix some explicit problem experienced by a
developer or a programmer or system administrator. If the product is a success,
sooner or later it evolves from this model into a complete product satisfying the
design requirements and meeting user needs. For example, the Apache project
started sharing patches to the web server of NCSA (National Centre for
Supercomputing Applications). Today, it has developed into the most popular
server connected to the Internet. Fig 1 depicts the lifecycle of a Build and Fix model
approach.
Rapid prototype model
In addition to defining the database structure and constraints, a data model must include
a set of operations to manipulate the data. A basic set of relational model operations
constitute relational algebra. These operations enable the user to specify basic retrieval
requests. A sequence of relational algebra operations forms a relational algebra
expression, whose result will also be a relation.
The relational algebra operations are usually divided into two groups. One group
includes set operations from mathematical set theory. Set operations include UNION,
INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT. The other
group consists of operations developed specifically for relational databases; these
include SELECT, PROJECT, and JOIN. The SELECT and PROJECT operations are
discussed first because they are the simplest. Then we will see the set operations.
Finally, we discuss JOIN and other complex operations. The relational database shown
in Figure 3 is used for our examples.
b) AVG
AVG is used to calculate the average value of an expression and it ignores NULL
values. The general syntax is:
AVG(expression)
For example, we can use AVG function to calculate the average price of all books by
using the
following query:
SELECT AVG(Price) average_book_price FROM books
The output is given below:
average_book_price
-----------------
524
------------- ------------
800 150
d) COUNT function
The COUNT function returns the count (number) of the items in expression. We can
use the COUNT function to get the number of books in the book table as follows:
SELECT COUNT(*) AS Total
FROM books
The output is given below: Total
------
220
Assignment Set – 2
Q4.
1) Integers
2) Doubles
3) Booleans
4) NULL
5) Strings
6) Arrays
7) Objects
8) Resources.
In the above types, t he first five are simple types and the next two (arrays and objects)
are compound.
1) Integers
Integers are whole numbers without a decimal point. They are the simplest types. They
can be either positive or negative. We can declare a variable like,
$ int_var = 3456;
$ a =-567
Integers can be in decimal (base 10), hexadecimal (base 16), and octal (base 8) format.
this decimal is the default format. Hexadecimal can be represented as 0X and octal
integers with a leading 0.
2) Doubles
These are numbers with a floating-point like 3.145 or 52.3. we can declare a double
number as
$ dou_var = 5.554;
$ b = 8.167
3) Boolean
The Boolean type can have only two possible values. They are TRUE or FALSE. PHP
Provides a couple of constants TRUE or FALSE for use as Booleans. For example,
If (TRUE)
Print (“this is true statements”); Else
Print (“this is false statements”);
We can interpret other statements as Booleans. If a value is a number, if it is false, it is
exactly equal to zero and true otherwise. If a string is empty, then it is false and
Otherwise true. Values of type NULL are always false. If the variable is an array and it
contains no values then it is false otherwise it is true. Valid resources in which if a
function can return that resource then it is true, otherwise it is false. We can’t use double
as Booleans.
4) NULL
NULL is a special type that only has one value that is NULL. To give a variable NULL
value, we can declare like,
&sample = NULL;
5) Strings
Strings are sequences of characters. We can declare a string like,
$string1= “this is an example string”;
In a string, the escape sequence replacements are,
\n – is replaced by the newline character
\r – is replaced by the carriage-return character
\t – is replaced by the tab character
\$ – is replaced by the dollar sign
\” – is replaced by the single double-quote
\\ – is replaced by the single backslash
6) Arrays
The array is a special variable that can store multiple values of the same type. There
are 3 types of arrays that can be used. They are, Indexed arrays -Arrays with numeric
index, Associative arrays - Arrays with named keys, and Multidimensional arrays -
Arrays containing one or more arrays.
For example, consider the array given below.
$colors= array (“red”,”green”,”blue”);
And each color can access using index. That is,
$colors[0] = “red”;
$colors[1]=”green”;
$colors[2]=”blue”;
7) Objects
Another variable type is an object. We can create an object using the keyword ‘new’.
Consider the example,
<?php? Class AB
{
Function display ()
{
Print “doing example”;
}
}
$a1= new AB;
$a1->display ();
?>
8) Resources
The resource is a special variable holding a reference to an external variable or resource.
A resource with no more references to it will delete automatically
POSTGRESQL
The main advantage of Postgres95 is that it provided SQL support to the database. So in
1996, it was renamed PostgreSQL. During Postgres95 development, more emphasis was
given to identifying and understanding existing problems in the server code. But in
PostgreSQL, the emphasis has shifted to augmenting features and capabilities, although
work continued in all areas. Enhancements incorporated in PostgreSQL are:
Table-level locking has been replaced with multi-version concurrency control and
this allows users to continue reading consistent data during writer activity and
enables hot backups from pg_dump while the database stays available for queries.
Some backend features such as sub-selects, defaults, constraints, and triggers, have
been implemented.
Additional SQL92-compliant language features have been added, including
primary keys, quoted identifiers, literal string type coercion (this is used to convert
literal strings into appropriate values), type casting, etc.
Built-in types have been improved, including new wide-range date/time types and
additional geometric type support.
Overall backend code speed has been increased by approximately 20-40% and
backend start-up time has decreased 80% since version 6.0 was released.
Q5.
INDEX Types
There are mainly three types of indices. The type of the index can be specified by a
USING clause which is optional. Three indices are,
B-tree
R-tree
Hash
In this, B-tree implementation is the default index type. This is known as both the most
capable, and commonly used indexing method. R tree is a data structure used for spatial
access methods. Hash trees allow efficient and secure verification of the contents of
larger data structures. The USING clause can be used with the keywords BTREE,
RTREE, and HASH to explicitly choose the type of index to need to create. Specifying
BTREE explicitly chooses the default index type.
b. Describe the creation of new operators in PostgreSQL.
Ans: In PostgreSQL, we can create custom operators. These are sometimes called
syntactic sugar for functions because an operator act as an alternate syntax for an
existing function. The CREATE OPERATOR SQL command creates a new operator.
The syntax is given below,
CREATE OPERATOR name (PROCEDURE = functionname [, LEFTARG = type1]
[, RIGHTARG = type2]
[, COMMUTATOR = commutatorop] [, NEGATOR = negatorop]
[, RESTRICT = restrictproc]
[, JOIN = joinproc]
[, HASHES]
[, SORT1 = leftsortop]
[, SORT2 = rightsortop])
In the above syntax, the name is the new operator name; functionname is the name of
the func- tion which is called by an operator. The remaining clauses are optional; the
LEFTARG data type creates an operator that operates only on a value to its left and the
RIGHTARG data type operates on the value to its right. For binary operators, both must
be defined.
Example:
The following example creates an operator named *#, which passes the value to its left to the
isempty () function. Isempty () procedure returns the rows which have empty (zero) values for
the given parameter. Suppose the parameter given is stock value then this procedure returns
the rows which have stock value zero.
CREATE OPERATOR *# (PROCEDURE = isempty,
LEFTARG = integer);
The following query shows the use of the new *# operator to check for books that are out of
stock in ‘Stock item’ table in which ‘stock value’ attribute returns the number of items
available.
SELECT * FROM Stockitem WHERE stockvalue*#;
Q6.
1) Integers
2) Doubles
3) Booleans
4) NULL
5) Strings
6) Arrays
7) Objects
8) Resources.
In the above types, t he first five are simple types and the next two (arrays and
objects)
are compound.
1) Integers
Integers are whole numbers without a decimal point. They are the simplest
types. They can be either positive or negative. We can declare a variable like,
$ int_var = 3456;
$ a =-567
Integers can be in decimal (base 10), hexadecimal (base 16), and octal (base 8)
format. this decimal is the default format. Hexadecimal can be represented as
0X and octal integers with a leading 0.
2) Doubles
These are numbers with a floating-point like 3.145 or 52.3. we can declare a
double
number as
$ dou_var = 5.554;
$ b = 8.167
3) Boolean
The Boolean type can have only two possible values. They are TRUE or
FALSE. PHP
Provides a couple of constants TRUE or FALSE for use as Booleans. For
example,
If (TRUE)
Print (“this is true statements”); Else
Print (“this is false statements”);
We can interpret other statements as Booleans. If a value is a number, if it is
false, it is exactly equal to zero and true otherwise. If a string is empty, then it
is false and
Otherwise true. Values of type NULL are always false. If the variable is an array
and it contains no values then it is false otherwise it is true. Valid resources in
which if a
function can return that resource then it is true, otherwise it is false. We can’t
use double as Booleans.
4) NULL
NULL is a special type that only has one value that is NULL. To give a variable
NULL value, we can declare like,
&sample = NULL;
5) Strings
Strings are sequences of characters. We can declare a string like,
$string1= “this is an example string”;
In a string, the escape sequence replacements are,
\n – is replaced by the newline character
\r – is replaced by the carriage-return character
\t – is replaced by the tab character
\$ – is replaced by the dollar sign
\” – is replaced by the single double-quote
\\ – is replaced by the single backslash
6) Arrays
The array is a special variable that can store multiple values of the same type.
There
are 3 types of arrays that can be used. They are, Indexed arrays -Arrays with
numeric
index, Associative arrays - Arrays with named keys, and Multidimensional
arrays -
Arrays containing one or more arrays.
For example, consider the array given below.
$colors= array (“red”,”green”,”blue”);
And each color can access using index. That is,
$colors[0] = “red”;
$colors[1]=”green”;
$colors[2]=”blue”;
7) Objects
Another variable type is an object. We can create an object using the keyword
‘new’. Consider the example,
<?php? Class AB
{
Function display ()
{
Print “doing example”;
}
}
$a1= new AB;
$a1->display ();
?>
8) Resources
The resource is a special variable holding a reference to an external variable or
resource. A resource with no more references to it will delete automatically
b. Describe about Join algorithm in Berkeley DB.
Ans: The join algorithm inside Berkeley DB is not used in the table handler.
MySQL executes the queries and processes the results using the table handlers
only as data retrieval and storage systems. The joining conventions of Berkeley
DB are presented here; these are not used in MySQL.
BDB supports natural joins on secondary indexes. All the data is stored as
key/value pairs. The primary table has a primary key and a value of some type.
The secondary index consists of the secondary key/value of primary key pairs.
The join can be used to retrieve records from the primary table based on a
secondary index.
Joining two primary tables is not possible. Several secondary indexes can be
used in one join; each has primary keys of the primary table as values. However,
secondary indexes cannot be joined to each other; they can only be used to
retrieve records from the primary table. To perform a join the application
programmer retrieves the desired values from each secondary index involved.
The secondary index values are looped through to find all common primary
index keys, which are used to retrieve the desired values. The secondary index
looping is a modified version of the indexed nested loop.