UNIT-2

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)
Branch_name SBI DB Road SBI DB Road SBI DB Road Loan_number L-15 L-20 L-17 Amount 1500 1700 2000

In genral we allow comparisons using =,!=,<,<=,>,>= in the selection predicate.

Project operation:The project operation allows us to produce this relation. the project operation is unary operation that returns its argument relation. projection is denoted by the greek letter pi(π). we list those attributes that we wish to appear in the result as a subscript to π. The argument relation follow in parantheses. Thus the query to list all loan numbers and amount of loan can be written as

πLoan_number,Amount(Loan)
Result:-

Loan_numb er L-10 L-15 L-20 L-17

Amoun t 1000 1500 1700 2000

Composition of Relational operations:-

Customer
C_Name Amit Sumit Ajit Alok Rahul C_Street Manas Nagar Manas Nagar Santi Nagar Santi Nagar Mahendhru C_City Madhepura Madhepura Saharsa Saharsa Patna

Π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 Amit Sumit Ajit

Loan_number L-17 L-29 L-22

Loan Branch_Name Loan_number Amount SBI DB Road SBI City SBI Main L-17 L-29 L-30 1000 2000 3000

σBranch_Name=”SBI DB Road”(BorrowerХLoan)
Natural-join operation:C_Name Amit Sumit Ajit Borrower.Loan_number L-17 L-29 L-22 Branch_name SBI DB Road SBI DB Road SBI DB Road Loan.Loan_number L-17 L-17 L-17 Amount 1000 1000 1000

The Natural join is a binary operation that allow us to combine certain selections and a Cartesian product into one operation. It is denoted by the join symbol . The natural join operation forms a Cartesian

product of its two argument performs a selection forcing equality on those attributes that appear in both relation schemas and finally removes duplicate attributes. 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 Amit Sumit Ajit Loan_number L-17 L-29 L-22 Amount 1000 2000 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 Amit Sumit Loan_number L-17 L-29

Ajit

L-22

Loan Branch_Name Loan_number Amount SBI DB Road SBI City SBI Main
Sql Query:-

L-17 L-29 L-30

1000 2000 3000

select*from Loan inner join Borrower on

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:Branch_name SBI DB Road SBI City Loan_number L-17 L-29 Amount 1000 2000 C_name Amit Sumit Loan_number L-17 L-29

Left outer join:-

The result of a left outer join (or simply left join) for table A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B). This means that if the ON clause matches 0 (zero) records in B, the join will still return a row in the result—but with NULL in each column from B. This means that a left outer join returns all the values from the left table, plus matched values from the right table (or NULL in case of no matching join predicate). If the left table returns one row and the right table returns more than one matching row for it, the values in the left table will be repeated for each distinct row on the right table. Example:SQL Query:- Select * From Loan Left outer join Borrower on Loan.Loan_number=Borrower.Loan_number Relational Algebra:-

∏Branch_name,Loan.Loan_number,Amount,C_name,Borrower.Loan_number
(
σ (Borrower= Loan.Loan_number=Borrower.Loan_number Loan)

)

Result:Branch_name SBI DB Road SBI City SBI Main Loan_number L-17 L-29 L-30 Amount 1000 2000 3000 C_name Amit Sumit Null Loan_number L-17 L-19 Null

Right outer joins:A right outer join (or right join) closely resembles a left outer join, except with the treatment of the tables reversed. Every row from the "right" table (B) will appear in the joined table at least once. If no matching row from the "left" table (A) exists, NULL will appear in columns from A for those records that have no match in A.

A right outer join returns all the values from the right table and matched values from the left table (NULL in case of no matching join predicate). Example:SQL Query:- Select * From Loan right outer join Borrower on Loan.Loan_number=Borrower.Loan_number Relational Algebra:-

∏Branch_name,Loan.Loan_number,Amount,C_name,Borrower.Loan_number
(
σ (Borrower Loan.Loan_number=Borrower.Loan_number =Loan)

)

Result:Branch_name SBI DB Road SBI City Null Loan_number L-17 L-29 Null Amount 1000 2000 Null C_name Amit Sumit Ajit Loan_number L-17 L-19 L-22

Full outer join:A full outer join combines the results of both left and right outer joins. The joined table will contain all records from both tables, and fill in NULLs for missing matches on either side. Example:SQL Query:- Select * From Loan full outer join Borrower on Loan.Loan_number=Borrower.Loan_number Relational Algebra:-

∏Branch_name,Loan.Loan_number,Amount,C_name,Borrower.Loan_number
(
σ (Borrower = Loan.Loan_number=Borrower.Loan_number =Loan)

)

Result:Branch_name SBI DB Road SBI City Null SBI Main Loan_number L-17 L-29 Null L-30 Amount 1000 2000 Null 3000 C_name Amit Sumit Ajit Null Loan_number L-17 L-19 L-22 Null

θ-join and equijoin: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 CarMo CarPri del ce CarA 20'000 CarB 30'000 CarC 50'000 Boat BoatMo BoatPri del ce Boat1 10'000 Boat2 40'000 Boat3 60'000

CarMo del CarA CarB CarC CarC

CarPri BoatMo BoatPri ce del ce 20'000 Boat1 10'000 30'000 Boat1 10'000 50'000 Boat1 10'000 50'000 Boat2 40'000

If we want to combine tuples from two relations where the combination condition is not simply the equality of shared attributes then it is convenient to have a more general form of join operator, which is the θ-join (or thetajoin). The θ-join is a binary operator that is written as θ is a binary relation in the set {<, ≤, =, >, ≥} and R and S are relations. The result of this operation consists of all combinations of tuples in R and S that satisfy the relation θ. The result of the θ-join is defined only if the headers of S and R are disjoint, that is, do not contain a common attribute. The simulation of this operation in the fundamental operations is therefore as follows: 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).

Set Difference operation: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 Amit Sumit Ajit Loan_number L-17 L-29 L-22

Depositor C_name Amit Sumit Manish Ajit Alok Account_number A-125 A-120 A-110 A-112 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:A query in tuple relational calculus is expressed as { 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. We build up formulae from atoms using the following Rules: An atom is aformula.  If p1 and p2 are formula, then so are p1v p2 ,P1^P2 and P1=>p2.

 If P1(s) is formula containing a free tuple variable s, and r is a relation then

s r (p1(s)) and
also formula. relational Calculus is of the form

s r (p1(s)

Are

Domain Relational Calculus:- An expression in the domain
{ <x1,x2,………..xn> | p(x1,x2,…….xn) } Where x1,x2,………..xn represent domain variables. P represents a formula composed of atom as was the case in the tuple relational calculus.An atom in the domain relational calculus has one of the following forms: <x1,x2,………..xn>

x1,x2,………..xn

r where r is a relation on n attributes and are domain variables or domain constants.

 X(-)y where x and y are domain variable and (-) is comparsion operator. Ex:Find the Branch_name, Loan_number and Amount for Loan over 1200

{<b, l, a>|<b, l, a>

Loan ^ a> 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.

Candidate Key or Alternate key:A relation can have only one primary key. It may contain many fields or combination of fields that can be used as primary key. One field or combination of fields is used as primary key. The fields or combination of fields that are not used as primary key are known as candidate key or alternate key.

Composite key or concatenate key:A primary key that consists of two or more attributes is known as composite key.

Sort Or control key:A field or combination of fields that is used to physically sequence the stored data called sort key. It is also known s control key.

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.Data is Presented in Tables
• A set of related tables forms a database and all data is represented as tables Data within a cell is atomic. The relationships among tables are logical; there are no physical relationships among tables. Relation ship is value based.

• •

2. Data is Logically Accessible

A relational database does not reference data by physical location; there is no such thing as the ‘fifth row in the customers table' Each piece of data must be logically accessible by referencing

1) a table 2) a primary or unique key value 3) a column

3. Nulls are Treated Uniformly As Unknown
• •

Null must always be interpreted as an unknown value Null means no value has been entered; the value is not known unknown' is not the same thing as an empty string ("") or zero

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 The collection of system tables is also referred to as the system catalog or data dictionary System tables can be accessed in the same manner as user tables.

5. A Single Language is Used to Communicate with the Database Management System
• There must be a single language that handles all communication with the database management system.

RDBMS may support many languages, but there should be a single language which should provide all the Relation operations. SQL is industry standard.

6. Provides Alternatives for Viewing Data
• A relational database must not be limited to source tables when presenting data to the user Views are virtual tables or abstractions of the source tables A view is an alternative way of looking at data from one or more tables A view definition does not duplicate data; a view is not a copy of the data in the source tables
Once created, a view can be manipulated in the same way as a source table

• • •

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

7. Supports Relational Operations
• Rows are treated as sets for data manipulation operations (SELECT, INSERT, UPDATE, DELETE) A relational database must support basic relational algebra operations (selection, projection; & join ) and set operations (union, intersection, division, and difference) Set operations and relational algebra are used to operate on 'relations' (tables) to produce other relations

8. Physical Data Independence
• Applications that access data in a relational database must be unaffected by changes in the way the data is physically stored (i.e., the physical structure)

9. Logical Data Independence
• The database schema or structure of tables and relationships (logical) can change without having to re-create the database or the applications that use it

10. Data Integrity Is a Function of the DBMS
• In order to be considered relational, data integrity must be an internal function of the DBMS; not the application program Data integrity means the consistency and accuracy of the data in the database (i.e., keeping the garbage out of the database)

11. Supports Distributed Operations
• • Data in a relational database can be stored centrally or distributed Users can join data from tables on different servers (distributed queries) and from other relational databases (heterogeneous queries) Data integrity must be maintained regardless of the number of copies of data and where it resides

12. Data Integrity Cannot be Subverted

There cannot be other paths into the database that subvert data integrity; in other words, you can't get in the 'back door' and change the data in such a manner as data integrity is violated.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times