190 likes | 496 Views
Outer 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:
E N D
Outer 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 exist • All comparisons involving null are (roughly speaking) false by definition. • We shall study precise meaning of comparisons with nulls later
branch_name loan_number amount Downtown Redwood Perryridge L-170 L-230 L-260 3000 4000 1700 customer_name loan_number Jones Smith Hayes L-170 L-230 L-155 Outer Join – Example • Relation loan • Relation borrower
loan_number branch_name amount customer_name L-170 L-230 Downtown Redwood 3000 4000 Jones Smith • Left Outer Join loan borrower loan_number branch_name amount customer_name L-170 L-230 L-260 Downtown Redwood Perryridge 3000 4000 1700 Jones Smith null Outer Join – Example • Join loan borrower
Right Outer Join loan borrower loan_number branch_name amount customer_name L-170 L-230 L-155 Downtown Redwood null 3000 4000 null Jones Smith Hayes • Full Outer Join loan borrower loan_number branch_name amount customer_name L-170 L-230 L-260 L-155 Downtown Redwood Perryridge null 3000 4000 1700 null Jones Smith null Hayes Outer Join – Example
Null Values • It is possible for tuples to have a null value, denoted by null, for some of their attributes • null signifies an unknown value or that a value does not exist. • The result of any arithmetic expression involving null is null. • Aggregate functions simply ignore null values (as in SQL) • For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same (as in SQL)
Null Values • Comparisons with null values return the special truth value: unknown • Three-valued logic using the truth value unknown: • OR: (unknownortrue) = true, (unknownorfalse) = unknown (unknown or unknown) = unknown • AND:(true and unknown) = unknown, (false and unknown) = false,(unknown and unknown) = unknown • NOT: (not unknown) = unknown • In SQL “P is unknown”evaluates to true if predicate P evaluates to unknown • Result of select predicate is treated as false if it evaluates to unknown
Modification of the Database • The content of the database may be modified using the following operations: • Deletion • Insertion • Updating • All these operations are expressed using the assignment operator.
Deletion • A delete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database. • Can delete only whole tuples; cannot delete values on only particular attributes • A deletion is expressed in relational algebra by: r r – E where r is a relation and E is a relational algebra query.
r1 branch_city= “Brooklyn”(account branch ) r2 account_number,branch_name, balance (r1) r3 customer_name, account_number(r2 depositor) depositor depositor – r3 account account – r2 Deletion Examples • Delete all account records in the Perryridge branch. account account – branch_name = “Perryridge”(account ) • Deleteall loan records with amount in the range of 0 to 50 loan loan – amount 0Λamount 50 (loan) • Delete all accounts of Brighton branch.
Insertion • To 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 r E where r is a relation and E is a relational algebra expression. • The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.
r1 (branch_name = “Perryridge” (borrower loan)) r2 loan_number, branch_name(r1) account account (r2 X {200}) depositor depositor customer_name, loan_number(r1) Insertion Examples • Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch. account account {(“A-973”,“Perryridge”, 1200)} depositor depositor {(“Smith”, “A-973”)} • Provide as a gift for all loan customers in the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account.
Updating • A mechanism to change a value in a tuple without changing all values in the tuple • Use the generalized projection operator to do this task • Each Fi is either • the I th attribute of r, if the I th attribute is not updated, or, • if the attribute is to be updated Fi is an expression, involving only constants and the attributes of r, which gives the new value for the attribute
account account_number, branch_name, balance * 1.05(account) Update Examples • Make interest payments by increasing all balances by 5 percent. • Pay all accounts with balances over $100, 6 percent interest and pay all others 5 percent account account_number, branch_name, balance * 1.06( Balance 100 (account )) account_number, branch_name, balance * 1.05 (Balance 100 (account))