Dbms Unit 2
Dbms Unit 2
Drop table
The Syntax of the drop table with example
drop table r; drop table student;
SQL Data Manipulation
Insert
Type-1:
insert into student values (‘A101’, ’Mahesh’, 520, 85.73);
Type-2:
insert into student(sid, sname, tmarks, avg) values
(‘A101’, ’Mahesh’, 520, 85.73);
Type-3:
insert into student values(‘&sid’, ‘&sname’, &tmarks,
&avg);
SQL Data Manipulation(Cont.)
Update
update student update student
set tmarks=520 set avg=tmarks/5;
where sid=‘A101’;
Delete
delete * delete
from student; from student
Where sid=‘A105’;
Basic Structure of SQL Queries
• SQL query consists of three clauses:
select, from, and where.
String Operations
SQL permits a variety of functions on character strings:
select fname
from faculty
where dname = ’AI’
order by fname;
select *
from student
order by age desc, sname asc;
Additional Basic Operations(cont.)
Instead of :
select fname
from faculty
where salary <= 100000 and salary >= 90000;
Boolean Operations
The predicate in a where clause can involve Boolean operations such
as and, or, and not on the results of comparisons, the definitions of the
Boolean operations are extended to deal with the value unknown.
and: The result of true and unknown is unknown, false and unknown is
false, while unknown and unknown is unknown.
Set Comparison
SQL also allows < some, <= some, >= some, = some, and <> some com
select fname from faculty
where salary > some (select salary from faculty where dname = ’AI’);
SQL also allows < all, <= all, >= all, = all, and <> all comparisons.
select fname from faculty
where salary > all (select salary from faculty where dname = ’AI’);
For example,
the relation schema for r = instructor × teaches is:
for example
{ (22222, Einstein, Physics, 95000), (76543, Singh,
Finance, 80000) }.
Fundamental Operations(cont.)
Formal Definition of the Relational Algebra
Generalized Projection:
extends the projection operation by allowing operations such as
arithmetic and string functions to be used in the projection list.
generalized-projection operation has the form:
{t | ∃ s ∈ borrower( t[customer-name] =
s[customer-name]) ∧ ∃ u ∈ depositor( t[customer-
name] = u[customer-name])}
Resulting relation:
The Tuple Relational Calculus
Queries-4: Find the names of all customers having
a loan at the “ABC” branch.
{t | ∃ s ∈ borrower(t[customer-name] =
s[customer-name] ∧ ∃ u ∈ loan(u[branch-name]
= “ABC” ∧
u[loan-number] = s[loan-number]))}
Resulting relation:
The Tuple Relational Calculus
Safety of Expressions
A tuple relational calculus expression may
generate an infinite expression.
We need to restrict the relational calculus a bit.
• The of a
denoted
domain dom(P), formula P, is the
referenced in P. set of all
• These include valuesvalues
mentioned in P as well
as values that appear in a tuple of a relation
mentioned in P.
• So, the domain of P is the set of all values
explicitly appearing in P or that appear in
relations mentioned is P.
The Tuple Relational Calculus
Safety of Expressions
We may say an expression { t | P} is safe if all
values that appear in the result are values
from dom(P)
{ t | not(EMPLOYEE) } is UNSAFE!
THANK YOU