Sie sind auf Seite 1von 12

Relation algebra

Relation algebra consists of a set of operations that take one or two relations as input and produce a
new relation as their results.
Relatin algebra is procedural language

Six basic operators
- select: o
- project: [
- union:
- set difference:
- Cartesian product: x
- rename:

Fundamental Relational
The select,project And rename operations are called unary operations,because they operate on one
relation.
The other set difference,Cartesian product,union are operate on pairs of relations and are therefore
called binary operations.
Unary Operations
The Select Operations
The select operations select tuples that satisfy a given predicate.
Select is denoted by lowercase Greek letter sigma(o ),with the predicate appearing as a subscript
Notation-o
p
(r)
Example-To select those tuples of the relation Employee whose city is Mumbai.
o
city=Mumbai

(Employee)
We combine several predicates into a larger predicates by using the connectives . (and),
v (or), (not)
Example- To select those tuples of the relation Employee whose city is Mumbai and empid is
10
o
city=Mumbai
.
empid=10


(Employee)

The Project Operations
The project operation is a unary operation that returns its argument relation,with certain attributes left
out.
Project is denoted by lowercase Greek letter pi([)
We list those attributes that we wish to appear in the result as a subscript to [
Notation-
[
attribute1,attribute2attributen
(Relation)
Example-To list only empname and city from employee relation.
[
empname,city
(Employee)

The Rename operation
Relation operatioin allows us to refer to a relation by more than one name.
Rename is denoted by lowercase Greek letter rho()
Notation-

x
(R)
It gives new name to relation R as x.
Example-Give new name to Employee realtion to Emp.

Emp
(Employee)


Binary Operations
Union Operation
The union operation is denoted as in set theory. It returns the union (set union) of
two compatible relations. For a union operation r s to be legal, we require that
- r and s must have the same number of attributes.
- The domains of the corresponding attributes must be the same.

Example

r s

The Set difference operations
Set difference is denoted by the minus sign (-). It finds tuples that are in one relation, but not in
another.
Thus r - s results in a relation containing tuples that are in r but not in s.
Example-

r-s

The Cartesian Product Operation
The cartesian product of two relations is denoted by a cross (x), written r1 x r2 for relations r1 and r2
The result of r1 x r2 is a new relation with a tuple for each possible pairing of tuples from r1 and r2.

r x s

Additional Operations
We define additional operations that do not add any power to the
relational algebra, but that simplify common queries.
n Set intersection
n Natural join
n Division
Assignment

Set-Intersection Operation
Set intersection is denoted by , and returns a relation that contains tuples that are in both of its
argument relations. It does not add any power as
r s = r - (r - s)

rs

Natural-Join Operation
Natural join combines a cartesian product and a selection into one operation.
Duplicates are removed as in all relation operations
It is denoted by a sign
Notation: r s
Example



Division Operation
Division, denoted , is suited to queries that include the phrase for all"
Notation-
r s



Assignment Operation
The assignment operation () provides a convenient way to express complex queries.
Example
n Write r s as
temp1

[
R-S
(r )
temp2 [
R-S
((temp1 x s ) [
R-S,S
(r ))
result = temp1 temp2
The result to the right of the is assigned to the relation variable on the left of the .







Handling data using SQL
SQL has become the standard relational database language. It has several parts:
Data definition language (DDL) - provides commands to
- Define relation schemes.
- Delete relations.
- Create indices.
- Modify schemes
Select Clause
The Select command simply instructs the database to retrieves information from a table.
Basic structure of an SQL expression consists of select, from and where clauses.
- select clause lists attributes to be copied - corresponds to relational algebra project.
- from clause corresponds to Cartesian product - lists relations to be used.
- where clause corresponds to selection predicate\conditions in relational algebra

General Syntax-
SELECT attribute_list FROM table_name WHERE condition;\
Example-List Employee details from Employee Table whose city is Mumbai;
SELECT * FROM Employee WHERE city=Mumbai;

DISTINCT Keyword
To force the elimination of duplicates, insert the keyword
distinct after select
Exmple:List all city where Employee is living.
SELECT DISTINCT(city) FROM Employee.

BETWEEN Operator(The range test)
BETWEEN defines the range that values must fall in to make the condition true.
BETWEEN followed by the beginning vale ,the AND keyword and the ending value.
Syntax:
SELECt attribute_List FROM table_name WHERE attr_nm BETWEEN beginning_value AND end_value;

Example:list employee details whose salary is between 10000 to 40000;
SELECT * FROM Employee WHERE salary BETWEEN 10000 AND 40000;

IN Operator(set membership test)
IN explitly defines a set in which a given value may or may not ne included.
Syntax:
SELECT attr_list FROM table_nm WHERE attr_nm IN(value1,value2,value n);
Example:
List employee details whose empid is 10,20 and 30;
SELECT * FROM Employee WHERE empid IN(10,20,30);

LIKE Operator(Pattern matching Test)
LIKE can be applied only to field of types CHAR or VARCHAR ,against which is used to find substrings.
To do this,it uses wildcards.
There are 2 types of wildcards used with LIKE.
- Underscore(_)stands for any single character.
- Percetage sign(%) stands for a sequence of any numbers of characters
Example:
List employee name where second character of first_name is g
SELECT first_name FROM employee WHERE first_name LIKE _g%;

ORDER BY clause
ORDER BY clause causes the tuples in the results of a query to appear in sorted order.
By default it list items in ascending order.
To specify order we may specify desc for descending order and asc fro ascending order.
Exmple:
List employee names in descending order
SELECT first_name FROM employee ORDER BY first_name desc;

GROUP BY clause
It allows us to define a subset of the values in a particular field in terms of another field
Example :list employee details with department wise
SELECT * FROM employee GROUP BY deptid;

HAVING clause
It used to define conditions for group of rows,does as WHERE clause used for single row.
WHERE clause cannot use with GROUP BY clause,so we use HAVING clause.
Example list employee details with department wise and living in Mumbai
SELECT * FROM employee GROUP BY deptid HAVING city=mumbai;

Aggregate functions
1.AVG()
It returns average value of attribute
Example
Display average of salary of employees
Select AVG(salary) as avg_sal from Employee;
2.SUM()
It returns sum of values of attributes.
Example
Display sum of salary of employees
Select SUM(salary ) from Employee
3.MIN()
It returns minimum of values of attributes.
Example
Display minimum salary of employee
Select MIN(salary ) from Employee
4.MAX()
It returns maximum value of attributes.
Example
Display maximum salary of employees
Select MAX(salary ) from Employee
5.COUNT()
It returns number of records of table.
Example
Display number of records of employee table
Select COUNT(*) from Employee;


INSERTION
To insert data into a relation, we either specify a tuple, or write a query whose result is the set of tuples
to be inserted. Attribute values for inserted tuples must be members of the attribute's domain.
SYNTAX:
INSERT INTO table_nm VALUES(<attrb1_val>,<attrb2_val>,.,<attrbn_val>)
INSERT INTO table_nm(attrb1_nm,attrb2_nm) VALUES(<attrb1_val>,<attrb2_val>);
IN

Das könnte Ihnen auch gefallen