LDK R Logics for Data and Knowledge Representation Query languages Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia,

Slides:



Advertisements
Similar presentations
LDK R Logics for Data and Knowledge Representation Exercises: First Order Logics (FOL) Originally by Alessandro Agostini and Fausto Giunchiglia Modified.
Advertisements

IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Chapter 3 : Relational Model
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Relational Schemas and Predicate Logic: Notation.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
LDK R Logics for Data and Knowledge Representation Modal Logic Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia,
D ATABASE S YSTEMS I A DMIN S TUFF. 2 Mid-term exam Tuesday, Oct 2:30pm Room 3005 (usual room) Closed book No cheating, blah blah No class on Oct.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus.
1 Lecture 5: Relational calculus
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Introduction to Database Systems 1 Relational Calculus Relational Model : Topic 2.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
Rutgers University Relational Calculus 198:541 Rutgers University.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Relational Algebra Example Database Application (COMPANY) Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory.
LDK R Logics for Data and Knowledge Representation Context Logic Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia,
The Relational Model: Relational Calculus
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
Logics for Data and Knowledge Representation Exercises: Modeling Fausto Giunchiglia, Rui Zhang and Vincenzo Maltese.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Relational Calculus R&G, Chapter 4. Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
The Relational Algebra and Calculus
LDK R Logics for Data and Knowledge Representation Modal Logic Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia,
Advanced Relational Algebra & SQL (Part1 )
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
LDK R Logics for Data and Knowledge Representation Propositional Logic: Reasoning First version by Alessandro Agostini and Fausto Giunchiglia Second version.
IST 210 The Relational Language Todd S. Bacastow January 2004.
DISCRETE COMPUTATIONAL STRUCTURES CSE 2353 Fall 2010 Most slides modified from Discrete Mathematical Structures: Theory and Applications by D.S. Malik.
LDK R Logics for Data and Knowledge Representation First Order Logics (FOL) Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Web Science & Technologies University of Koblenz ▪ Landau, Germany Relational Data Model.
Copyright © Curt Hill The Relational Calculus Another way to do queries.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
IST 210 The Relational Language Todd S. Bacastow January 2005.
LDK R Logics for Data and Knowledge Representation Propositional Logic Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
LDK R Logics for Data and Knowledge Representation Modal Logic: exercises Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia,
LDK R Logics for Data and Knowledge Representation Modal Logic: exercises Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia,
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
CPSC 603 Database Systems Lecturer: Laurie Webster II, Ph.D., P.E.
A First Course in Database Systems
COMP3017 Advanced Databases
Relational Calculus Chapter 4, Part B
Exercises: First Order Logics (FOL)
Logics for Data and Knowledge Representation
Relational Databases The Relational Model.
Relational Databases The Relational Model.
Logics for Data and Knowledge Representation
Relational Calculus.
Chapter 2: Intro to Relational Model
Logics for Data and Knowledge Representation
Logics for Data and Knowledge Representation
Relational Calculus Chapter 4 – Part II.
Presentation transcript:

LDK R Logics for Data and Knowledge Representation Query languages Originally by Alessandro Agostini and Fausto Giunchiglia Modified by Fausto Giunchiglia, Rui Zhang and Vincenzo Maltese

2 Outline  Relational and Algebraic Structures  Answer set  Relational schemas and Databases  Query languages  Domain Relational Calculus  Tuple Relational Calculus  SQL 2

Relational Structure  A relational structure is a mathematical structure of the form S = where: - D is a non-empty domain (a set of objects) - R i with 1≤ i ≤ n is a relation over D of any arity 3 is a relational structure STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Algebraic Structure  A mathematical structure S is an algebraic structure if S = where each f i with 1≤ i ≤ n is a function.  The term structure is therefore used to denote either a relational structure or an algebraic structure. If D is finite, we say that the structure S is finite 4 is an algebraic structure STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Answer Set  FOL can be used as query language on structures  Let be γ = γ( x 1, …, x n ) a FOL-formula, M a structure, a an assignment then γ is a query over M. Q γ = {(a 1, …, a n )  D n | M ⊨ γ [a 1, …, a n ]}  Q γ is called the answer set (or retrieval set) of γ. Since model checking takes polynomial time, also the problem of finding the answer set takes polynomial time. NOTE: A database is a relational structure. [Codd, 1970] Tables in databases are finite relations. 5 STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Example (I)  DB = D = {Enzo, Max, Mary, LDKR, ML, Fausto, Alex} Attend = {(Enzo, LDKR, Fausto), (Max, LDKR, Fausto), (Mary, ML, Alex), (Mary, LDKR, Fausto)} γ = Attend( Enzo, LDKR, x )Q γ = {(Fausto)} γ = Attend( Mary, x, Alex ) Q γ = {(ML)} γ = Attend( x, LDKR, Fausto ) Q γ = {(Enzo), (Max), (Mary)} 6 ATTEND STUDENTCOURSEPROFESSOR EnzoLDKRFausto MaxLDKRFausto MaryMLAlex MaryLDKRFausto STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Example (II)  DB = D = {Enzo, Max, Mary, LDKR, ML, Fausto, Alex} Attend = {(Enzo, LDKR, Fausto), (Max, LDKR, Fausto), (Mary, ML, Alex), (Mary, LDKR, Fausto)} γ = ∃ z Attend( Mary, x, z )Q γ = {(ML), (LDKR)} Q γ is such that DB ⊨ ∃ z Attend( Mary, x, z ) [a(x) = ML] or DB ⊨ ∃ z Attend( Mary, x, z ) [a(x) = LDKR] 7 STUDENTCOURSEPROFESSOR EnzoLDKRFausto MaxLDKRFausto MaryMLAlex MaryLDKRFausto ATTEND STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Relational schema and database schema  A relational schema consists of a name and an arity  A relational schema is a FOL-formula.  A database schema is a collection of relational schemas plus a domain 8 Attend(x, y, z) has name “Attend” and arity 3. D + Attend(x, y, z) + Student(x, y) + Course(x, y) STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Relational instance and database instance  A n-ary relational instance is an n-ary relation R  D n  A database instance is a collection of relational instances over a domain NOTE: a database instance is a relational structure 9 Attend(Enzo, LDKR, Fausto) DB + the collection of rows in the tables STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Relational database  A relational database is a finite relational structure DB =  In particular a DB is said to be in first normal form if: - the objects in D are atomic (i.e. are not sets) - the relations R 1, …, R n are defined over D - the order of the tuples in each R i does not matter (is a set) 10 STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Domain Relational Calculus (DRC) “List name, position and manager of the employees” γ = Employee(x, y, z ) Q γ = All the tuples in the Relation “List managers of the employees named Enzo” γ = ∃ y Employee( Enzo, y, z ) Q γ = {(Fausto)} 11 NAMEPOSITIONMANAGER EnzoPhDFausto ProfessorBassi FerozPhDEnzo EMPLOYEE STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

SELECT “List name and position of the employees having a manager” γ = ∃ z Employee(x, y, z ) Q γ = {(Enzo, PhD), (Fausto, Professor), (Feroz, PhD)} NOTE: The ∀ quantifier is used to state a condition that must be true for all the tuples in the database, i.e. no missing values. 12 NAMEPOSITIONMANAGER EnzoPhDFausto ProfessorBassi FerozPhDEnzo EMPLOYEE STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

FILTER “List name and position of the employees with Fausto as manager” γ = ∃ z Employee(x, y, Fausto, z ) Q γ = {(Enzo, PhD)} “List name and position of the employees with age > 28” γ = ∃ w ∃ z (Employee(x, y, z, w) ∧ (w > 28)) Q γ = {(Enzo, PhD), (Fausto, Professor)} 13 NAMEPOSITIONMANAGERAGE EnzoPhDFausto35 FaustoProfessorBassi40 FerozPhDEnzo20 EMPLOYEE STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

JOIN “List name of faculty members and their department” γ = ∃ y ∃ z ( Faculty(x, y, z ) ∧ Dept(x, w) ) Q γ = {(Enzo, DISI), (Fausto, DISI), (Feroz, MATH)} “List name of faculty members who belong to all departments” γ = ∀ w ( ∃ x Dept(x, w) → ∃ y ∃ z (Faculty(x, y, z) ∧ Dept(x, w))) 14 NAMEPOSITIONMANAGER EnzoPhDFausto ProfessorBassi FerozPhDEnzo NAMEDEPT EnzoDISI FaustoDISI FerozMATH FACULTYDEPT STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Problems with DRC  Atomic objects in the domain cannot be distinguished by column Faculty(Phd, 28, Enzo, Fausto) is a valid (but unwanted) query  The order of the columns is important. The DB must be queried by remembering the order of the columns To overcome these problems we use the Tuple Relational Calculus (see next slide) 15 STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Tuple Relational Calculus (TRC)  We define a set of types { t 1, …, t n }. The set of objects D is then partitioned as follows: D = D t 1 ∪ … ∪ Dt n where Dt i is the set of objects of type t i  We introduce a set of attributes, i.e. pairs of the form (name, type). Each pair is a typed attribute and the name is the attribute (that correspond to the name of the columns).  A relational schema is a relation name with a tuple of typed attributes. 16 Employee(Name String, Position String, Manager String, Age Int) STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Queries in Tuple Relational Calculus “List all employees who have an age of exactly 35” γ = Employee(t) ∧ (t.age = 35) “List name and position of the employees who have age 35” γ = ∃ t : {name, position} (Employee(t) ∧ t.age = 35)  We write t.age to extract from the tuple t the content of the attribute called “age”  t is called a tuple variable  In TRC variables range over tuples and not over the domain D 17 STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Structured Query Language (SQL)  A friendly face over the Tuple Relational Calculus SELECT {T1.attrib, …, T2.attrib} FROM {relation} T1, {relation} T2, … WHERE {predicates} 18 STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES

Queries in Structured Query Language “List name and position of the employees with Fausto as manager” SELECT name, position FROM employee WHERE manager = ‘Fausto’ 19 EMPLOYEEPOSITIONMANAGER EnzoPhDFausto ProfessorBassi FerozPhDEnzo EMPLOYEE STRUCTURES :: ANSWER SET :: RELATIONAL SCHEMAS AND DATABASES :: QUERY LANGUAGES