Presentation On Data Base & SQL
Presentation On Data Base & SQL
Presentation On Data Base & SQL
Union
R UNION S
Includes all tuples that are in R or S or both.
Duplicate tuples are removed( consider only once).
For a union operation R U S to be valid, two
conditions must hold:
The relation R and S must be of the same arity, i.e.
they must have the same number of attributes.
The domains of the ith attribute of R and the ith
attribute of S must be the same for all i.
Unionall
R UNIONALL S
Includes all tuples that are in R or S or both.
Duplicate tuples are not removed. For a union
operation R U S to be valid, two conditions
must hold:
The relation R and S must be of the same arity, i.e.
they must have the same number of attributes.
The domains of the ith attribute of R and the ith
attribute of S must be the same for all i.
Intersect
R intersect S
Includes all tuples that are common for R and S.
Cartesian Product
The Cartesian Product is also an operator which works on
two sets. It is sometimes called the CROSS PRODUCT or
CROSS JOIN or UNRESTRICTED JOIN. It combines the
tuples of one relation with all the tuples of the other
relation.
Relational Algebra
Selection
Selects tuples from a relation whose attributes meet the
selection criteria, which is normally expressed as a predicate.
R2 = σ (R1,P) select
That is, from R1 we create a new relation R2 containing
those tuples from R1 that satisfy (make true) the
predicate P.
Relational Algebra
Projection
Chooses a subset of the columns in a relation, and
discards the rest.
R2 = ∏(R1,D1,D2,...Dn) project
That is, from the tuples in R1 we create a new relation
R2 containing only the domains D1,D2,..Dn.
SQL
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
What Can SQL do?
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL
Using alias:
Select *
From table1 t1, table2 t2
Where t1.columnname= t2.columnname;
Table : dept Table: Emp
Deptno# Empno#
deptname Ename
Deptno
salary
Deptno Deptname empno ename deptno Salary
10 English 101 ABC 11 35000
11 Maths 102 PQR 14 36000
12 Physics 103 XYZ 10 31000
13 Chemistry
14 Computer
Suppose we give the command
SELECT * from emp, dept;
Then we will get all possible combinations of records of both tables,
which is called CARTESIAN PRODUCT of records.
If table A has 3 records( cardinality) and table B has 4 records the
cardinality of Cartesian product of a and B will be 3X 4=12
Suppose we give the following command then we will get only the
corresponding records of both tables.
SELECT empno, ename, emp.deptno, deptname, salary
from emp, dept
where emp.deptno=dept.deptno;
Instead of using table name, we can use the alias name for the table.
SELECT empno, ename, e.deptno, deptname, salary
from emp e, dept d
where e.deptno=d.deptno;
QUERY FROM MULTIPLE TABLES: (union)
Select * from table1
union
select * from table2;
[Where table1.columnname= table2.columnname;]
( For query, both table structure should be same)
It will display the records from both tables( duplicate
record(if any) comes only once)
Like union all set relation operations can be used such as
( union all, intersect, minus)
JOINs are of three types
Equi join
Non equi join
Natural join
Equi join: In this type of joins, columns are compared for equality.
SELECT empno, ename, e.deptno, deptname, salary
from emp e, dept d
where e.deptno=d.deptno;
Non equi join : In this type of joins, columns are compared for non
equality.
Natural Join: To avoid repeated column, we can give the command like
SELECT emp.*, deptname
from emp e, dept d
where e.deptno=d.deptno;