资源描述
DataBase Chapter Two Outline
The set of allowed values for each attribute is called the domain of the attributer(R) is a relation on the relation schema R
Example:
customer (Customer_schema)About Key:
K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R)
K is a candidate key if K is minimal (最小超码)
Primary Key :被数据库设计者选中的用来在同一关系中区分不同元组的候选码。
Relational Algebra
Procedural language Six basic operators select: o project: fl union: u set difference:- Cartesian product: x rename: pThe operators take one or two relations as inputs and produce a new relation as a result.
Select Operation Notation: (Jp(r) p is called the selection predicate Defined as: a-p(r) = {t \ t e rand p(t)}
Where p is a formula in propositional calculus consisting of terms connected by : a (and), v (or), -i (not)Each term is one of:
<attribute> op <attribute> or <constant> where op is one of: =, w, >, >. <. <Project Operation
.⑺ Notation:
where Ai, A2 are attribute names and r is a relation name.
Union OperationNotation: 2 s
Defined as:
r us = {f | t e ror t e s} For r u s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2nd column of s)Set Difference Operation
Notation r-sDefined as:
r-s = {t | t e rand t g s}Set differences must be taken between compatible relations.
r and s must have the same arityattribute domains of r and s must be compatible
Cartesian-Product OperationNotation r x s
Defined as:
rxs = {t q | ter and q e s}Assume that attributes of r(R) and s(S) are disjoint. (That is, /? n S = 0}.
If attributes of r(R) and s(S) are not disjoint, then renaming must be used.
Rename Operation
Allows us to name, and therefore to refer to, the results of relational-algebra expressions. Allows us to refer to a relation by more than one name.
Example:
px(E)
returns the expression E under the name XIf a relational-algebra expression E has arity n, then
Px(Al9A2
returns the result of expression E under the name X, and with the
attributes renamed to Ai, 2,・…,An.
Additional OperationsSet-Intersection Operation
Notation: rnsDefined as:
r s = {t \ t e rand t e s}Assume:
r, s have the same arity attributes of r and s are compatibleNote: rns = r-(r-s)
Natural-Join OperationNotation: rtxs
Let r and s be relations on schemas R and S respectively.
Then, r rx s is a relation on schema R oS obtained as follows:
Consider each pair of tuples tr from r and ts from s.
If tr and ts have the same value on each of the attributes in R c S, add a tuple t to the result, where
t has the same value as tr on r t has the same value as ts on s0 -Join Operation
rixes= o o(rxs)Division Operation
Notation:
Suited to queries that include the phrase "for all〃.
Let r and s be relations on schemas R and 5 respectively whereR =依17 …/Am / Bl …/ Bn)
S = (Bi,Bn)The result of r-r s is a relation on schema
R - S = (4,.・・, Am)r^s = {t \ t e]~[ R-s(r) /\\/ u e s (tu e r)}
Where tu means the concatenation of tuples t and u to produce a single tupler-r s = Y\r-s (r) - Y1r-s (( YIr-s (r) x s ) —))
Assignment OperationThe assignment operation (—) provides a convenient way to express complex queries. Write query as a sequential program consisting of
► a series of assignmentsfollowed by an expression whose value is displayed as a result of the query.
Assignment must always be made to a temporary relation variable.
Example: Write r + 5 astempi <- Eks (r)
temp2 <- Ur-s ((tempi xs)- IIk-5,5 (r))result <— tempi - temp2
The result to the right of the — is assigned to the relation variable on the left of the —・
May use variable in subsequent expressions.
Extended Relational-Algebra-OperationsGeneralized Projection允许投影使用算数运算跟重命名。
Extends the projection operation by allowing arithmetic functions to be used in the projection list.
E is any relational-algebra expression
Each of Fi, F2, Fn are are arithmetic expressions involving constants and attributes in the schema of E.
Given relation creditJnfofcustomer_name, limit, credi^balance), find how much more each person can spend:
Wcustomer_nome, limit - credit_balance (creditjnfo)
Also, we can apply rename operation:
Wcustomer_name, limit- credit_balance as credit_availble(Cr6dit_info)Aggregate Functions and Operations允许使用列函数,并且分组。
Aggregation function takes a collection of values and returns a single value as a result.
avg: min:
max:
sum:
average value minimum value maximum value sum of values
count: number of values
Aggregate operation in relational algebra5—)抵(&),…与(4)(")
E is any relational-algebra expressionGi, G2 Gn is a list of attributes on which to group (can be empty)
Each Fi is an aggregate functionEach Ai is an attribute name
The schema of result is (Gi, G2 Gn , Oi,。2 …,om ).where a; is the result of applying the aggregate function F, on the multiset of values for attribute A, in the groupOuter Join
An extension of the join operation that avoids loss of information.
Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join.
Uses null values:
null signifies that the value is unknown or does not existAll comparisons involving null are (roughly speaking) false by definition.
► We shall study precise meaning of comparisons with nulls laterThe result of any arithmetic expression involving null is null.
Modification of the DatabaseDeletion
Can delete only whole tuples; cannot delete values on only particular attributesA deletion is expressed in relational algebra by: r<-r-F
where r is a relation and £is a relational algebra query.
InsertionTo insert data into a relation, we either: specify a tuple to be inserted write a query whose result is a set of tuples to be inserted
in relational algebra, an insertion is expressed by: r<- ru E
where r is a relation and E is a relational algebra expression.
The insertion of a single tuple is expressed by letting £ be a constant relation containing one tuple.
UpdatingA mechanism to change a value in a tuple without charging a//values in the tuple Use the generalized projection operator to do this task
Each Fi is eitherthe /th attribute of r, if the /th attribute is not updated, or,
if the attribute is to be updated F; is an expression, involving only constants and the attributes of r, which gives the new value for the attribute
展开阅读全文