







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








( DML ) commands of SQL are
Creation of views ^ views correspond to external DB schemas ^ In relational DBS views are regarded as derived relations which are defined by que-ries. ^ create view
as^
^ example:^ create view
major_students
as select^ *^ from
students^ where
sem > 4
The keyword “*” is a shortcut for the complete attribute list of those relations placedafter^ from. Deletion of views drop view^
^ If the^ where
clause is omitted,
P^ =^ true^ holds.
^ The result of an SQL query can contain the same tuple multiple times (multiset!). ^ If different relations have attributes with equal names, these are distinguished by therelation name.Transfer of the operations of the relational algebra into SQL ^ relation
R − select^ ^ from^ R − The declaration “” in the
select -clause indicates that all attributes of the relation
after the^ from
-clause belong to the output. ^ projection
π( R ) A ,^ B − select distinct^ A ,
B^ from^ R −^ Without the keyword
distinct^ the result is a multiset (multi-relation). ^ selection
σ( R ) F select distinct^ *^ from
R^ where^ F ^ Cartesian product
select^ *^ from
^ theta join
R^ S^ on relations F^
R ( A ,^ B ) and
select^ *^ from
R ,^ S^ where
^ union^ R
∪^ S^ of the relations
R ( A ,^ B ) and
select^ *^ from
R^ union^ select
*^ from^ S
^ difference
R^ −^ S^ of the relations
R ( A ,^ B ) and
select^ *^ from
R^ minus^ select
*^ from^ S
Duplicates and duplicate elimination ^ The usual
select^ clause does not eliminate duplicates in the result relation, whichtherefore is a multiset (multi-relation). But this can be done by using the keyword
dis-
tinct^ so that a relation is created as output. The^ minus
operation on two multisets corresponds to the semantics of the extended relational algebra. Keyword
minus^ is only used by Oracle. In SQL92 the keyword except^ is used instead. The^ union
operation defined on relations automatically eliminates duplicates. If duplicates are not to be eliminated, the keyword
all^ has to follow the keyword
union.
^ Determine the different ranks of professors.^ select distinct
rank^ from professors −^ elimination of duplicates in a table is not automatically executed for efficiency rea-sons (sorting necessary) −^ keyword
distinct^ for explicit duplicate elimination ^ Determine the names of professors who hold the lecture titled “maieutics”.^ select^ name, title^ from^ professors, lectures^ where^ pers-id = held_by
and^ title = “maieutics” rankC3C4 name^
title Sokrates^ maieutics
^ Which students attend which lecture? Output student names and lecture titles.^ select^ name, title^ from^ students, attends, lectures^ where^
students.reg-id = attends.reg-id
and attends.id = lectures.idalternative formulation using
tuple variables
that are associated to relations:
select^ s.name, l.title from^ students
as^ s, attends
as^ a, lectures
as^ l
where^ s.reg-id = a.reg-id
and a.id = l.id − relationship to the tuple relational calculus observable: a variable is bound totuples of a relation ^ Determine the names of all university employees, i.e., the names of all professorsand all assistants.( select name from assistants) union ( select name from professors)
Existential quantifier
exists ^ This operator checks whether a set of tuples specified by a subquery is empty. For anon-empty set the
exists^ operator yields
true , otherwise
false. For the operator
not
exists^ it is just vice versa. Operator^ exists
corresponds to the existential quantifier of the relational calculus. ^ example: Which professors do not hold lectures?^ select^ name^ from^ professors^ where not exists
( select^ *^ from
lectures^ where
held_by = pers-id)
The renaming operator ^ application of the
as -clause ^ use:^ −^ Two relations in the
from^ clause have attributes with the same name, which would appear in the result relation without renaming. − If an arithmetic expression is used in the
select^ clause, the result attribute does
not have a name. − explicit change of an attribute name
^ example: Which credit line do the customers still have?^ select^ name, credit-line
−^ debit^ as^
rest
from^ customersTuple variables A tuple variable in SQL is bound to a relation. Tuple variables are defined in the
from^ clause by means of the
as^ clause.
^ in particular useful in order to compare two tuples of the same relation ^ example: Which students attend which lectures?^ select^ s.name, v.title^ from^ students
as^ s, attend
as^ a, lectures
as^ l
where^ s.reg-id = a.reg-id
and^ a.id = l.id String operations ^ search patterns are described by^ −^ a percent sign (%): this represents any substring^ −^ an underscore (_): this represents any character
Aggregate functions ^ Functions
count^ (number of tuples),
sum^ (sum),
avg^ (average),
min^ (minimum) and
max^ (maximum) can be applied to a set of numbers given as a column of a relation. no other aggregate functions in SQL2 If the keyword
distinct^ is used in front of the aggregation attribute, first duplicates are eliminated before the aggregate is computed. examples: −^ How many students are there with different names?^ select^ count
( distinct^ name)
from^ students
−^ How many professors have a name starting with “Sch”?^ select^ count
(*)^ from^ professors
where^ name
like^ “Sch%”
−^ What is the average number of semesters of all students?^ select^ avg
(sem)^ from
students