Relational Calculus Introduction • Contrived by Edgar F
Total Page:16
File Type:pdf, Size:1020Kb
Starting with SQL Server and Azure SQL Database Module 5: Introducing the relational model Lessons • Introduction to the relational model • Domains • Relational operators • Relational algebra • Relational calculus Introduction • Contrived by Edgar F. Codd, IBM, 1969 • Background independent • A simple, yet rigorously defined concept of how users perceive data – The relational model represents data in the form of two- dimension tables, i.e. relations – Each table represents some real-world entity (person, place, thing, or event) about which information is collected – Information principle: all information is stored in relations A Logical Point of View • A relational database is a collection of tables – The organization of data into relational tables is known as the logical view of the database – The way the database software physically stores the data on a computer disk system is called the internal view – The internal view differs from product to product – Interchangeability principle: a physical relation can always be replaced by a virtual one (e.g., a view can replace a table) Tuples • A tuple is a set of ordered triples of attributes – A triple consists of attribute name, attribute type and attribute value – Every attribute of a tuple contains exactly one value of appropriate type – The order of the attributes is insignificant • Every attribute must have unique name – A subset of attributes of tuple is a tuple Relations • A relation is a variable consisting of a set of tuples – The order of the attributes is insignificant – Every attribute if of exactly one, strongly defined type • A tuple contains exactly one value for each attribute • A relation is normalized – A projection on a relation is a relation – There are no duplicate tuples in a relation – The order of tuples is insignificant Meaning of Relations (1) • A relation represents some real-world entity, like person, place, thing or event – An entity can be distinctly identified and is of business interest – Each representation of an entity can be uniquely identified – Relationships are associations between entities Meaning of Relations (2) • In natural language we use statements of facts or, in logic, propositions about entities of interest, asserted to be true – Employee with EmpNo 17 is named Fernando, works in department D1 and was hired on July 19th, 2003 – Reduced: {17, Fernando,D1, July 19th, 2003} – This is a tuple • Tuples in a relation show propositions Meaning of Relations (3) • Generalized forms of propositions are predicates – Employee with EmpNo (Emp#) has a name (Name), works in department (Dept#) and was hired on (Hiredate) – Terms in parenthesis are values placeholders (entity attributes) – When specific values are substituted, a predicate reduces to an individual proposition: • {17, Fernando,D1, July 19th, 2003} • Database schema shows predicates Types • Domains (= data types) are rules that define sets of valid values and operations and consists of: – A name – One or more named possible representations • One is physically stored • At least one is declared to the users – Type constraints – A set of operators permissible on the type’s values • A value is scalar if you work with it only through operations defined on the data type of the value • Relational model is type independent Types of Types • “Simple” – System-defined – Agreed-on operations • “Complex” – User-defined – Implementation of the operators is complex, they are not agreed-on • Both kinds of types are atomic • Even different “simple” types with the same representation are not meaningfully comparable – Compare employee numbers with department numbers? Boolean Relational Operators • = (equals) • ≠ (not equals) • ⊆ (subset of) • ⊇ (superset of) • ∈ (element of) • ∅ (is empty) Other Useful Operators • Order By – Does not return a relation • There must be at least one attribute of a type that supports the following type operators: > (greater than) ≥ (greater than or equal to) ≤ (smaller than or equal to) < (smaller than) Relational Algebra • Relational algebra is an open-ended collection of operators that accept relations as input parameters and return relations – Closure property of the relational algebra – Eight original operators defined by Codd include Restrict, Project, Product, Union, Intersect, Minus, Join, and Divide Restrict, Project, Product, Union Restrict Project Product Union a x a x b y a y c b x b y c x c y Intersect, Minus, Join, Divide Intersect Minus Natural Join x1 y1 y1 z1 x1 y1 z1 x2 y2 y2 z2 x2 y2 z2 x3 y3 y3 z3 x3 y3 z3 y4 z4 Divide a a x x a b a y z c a z b x b y Rename, Extend, Summarize, Semijoin Rename Extend Summarize A B A C x1 y1 z1 x1 f(z1,z2) x1 y1 x1 y1 x1 y2 z2 x2 f(z3,z4) x1 y2 x1 y2 x2 y1 z3 x2 y1 x2 y1 x2 y2 z4 Semijoin x1 y1 y1 z1 x1 y1 x2 y2 y2 z2 x2 y2 x3 y3 y3 z3 x3 y3 x4 y4 Relational Calculus • Relational algebra is prescriptive • Relational calculus is descriptive – Describe the resulting relation - e.g., Divide: Return customers for whom you cannot find any employee from the USA for whom you cannot find any order placed for the subject customer and by the subject employee (and there is at least one employee from the USA) Relational Calculus: Description • A description of the desired resulting relation includes: – A description of resulting header (also called proto-tuple) – A description of constraints in terms of a predicate • Logical expressions using standard logical operators ¬ (Not), ∧ (And), and ∨ (Or) • Quantified expressions with the existential quantifier ∃ (Exists) and the universal quantifier ∀ (For all) End of module 05: Introducing the relational model.