Exam Flashcards
What is a Data Model?
1. Mathematical representation of data. Examples: relational model = tables; semistructured model = trees/graphs. 2. Operations on data. 3. Constraints.
What is a Relation?
A Relation is a table with Attributes(column headers), Tuples(rows) and Relation name.
Relation Schema, Database Schema, Database
Relation schema = relation name and attribute list. Optionally: types of attributes. Database schema = set of all relation schemes in the database. Database = collection of relations.
A Key in Relations
Key = tuples cannot have
the same value in all key attributes.
Creating a Relation in SQL
Simplest form is: CREATE TABLE (
);
To delete a relation:
DROP TABLE ;
Elements of Table Declarations
Most basic element: an attribute and its
type.
The most common types are:
INT or INTEGER (synonyms).
REAL or FLOAT (synonyms).
CHAR(n) = fixed-length string of n characters.
VARCHAR(n) = variable-length string of up to n characters.
Declaring Keys
An attribute or list of attributes may be
declared PRIMARY KEY or UNIQUE.
Either says that no two tuples of the
relation may agree in all the attribute(s)
on the list.
Declaring Single-Attribute Keys
Place PRIMARY KEY or UNIQUE after the
type in the declaration of the attribute.
Declaring Multi-attribute Keys
A key declaration can also be another
element in the list of elements of a CREATE TABLE statement.
This form is essential if the key consists
of more than one attribute.
May be used even for one-attribute keys.
PRIMARY KEY vs. UNIQUE
- There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes.
- No attribute of a PRIMARY KEY can ever be NULL in any tuple. But attributes declared UNIQUE may have NULL’s, and there may be several tuples with NULL.
Graphs of Semi-structured Data
- Nodes = objects.
- Arc labels (properties of objects).
- Atomic values at leaf nodes (nodes with no arcs out).
- Flexibility: no restriction on:
Labels out of a node.
Number of successors with a given label.
What is an “Algebra”
Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators --- symbols denoting procedures that construct new values from given values.
What is Relational Algebra?
An algebra whose operands are relations or variables that represent relations. Operators are designed to do the most common things that we need to do with relations in a database. The result is an algebra that can be used as a query language for relations.
Core Relational Algebra
Union, intersection, and difference. Usual set operations, but both operands must have the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes.
Selection in RA
R1 := σC (R2)
1. C is a condition (as in “if” statements) that
refers to attributes of R2.
2. R1 is all those tuples of R2 that satisfy C.
Projection in RA
R1 := πL (R2)
- L is a list of attributes from the schema of R2.
- R1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1.
- Eliminate duplicate tuples, if any.
Extended Projection in RA
Using the same πL operator, we allow the list L to contain arbitrary expressions involving attributes:
- Arithmetic on attributes, e.g., A+B->C.
- Duplicate occurrences of the same attribute.
Product in RA
R3 := R1 Χ R2
Pair each tuple t1 of R1 with each tuple t2 of R2.
Concatenation t1t2 is a tuple of R3.
Schema of R3 is the attributes of R1 and then R2, in
order.
But beware attribute A of the same name in R1 and R2:
use R1.A and R2.A.
Theta-Join in RA
R3 := R1 XC R2
Take the product R1 Χ R2.
Then apply σC to the result.
As for σ, C can be any boolean-valued condition.
Natural Join in RA
A useful join variant (natural join) connects two relations by:
Equating attributes of the same name
Projecting out one copy of each pair of
equated attributes
Renaming in RA
The ρ operator gives a new schema to a
relation.
R1 := ρR1(A1,…,An)(R2) makes R1 be a relation with
attributes A1,…,An and the same tuples as R2.
Precedence of relational operators
- [σ,π,ρ] (highest).
- [Χ,(Theta-Join)].
- ∩.
- [∪,—]
Define a Bag
A bag (or multiset ) is like a set, but an element may appear more than once.
Operations on Bags
Selection applies to each tuple, so its effect on bags is like its effect on sets. Projection also applies to each tuple, but as a bag operator, we do not eliminate duplicates. Products and joins are done on each pair of tuples, so duplicates in bags have no effect on how we operate.
Bag Union
An element appears in the union of two
bags the sum of the number of times it
appears in each bag.
Bag Intersection
An element appears in the intersection
of two bags the minimum of the
number of times it appears in either
Bag Difference
An element appears in the difference A – B of bags as many times as it appears in A, minus the number of times it appears in B. But never less than 0 times
Bag Laws != Set Laws
The commutative law for
union (R∪S =S∪R) DOES hold for bags.
Set union is idempotent, meaning that S∪S = S.
However, for bags, if x appears n times in S, then it appears 2n times in S∪S.
Select-From-Where Statements
SELECT desired attributes
FROM one or more tables
WHERE condition about tuples of
the tables
Meaning of Single-Relation Query
Begin with the relation in the FROM clause.
Apply the selection indicated by the WHERE clause.
Apply the extended projection indicated by the SELECT clause.
Renaming Attributes
If you want the result to have different attribute names, use “AS ” to rename an attribute.
Patterns in SQL
A condition can compare a string to a pattern by:
LIKE or NOT LIKE
Pattern is a quoted string with % = “any string”; _ = “any character.”
Comparing NULL’s to Values
The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN.
Comparing any value (including NULL itself) with NULL yields UNKNOWN.
A tuple is in a query answer iff the WHERE clause is TRUE (not FALSE or UNKNOWN).
Three-Valued Logic
To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½.
AND = MIN; OR = MAX, NOT(x) = 1-x.
Subqueries
A parenthesized SELECT-FROM-WHERE statement (subquery ) can be used as a value in a number of places, including FROM and WHERE clauses
The IN Operator
IN () is true if and only if the tuple is a member of the relation produced by the subquery.
Opposite: NOT IN ().
IN-expressions can appear in WHERE clauses.
The Exists Operator
EXISTS() is true if and only if the subquery result is not empty.
The Operator ANY`
x = ANY() is a boolean condition that is true iff x equals at least one tuple in the subquery result.
= could be any comparison operator.
The Operator ALL
x <> ALL() is true iff for every tuple t in the relation, x is not equal to t.
That is, x is not in the subquery result.
<> can be any comparison operator.
Union, Intersection, and Difference
Union, intersection, and difference of relations are expressed by the following forms, each involving subqueries:
() UNION ()
() INTERSECT ()
() EXCEPT ()