Professional Documents
Culture Documents
Relational Algebra:-
The relational algebra is procedural query language . It
consists of a set of operations that take one or two relation as input and
produce a new relation as their result. The fundamental operations in the
relational algebra are select,project,Union, set difference,Cartesian product
and Rename.In addition to the fundamental operations there are several
other operations namely set intersection,natural join, division and
assignment. These operations will be defined in term of the fundamental
operations.
Fundamental operations:-
Select, project and rename operation are called unary
operations because the operate on one relation. The other three operations
operate on pair of relations are therefore called binary operations.
Select operation:-
Select operation selects tuples that satisfy a given predicate.we use the
greek letter sigma(σ) to denote selection. The predicate appears as a
subscript to σ. The argument relation is given in parenthescs following the
σ.thus ,to select those tuples of the loan relation where the branch is “SBI
DB Road”.
σbranch_name=”SBI DB Road”^Amount>1200(loan)
predicate.
Project operation:-
πLoan_number,Amount(Loan)
Result:-
Loan_numb Amoun
er t
L-10 1000
L-15 1500
L-20 1700
L-17 2000
ΠC_Name
(σ (Customer))
C_Name=”Madhepura”
Result:-
C_Name
Cartesian- Product
Operation:- Amit
The Cartesian –
Sumit
Product Operation denoted by
a Cross(Х) allows us to
Combine information from any two relation .We write the Cartesian product
of relation r1 and r2 as r1 Х r2 .
Borrower
C_Name Loan_number
Amit L-17
Sumit L-29
Ajit L-22
Loan
Branch_Name Loan_number Amount
σBranch_Name=”SBI DB Road”(BorrowerХLoan)
Natural-join operation:-
C_Name Borrower.Loan_number Branch_name Loan.Loan_number Amount
Find the names of all customers who have a loan at the bank and find the amount
of the loan. This query can be expressed using the natural join as follows:-
Πc_Name ,Loan_number,Amount(Borrower Loan)
Result:-
C_Name Loan_number Amount
Amit L-17 1000
Sumit L-29 2000
Ajit L-22 3000
JOIN:-
A JOIN is a means for combining fields from two tables by using values
common to each. ANSI standard SQL specifies four types of JOINs:
INNER, OUTER, LEFT, and RIGHT. In special cases, a table (base
table, view, or joined table) can JOIN to itself in a self-join.
Borrower
C_Name Loan_number
Amit L-17
Sumit L-29
Ajit L-22
Loan
Branch_Name Loan_number Amount
Loan.Loan_number=Borrower.Loan_number
OR
Algebra:- ∏Branch_name,Loan.Loan_number,C_name,Amount,Borrower.Loan_number
(
(σBorrower.Loan_number=Loan.Loan_number Borrower Loan))
Result:-
Example:-
Relational Algebra:-
∏Branch_name,Loan.Loan_number,Amount,C_name,Borrower.Loan_number
σ (Borrower= Loan)
( Loan.Loan_number=Borrower.Loan_number )
Result:-
Example:-
Relational Algebra:-
∏Branch_name,Loan.Loan_number,Amount,C_name,Borrower.Loan_number
σ (Borrower =Loan)
( Loan.Loan_number=Borrower.Loan_number )
Result:-
Example:-
Relational Algebra:-
∏Branch_name,Loan.Loan_number,Amount,C_name,Borrower.Loan_number
σ (Borrower = =Loan)
( Loan.Loan_number=Borrower.Loan_number )
Result:-
Suppose a customer wants to buy a car and a boat, but she doesn't want to
spend more money for the boat than for the car. The θ-join on the relation
CarPrice ≥ BoatPrice produces a table with all the possible options.
Car Boat
CarMo CarPri BoatMo BoatPri
del ce del ce CarMo CarPri BoatMo BoatPri
CarA 20'000 Boat1 10'000 del ce del ce
CarB 30'000 Boat2 40'000 CarA 20'000 Boat1 10'000
CarC 50'000 Boat3 60'000 CarB 30'000 Boat1 10'000
CarC 50'000 Boat1 10'000
CarC 50'000 Boat2 40'000
R φ S = σφ(R × S)
In case the operator θ is the equality operator (=) then this join is also
called an equijoin.
Note, however, that a computer language that supports the natural join and
rename operators does not need θ-join as well, as this can be achieved by
selection from the result of a natural join (which degenerates to Cartesian
product when there are no shared attributes).
The set- Difference operation denoted by (-) allow us to find tuples that are
in one relation but are not in another . the expression r-s result in a relation
containing those tuples in r but in s.
Borrower
C_Name Loan_number
Amit L-17
Sumit L-29
Ajit L-22
Depositor
C_name Account_number
Amit A-125
Sumit A-120
Manish A-110
Ajit A-112
Alok A-111
∏C_name(Depositor)-∏C_name(Borrower)
Result:-
C_name
Manish
Alok
Union Operation:-
∏C_name(Depositor) ∏C_name(Borrower)
Result:-
C_name
Amit
Sumit
Manish
Ajit
Alok
Intersection Operation:-
∏C_name(Depositor) ∏C_name(Borrower)
Result:- C_name
Amit
Sumit
Ajit
Relational Calculus:-
Tuples:-
{ t | p(t) }
i.e if is the set of all tuples t such that predicate p is true for t.
Ex:-
Mathematical logic:-
t r (Q(t))
Means there exist a tuples t in relation r such that predicate Q(t)
is true.
Example:-
{t| s Loan(t[Loan_number]=s[Loan_number]^s[Amount]>1200)}
We read the preceding expression as the set of all tuple t such that there
exists a tuple s in relation loan for which the value of t and s for the
Loan_number attributes are equal and the value of s for the amount
attribute is greater then 1200.
An atom is aformula.
{ <x1,x2,………..xn> | p(x1,x2,…….xn) }
Ex:-
Find the Branch_name, Loan_number and Amount for Loan over 1200
Where b is a Branch_name.
l is a loan_number.
a is a Amount.
Key:-
A key is a single or combination of multiple fields. Its purpose is to access or
retrieve data rows from table according to the requirement. The keys are
defined in tables to access or sequence the stored data quickly and
smoothly. They are also used to create links between different tables.
Types of Keys
The following tables or relations will be used to define different types of
keys.
Primary Key:-
The attribute or combination of attributes that uniquely identifies a row or
record in a relation is known as primary key.
Secondary key:-
A field or combination of fields that is basis for retrieval is known as
secondary key. Secondary key is a non-unique field. One secondary key
value may refer to many records.
Foreign Key:-
A foreign key is an attribute or combination of attribute in a relation whose
value match a primary key in another relation. The table in which foreign
key is created is called as dependent table. The table to which foreign key is
refers is known as parent table.
Codd’s Rules:-
Dr. E.F. Codd, an IBM researcher, first developed the relational data model
in 1970. In 1985, Dr. Codd published a list of 12 rules that concisely define
an ideal relational database, which have provided a guideline for the design
of all relational database systems ever since.
1) a table
3) a column
• Null means no value has been entered; the value is not known
4. Database is Self-Describing
• In addition to user data, a relational database contains data about
itself
• There are two types of tables in a RDBMS: user tables that contain the
'working' data and system tables contain data about the database
structure
• Metadata is data that describes the structure of the database itself and
includes object definitions (tables, indexes, stored procedures, etc.)
and how they relate to each other
• A view definition does not duplicate data; a view is not a copy of the
data in the source tables
• If you change data in a view, you are changing the underlying data in
the source table (although there are limits on how data can be
modified from a view)
• Views allow the creation of ‘custom tables’ that are tailored to special
needs
• Data integrity means the consistency and accuracy of the data in the
database (i.e., keeping the garbage out of the database)