3. Relational Model Flashcards
How is the relational model related to SQL?
SQL is a practical implementation of the relational concept
SQL and the relational model are not 100% the same
What is the relational model?
It is an abstract theory based on mathematics (Set theory and Predicate Logic)
Relational Terms
1 ) Tuple
- Ordered list of elements
- Often referred to as a single record
- Implemented as a table row
2) Attributes
- Elements are called either ‘field’ or ‘attribute’
- Attributes are said to have a ‘domain’ and values
- Values are normally atomic/ scalar (not always)
3) Relation
- Set of tuples, e.g. S(A1:D1, A2:D2…Am:Dm)
- Can be thought of as a table (tables are a common implementation of a relation)
4) Schema
- Plan of how data is organized –> blueprint for database design
- Includes logical constraints (table names, attributes, entity relations)
- Often represented visually
Relational Model aspects (1)
Every relation is a set
Not every set is a relation
Every relation can be perceived as a table
Not every table is a correct perception of a table (because tables can be a representation of combinations of relations?)
Relational Model aspects (2)
Structural - the data is perceived by the user as tables, and nothing but tables
Manipulation - A set of operators available to the user to allow them to manipulate the tables
Integrity - tables satisfy integrity constraints
What question does integrity answer?
What are the ways that I could mess up this database if I wanted to?
Manipulation
Three main operators:
1) Restrict
2) Project
3) Join
Result of all of these operators is a table
Restrict
Implements logical conditionals
E.g. Department WHERE Budget >= 10M
Project
Way of extracting only certain elements of a table (useful if, for example, there is no need to see all of the columns)
E.g. Department {Dept#, Budget}
Join
Useful for combining relations
E.g. Department JOIN Employee OVER Dept #
–> combines two relations into a table using Dept# as the reference that links the two tables
Relational expressions
Output from each expression is a table
Output from one expression can form input to another
Relational expressions can be nested (i.e. operands can be relational expressions)
Optimisation
Materialised Evaluation:
- Run part of the query and store the result on a disk as a table
- Next operation of the query runs on this part
- Lower memory usage but slower
Pipelined Evaluation:
- Run next part of the query as rows are generated by first
- Higher memory usage but faster
Relational models are DECLARATIVE languages
Types of Languages
1) Procedural
- Series of steps
- Interpreter
- Need to ensure steps produce result
2) Declarative
- Describe what you want
- Interpreter figures out the steps
Relational algebra
Purely procedural query language
3 Key operation types:
1) Set operations: set union, set intersection, set difference, cartesian product
2) Relational database specific operations: selection, projection, join, set division
3) Set functions: sum, avg, count, any, max, min
Order of operations can impact performance
Procedural Languages Process
1 - Cook beef patty
2 - Toast buns
3 - Put cheese on patty
4 - Take patty off pan and place on bottom bun
5 - Put top bun on patty/cheese combo
–> Start with one piece –> keep adding to the ‘stack’ immediately once each piece is done
More concerned with the step by step process of the outcome