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