0% found this document useful (0 votes)
6 views

5 IntermediateSQL

Uploaded by

mahesh31soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

5 IntermediateSQL

Uploaded by

mahesh31soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Intermediate SQL

Join Expressions – Join Conditions


• The join…using clause, which is a form of natural join that only requires
values to match on specified attributes
• SQL supports another form of join, in which an arbitrary join condition can be
specified
• The on condition allows a general predicate over the relations being joined
• This predicate is written like a where clause predicate except for the use of the
keyword on rather than where
 List the details of the students who have taken a course

13 Nov 2024 Dept. Of I&CT 2


Join Expressions – Join Conditions
select *
from student, takes
where student.id = takes.id;
OR
select *
from student natural join takes;
OR
select *
from student join takes on student.id = takes.id;

13 Nov 2024 Dept. Of I&CT 3


Outer Join
• An extension of the join operation that avoids loss of information
• The outer join operation works in a manner like the join operations we have
already studied, but it preserves those tuples that would be lost in a join, by
creating tuples in the result containing null values

13 Nov 2024 Dept. Of I&CT 4


Outer Join
• There are three forms of outer join:
 The left outer join preserves tuples only in the relation named before (to
the left of) the left outer join operation
List all the courses along with their pre requiste even if they don’t have
one.
select * from course natural left outer join prereq;

13 Nov 2024 Dept. Of I&CT 5


Outer Join
 Right outer Join preserves tuples only in the relation named after (to the
right of) the right outer join operation
 List all courses along with their prerequisite retaining the list of
prerequisites
select * from course natural right outer join prereq;

 Full outer join preserves tuples in both relations

13 Nov 2024 Dept. Of I&CT 6


Outer Join
• The right outer join is symmetric to the left outer join
• The full outer join is a combination of the left and right outer join types
• After the operation computes the result of the inner join, it extends with nulls
those tuples from the left-hand-side relation that did not match with any from
the right-hand-side relation, and adds them to the result and vice versa
• Display a list of all students in the Comp. Sci. department, along with the
course sections, if any, that they have taken in Spring 2021, all course sections
from spring 2021 must be displayed, even if no student from the Comp. Sci.
department has taken the course section

13 Nov 2024 Dept. Of I&CT 7


Outer Join
• The on clause can be used with outer joins
select *
from student left outer join takes on student.id = takes.id;
• on and where behave differently for outer join
• The on condition is part of the outer join specification, but a where clause
is not

13 Nov 2024 Dept. Of I&CT 8


Views( self study)
• In some cases, it is not desirable for all users to see the entire logical model
(that is, all the actual relations stored in the database)
• A view provides a mechanism to hide certain data from the view of certain
users
• SQL allows a “virtual relation” to be defined by a query, and the relation
conceptually contains the result of the query
• The virtual relation is not precomputed and stored, but instead is computed by
executing the query whenever the virtual relation is used
• Any relation that is not of the conceptual model but is made visible to a user as
a “virtual relation” is called a view
13 Nov 2024 Dept. Of I&CT 9
View Definition
• A view is defined using the create view statement which has the form
create view v as <query expression>
• Once a view is defined, the view name can be used to refer to the virtual
relation that the view generates
• View definition is not the same as creating a new relation by evaluating the
query exression
 Rather, a view definition causes the saving of an expression; the expression
is substituted into queries using the view
• View definitions are stored but not the result of the query in view!

13 Nov 2024 Dept. Of I&CT 10


View Definition
• A view of instructors without salary field
create view faculty_details as
select id, name, deptname
from instructor;
 Find all instructors in the Biology department
• View names may appear in a query any place where a relation name may
appear
• Create a view which gets the department salary total
• One view may be used in the expression defining another view
• A view relation v1 is said to depend directly on a view relation v2 if v2 is used
in the expression defining v1
13 Nov 2024 Dept. Of I&CT 11
View Definition
• A view relation v is said to be recursive if it depends on itself
 Define a view that lists the course ID and room number of all Computer
Science courses offered in the Spring 2022 semester in the Watson building
create view cse_fall_2022 as
select course.coursed, secid, building, roomnumber
from course, section
where course.coursed = section.coursed
and course.deptname = ‘Comp. Sci.;
and section.semester = ‘Spring’
and section.year = 2022;
13 Nov 2024 Dept. Of I&CT 12
View Definition

create view cseWatson as


select coursed, roomnumber
from cse_fall_2015
where building = ‘Watson’;

13 Nov 2024 Dept. Of I&CT 13


Materialized Views
• Certain database systems allow view relations to be stored, but they make sure
that, if the actual relations used in the view definition change, the view is kept
up-to-date. Such views are called materialized views
• The process of keeping the materialized view up-to-date is called materialized
view maintenance
• Applications that use a view frequently may benefit if the view is materialized
• Benefits to queries from the materialization of a view must be weighted against
the storage costs and the added overhead for updates

13 Nov 2024 Dept. Of I&CT 14


Materialized Views
create materialized view mview
build immediate/deferred
refresh complete/fast/force
on commit/demand
as
select * from instructor
where deptname = ‘ICT’;

13 Nov 2024 Dept. Of I&CT 15


Materialized View
• Build Immediate: Means materialized views (mv) created immediately
• Build Deferred: Means materialized view created after one refresh
• Refresh on Commit: Commits the data in materialized views immediately
after data inserted and committed in table – Also known as Incremental
refresh
• Refresh on Demand: Using this option you can add the condition for
refreshing data in materialized views
• Complete Refresh: Oracle database must re-execute the materialized view
query to refresh the view
• Fast refresh: Update (or insert/delete) only the rows which have been changed
on 13master
Nov 2024
tables Dept. Of I&CT 16
Integrity Constraints
• Integrity constraints guard against accidental damage to the database by
ensuring that authorized changes to the database do not result in a loss of data
consistency
• Primary key, not null, unique, check(P), where P is a predicate
• Not null: The not null specification prohibits the insertion of a null value for
the attribute
name varchar(20) not null
budget numeric(12, 2) not null
• Unique (A1, A2,…, Am
• The unique specification states that the attributes A 1, A2,…, Am form a
candidate key
13 Nov 2024 Dept. Of I&CT 17
Integrity Constraints
• Candidate keys are permitted to be null (in contrast to primary keys)
• Check(P): When applied to a relation declaration, the clause check(P)
specifies a predicate P that must be satisfied by every tuple in a relation
create table department(
deptname varchar (20),
building varchar (15),
budget numeric (12, 2) check ( budget > 0 ),
primary key ( deptname ) ) ;

13 Nov 2024 Dept. Of I&CT 18

You might also like