Relational Database
Project 3
Part A - Creating the Database
Only making classes
(data members, constructors, toString)
Functionality will be added in Part B
Schemes = Relations (or Tables)
Schemes:� snap(S,N,A,P)� csg(C,S,G)
S | N | A | P |
| | | |
| | | |
| | | |
snap
C | S | G |
| | |
| | |
| | |
| | |
| | |
csg
Scheme
Name
Attribute
Facts = Tuples (or Rows)
Facts:� snap('12345','Charlie','12 Apple St.','555-1234').� snap('67890','Lucy','34 Pear Ave.','555-5678').� snap('33333','Snoopy','12 Apple St.','555-1234').� csg('CS101','12345','A').� csg('CS101','67890','B').� csg('EE200','12345','C').� csg('EE200','33333','B').� csg('CS101','33333','A-').
snap
C | S | G |
'CS101' | '12345' | 'A' |
'CS101' | '67890' | 'B' |
'EE200' | '12345' | 'C' |
'EE200' | '33333' | 'B' |
'CS101' | '33333' | 'A-' |
csg
S | N | A | P |
'12345' | 'Charlie' | '12 Apple St.' | '555-1234' |
'67890' | 'Lucy' | '34 Pear Ave.' | '555-5678' |
'33333' | 'Snoopy' | '12 Apple St.' | '555-1234' |
Collection of Tuples
Tuple
Values
Relation as a Whole
snap
S | N | A | P |
'12345' | 'Charlie' | '12 Apple St.' | '555-1234' |
'67890' | 'Lucy' | '34 Pear Ave.' | '555-5678' |
'33333' | 'Snoopy' | '12 Apple St.' | '555-1234' |
Set of Tuples
Scheme
Name
Queries and Rules
Questions about the Database structure?
Rules:� �Queries:� snap(Id,'Snoopy',A,P)?� csg(Course,'33333',Grade)?
Details about Tuples
Details about Relations
snap
S | N | A | P |
'12345' | 'Charlie' | '12 Apple St.' | '555-1234' |
'67890' | 'Lucy' | '34 Pear Ave.' | '555-5678' |
'33333' | 'Snoopy' | '12 Apple St.' | '555-1234' |
Printing out Relations
and its associated attribute in the
Scheme
iterator or a for-each loop
Relation toString() results:
S='12345', N='Charlie', A='12 Apple St.', P='555-1234'
S='33333', N='Snoopy', A='12 Apple St.', P='555-1234'�S='67890', N='Lucy', A='34 Pear Ave.', P='555-5678'
snap
S | N | A | P |
'12345' | 'Charlie' | '12 Apple St.' | '555-1234' |
'33333' | 'Snoopy' | '12 Apple St.' | '555-1234' |
| 'Lucy' | '34 Pear Ave.' | '555-5678' |
for (Tuple t : mySet) {
// print each attribute & value
}
'67890'
Details about the Database
An Interpreter that manages the Database
Overall Structure
Token
Scanner
Parser
Datalog
Program
Predicate
Rule
Parameter
Interpreter
Database
Relation
Scheme
Tuple
Proj 4
Proj 5
Interpreter Pseudocode
Input: DatalogProgram
TODO
Part B - Evaluating Queries
select, project, and rename
Assumptions You Get to Make
You may assume the following about the Datalog program:
None of the pass-off driver tests will violate these assumptions!�(We're not going to give it any funny business)
Evaluating Queries
Queries:
snap(Id,'Snoopy',A,P)?
csg(Course,'33333',Grade)?
Variable
Constant
Query Examples
father(same,same)? Yes(4)� same='Alma'� same='Helaman'� same='Nephi'
same='Pahoran'
father('Pahoran',kid)? Yes(3)
kid='Pahoran'
kid='Paanchi'
kid='Pacumeni'
Schemes:
father(name, son)
Facts:
father('Alma', 'Alma')
father('Alma', 'Helaman')
father('Helaman', 'Helaman')
father('Helaman', 'Nephi')
father('Helaman', 'Lehi')
father('Nephi', 'Nephi')
father('Pahoran','Pahoran')
father('Pahoran','Paanchi')
father('Pahoran','Pacumeni')
Evaluate Query
father('Pahoran',kid)? Yes(3)
kid='Pahoran'
kid='Paanchi'
kid='Pacumeni'
Project to only keep the last column
Rename the column to say "kid" instead of "son"
Query Flowchart
What type of parameter is this?
Do a select
(Type 1)
Have we seen this variable before?
Variable
Constant
Do a select
(Type 2)
Mark it to keep for the project and rename
Yes
No
For each parameter:
Relational Operations
Select
σEditor='Smith' Books
σAuthor=Editor Books
Title | Author | Editor |
'Home Renovations' | 'Fortt' | 'Smith' |
'Scuba Diving' | 'Jones' | 'Brown' |
'Italian Cooking' | 'Smith' | 'Smith' |
'Travel the World' | 'Barker' | 'Hales' |
Books
Project
πTitle Books
πAuthor,Editor Books
Title | Author | Editor |
'Home Renovations' | 'Fortt' | 'Smith' |
'Scuba Diving' | 'Jones' | 'Brown' |
'Italian Cooking' | 'Smith' | 'Smith' |
'Travel the World' | 'Barker' | 'Hales' |
Books
Rename
ρTitle ← headline Books
ρAuthor ← writer Books
Title | Author | Editor |
'Home Renovations' | 'Fortt' | 'Smith' |
'Scuba Diving' | 'Jones' | 'Brown' |
'Italian Cooking' | 'Smith' | 'Smith' |
'Travel the World' | 'Barker' | 'Hales' |
Books
Query Flowchart
snap(X,'Snoopy',A,X)?
-This query will do type 2 select on the X variables
-Also type 1 select on the string 'Snoopy'
What type of parameter is this?
Do a select
(Type 1)
Have we seen this variable before?
Variable
Constant
Do a select
(Type 2)
Mark it to keep for the project and rename
Yes
No
For each parameter:
Evaluate Predicate -- Mark for Project and Rename
Example
Schemes:
abcde(a,b,c,d,e)
Facts:
abcde('a','b','c','d','e').
abcde('s','t','r','r','s').
abcde('t','t','t','t','t').
abcde('g','t','g','h','g').
abcde('m','n','m','n','m').
Rules:
Queries:
abcde(X,'t',X,Y,X)?
0 | 1 | 2 | 3 | 4 |
X | 't' | X | Y | X |
Var | Col |
X | 0 |
Y | 3 |
For selects:
0 | 3 |
X | Y |
For project:
For rename:
Query parameters:
abcde(X,'t',X,Y,X)? Yes(2)
X='g', Y='h'
X='t', Y='t'