Databases: Wednesday, January 21, 2009 3:20 PM
Databases: Wednesday, January 21, 2009 3:20 PM
Access control
Speed
Error recovery
Tuple Row
Attribute Column
Relation Table
Candidate key A column(s) that will have a unique value for each
row/tuple.
Difference from super key: Least number of columns
needed to provided uniqueness
Primary key Candidate key that is selected as the unique identifier for
the rows
Alternate keys Candidate keys that are not selected as primary keys
Foreign key When a key must exist in another table to exist in the
current table
A table can never have a duplicate tuple/row in a table EVER
Worst case, an entire set of columns will be the primary key
Mathematicians
Binary relation
Set of ordered pairs
# and its square { (1,1) (2,4) (3,9) } (25,5) would not fit, order within the
set does not matter
Set does not contain repetition
Ternary relations
Set of ordered 3-tuples
R3= {('Bob', 71,160), ('sue', 65,120)}
K-ary relation
Subset of the cartesian product of k domains
E. F. Codd
Mathematician who created idea for DBs
Relation (table) as we use it
Database relations
Assume we have identified domains
Domain
Finite set of values
A subset of the cartesian product of a number of domains
Cartesian product
All combinations of values
+ the list of domains that we are taking the values from
Schema (structure)
Description of the table
# of columns
Names of columns
Data types
Relation to store standard info about cars
Color: Make: Model: Year: Automatic Trans: VIN: String (primary
string string string Int Bool key)
Deck of cards
Suit: enum{H,S,D,C} Value: int/enum
Each row will be unique
Relational algebra/calculus
Unary operators
-1 i.e. -(1)
Binary operators
3-2 i.e. 3.-(2)
Results are always relations
5 basic operations
Selection σ
Selecting rows from table
Unary operation
The only things allowable in the selection statement can be from the
argument, they cannot be fro m other relations
Projection π
Subset of columns
Unary, on single table
Returns columns along with schema (structure)
Rather than SELECT *, you would only
Cartesian product X
Sets
All combinations of sets of values
PxQ
Concatenation of every row in R with every row in Q
Pairs the two rows together to create new rows
SELECT * FROM R,Q is Cartesian product in SQL
Union RUS
All rows of one table, adding (at end) to end of other table, maintain
uniqueness
Set difference R-S
Rows that R has that S doesn't
3 others that are made up of the other 5 but make life easier
Join
Intersection
Division
σ Predicate (R)
Works on a single relation R and defines a relation that contains only those
tuples (rows) of R that satisfy the specified
SELECT *
JOINS
SELECT and Cartesian together such as SELECT * FROM users and login WHERE
userid = userid
Types of join |X|
Natural Join
Most important
Equijoin of two tables over all common attributes X. one
occurrence of each common attribute, eliminated one (copy) of
that column in the resulting column
SQL
SELECT * FROM R,S WHERE R.A = S.A
SELECT R NATURAL JOIN S ON R.A=S.A
Theta join
R|X|FS === σF(RxS)
Aka: cartesian product where F is true
Equijoin
Theta join except only using =, equality, in the predicate F.
common
Outer join
Left outer join =|X|
Include all of the left side even if it doesn’t have a value in
the common attribute that is equal to the ones in the
These rows/tuples will get NULL values in the values for the
part of the newly created relation/table from the relation
where the tuple didn’t have a foreign key/value
Semi join R|>S
Let you take local table, S, send it to (distrubed database) and in
return you get the rows in R that match up with S so that you can
compute
Example
π street, city, postcode(σ (t2 x PropertyforRent) t2.proptertyNo =
propertyforrent.propertyNo)
Is the same as
π street, city, postcode(t2 |X| Property For Rent) (natural join, select from
the two tables where the similar attribute/column are equal)
Division R÷S
Take the similar columns of the two, find rows in R that have all values of S
S must be subset of R
Returns all columns in R except column(s) that were used in comparison with
S
Name Relational Algebra Tuple Relational Domain Relational
Calculus Calculus