Exam Flashcards
(102 cards)
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.