1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8% 
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License. 
13% 
14% The Original Code is  The ECLiPSe Constraint Logic Programming System. 
15% The Initial Developer of the Original Code is  Cisco Systems, Inc. 
16% Portions created by the Initial Developer are
17% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18% 
19% Contributor(s): 
20% 
21% END LICENSE BLOCK
22
23% File		: database-sec.texx
24% Date		: March 1992
25% Author	: Michael Dahmen
26% Modified by	: Luis Hermosilla, August 1992
27% Project	: MegaLog-Sepia User Manual
28% Content	: Tutorial on the Relational Algebra
29
30\newpage
31
32\chapter{The \eclipse Database}
33\label{database-sec}
34\index{database}
35
36\section{Introduction}
37
38\eclipse uses the Relational Database Model as the basis 
39for its database design, and adapts it to 
40allow {\em deductive rules} to be stored in relations.
41This removes the restriction found in conventional relational 
42databases of only being able to store knowledge as 
43explicit facts, and allows more complex representations of knowledge to be
44kept in the database.  
45
46The purpose of \eclipse is to give the user a {\em logic programming environment}
47in which to build large scale D/KBMSs.  This requires the storage limitations 
48found in Prolog compilers (i.e. small capacity and no persistency) to be removed
49without losing any of the logic programming power.
50Therefore the \eclipse database has been designed
51to provide mass storage of knowledge in such a way that it can be conveniently
52and efficiently accessed by logic programs.
53
54The relational approach
55used by the \eclipse database is 
56very well suited to this as a relation
57has a one-to-one correspondence with a predicate
58(a relation's name \& attributes correspond to a predicate's functor \& arguments, and
59the tuples in the relation define the clauses of the predicate).
60A logic program can therefore obtain clauses
61by extracting single tuples from a relation with {\eclipse}'s `tuple-at-a-time' operations.
62\eclipse also provides backtracking through the database to step
63through all the solutions of a goal.
64
65As an alternative method for accessing the database \eclipse provides the 
66set-oriented operations found in relational databases (i.e. selection,
67union, difference and join). 
68This gives the user the
69freedom to select either method, or even to mix them, depending on what
70is best for the particular application.
71
72By integrating the database within the logic programming environment
73the database access has been made very efficient.  Its performance
74is comparable with both Prolog compilers (which only access clauses in main memory),
75and conventional relational databases.  The user is therefore provided 
76with the functionality of both without losing the performance of either.
77
78\eclipse provides two versions of its database, one with the deductive
79capability described above and one without.  This is because DBMSs, unlike
80KBMSs, do not need the facility to store deduction rules
81and by removing this functionality from the database it is possible to 
82enhance the access performance. Therefore there is the deductive {\bf KB} version
83of the database and the non-deductive
84{\bf DB} version.
85The rest of this chapter describes how to implement a database using the DB version
86, and the next chapter shows how to implement a knowledge base using the 
87KB version.  Chapter~\ref{multi} covers Multi User \eclipse, which 
88provides concurrent access to both DB and KB versions of the database.
89
90\newpage
91\section{Building a Database}
92
93To build a database the user needs to be
94able to perform the following operations:
95
96\begin{itemize}
97\item Create the database.
98\item Define a relation.
99\item Insert clauses.
100\item Access clauses. 
101\item Remove clauses.
102\item Remove a relation.
103\end{itemize}
104This section covers these operations for the DB version of the \eclipse database.
105
106\subsection{Creating the Database}
107\label{create}
108
109A database is created with the predicate {\bf createdb/1}. \index{createdb/1}
110The argument of the predicate is either the 
111full pathname of the database, or just its name 
112(in which case it will be put in the current directory).
113For example  
114
115\begin{verbatim}
116?- createdb('/usr/applic/bank_data').
117\end{verbatim}
118or
119\begin{verbatim}
120?- createdb('bank_data').
121\end{verbatim}
122
123In both these cases (assuming that the current
124directory is `/usr/applic/') a directory will
125be created within `/usr/applic/' with the name `bank\_data'. 
126This directory contains all the necessary files for storing
127data and maintaining the database.
128
129If the database already exists and just needs to be opened, then the 
130predicate {\bf opendb/1} \index{opendb/1} can be used. 
131This opens the database which has its pathname as the argument.
132Again, if the argument is just the database name it is assumed
133to be in the current directory.
134
135The database is automatically closed when you leave \eclipse or
136when you open another database (note that only one database
137can be open at a time).  To close a database in other circumstances the 
138{\bf closedb/0} \index{closedb/0} predicate is used.
139
140\subsection{Defining a Relation}	
141\index{relation}
142\index{attribute}
143A relation is added to the database by declaring its name and all its 
144attributes. This information is entered in the database with the 
145{\bf $<=>$/2} predicate. \index{$<=>$/2}
146
147As an example let's assume we have just created a database,
148and wish to add a relation describing the employees of a certain 
149firm. The relation is called `employee' and has the 
150attributes `name', `salary',  `department' and
151`number', where `number' is a unique identifier for the employee
152and is to be used as the key of the relation. `number' is an integer
153that takes up to four bytes to store, `name' is restricted to being
154no more than twelve characters in length, `department' is stored in 4  bytes,
155and `salary' in 2 bytes.  In \eclipse this would be entered in the
156database as follows
157
158\begin{verbatim}
159?- employee <=> [integer(number,4,+),
160                 atom(name,12,-),
161                 integer(department,4,-),
162                 integer(salary,2,-)].
163\end{verbatim}
164To understand this predicate we look at its syntax
165
166\begin{verbatim}
167Relation_Name <=> [ Attribute1,
168                    Attribute2,
169                           ....
170                    AttributeN ].
171\end{verbatim}
172
173where each attribute is defined in the following way
174
175\begin{verbatim}
176Type(Name, Length, Index)
177\end{verbatim}
178
179The first argument of the {\bf \verb-<=>-} predicate is the name of the
180relation.  This can either be an atom or a variable depending
181on whether the relation is to be {\em permanent} or {\em temporary}.
182\index{permanent relation} \index{temporary relation}
183A permanent relation is one that persists in the database when 
184the database is closed, while a temporary one will be destroyed.
185In the permanent case the {\bf Relation\_Name} is an atom that
186is unique (i.e. no other relation shares this name) and of no more
187than 31 characters. When the relation is to be temporary a variable is used
188for {\bf Relation\_Name}, and this will be instantiated by the system 
189to a name for the relation. For example
190\begin{verbatim}
191?- boy <=> [ atom(name,20,+),
192             integer(age,1,-) ].
193yes
194
195?- X   <=> [ atom(name,20,+),
196             integer(age,1,-) ].
197X = p1364r1
198yes
199\end{verbatim}
200
201The two examples produce relations with the same attributes, but
202the first has the name `boy' allocated by the user and is permanent, while
203the second has the name `p1364r1' allocated by the system and is temporary.
204
205
206For each attribute we have to give four pieces of information:
207
208\begin{itemize}
209
210\item{{\bf Type}}
211
212An attribute can be one of four types:
213{\bf integer}, {\bf real} , {\bf atom}, and {\bf term}.  Floating point numbers
214are given by the real type, and character strings by the atom type. Any Prolog
215term can be stored as type term, however general Prolog terms are
216less efficient and should not be used where one of the three other types
217would be sufficient.
218
219\item{{\bf Name}}
220 
221The attribute's name is an atom or a string, which is silently
222truncated to 31 characters. Attribute names should be unique within
223a relation i.e. two different relations can share the same
224attribute name. However, the uniqueness is not tested and duplicate names
225are not rejected.
226
227\item{{\bf Length}}
228 
229Depending on the type chosen there are certain
230restrictions placed on the length of an attribute value.
231Integers can be 1, 2, or 4 bytes in length, reals are always 4 bytes and atoms
232can be any length.  For the term type this argument is ignored and there
233is no restriction on the length; the
234system allocates space as required.  An extra restriction is that the maximum number
235of bytes for the whole relation (i.e. the total of the field lengths
236for all the attributes) must not exceed 1000 bytes, where a term field counts
23712 bytes.
238
239\item{{\bf Index}}
240
241\index{Indexing}
242\index{Relation Index}
243 
244This indicates whether the relation's index should include the attribute.
245\eclipse provides a very powerful multi-attribute indexing facility
246({\bf BANG} -- Balanced And Nested Grid file), which
247allows to index equally on several attributes using a single index
248tree. This achieves good performance even with varying and unpredictable
249access patterns.
250
251The index field is {\bf +} to indicate preference in participation
252in index and {\bf --} otherwise. Attributes of type term cannot be included
253in the index. Since a relation must always have at least one index
254attribute there must always be at least one attribute that is not
255of type term.
256If no index attribute is given, all attributes (except the
257terms) are indexing.
258 
259\end{itemize}
260
261When a relation is created, the field length and index
262values need not be specified (use a free variable instead).
263The default values for field length
264are four bytes for integers and reals, and 24 characters for atoms.
265The default setting for index is {\bf --}, e.g.
266\begin{verbatim}
267?- boy <=> [atom(name,T1,I1), integer(age,T2,I2)].
268
269T1 = 24
270I1 = -
271T2 = 4
272I2 = -
273yes.
274\end{verbatim}
275
276Whenever values are entered into a relation that exceed
277the field length defined in the schema they will be
278truncated as necessary, but no error message is given.
279The maximum number of attributes allowed in a relation is 50.
280\index{attribute maximum number of}
281
282
283If a relation already exists, it is possible to use the predicate 
284{\bf \verb-<=>-} to find its schema. 
285This is done by making the first argument the name of the relation
286and the second a variable. The variable then becomes instantiated to
287the schema. For example
288
289\begin{verbatim}
290?- boy <=> X.
291
292X = [atom(name, 24, +), integer(age, 4, +)]
293yes.
294\end{verbatim}
295
296It is often useful to refer to the same relation by a number of names.
297To be able to do this {\em synonyms} must be defined. This is
298done as follows: 
299\index{synonym}
300\begin{verbatim}
301Relation_Name <-> Synonym.
302\end{verbatim}
303\index{$<->$/2}
304where {\bf Relation\_Name} and {\bf Synonym} are atoms.
305\label{syn}
306\begin{verbatim}
307e.g. boy <-> son
308\end{verbatim}
309
310{\bf son} becoming a synonym for {\bf boy}.
311
312
313The same predicate may be 
314used to find all defined synonyms for a relation if the right
315hand side is a variable, or to find the real name of a relation
316whose synonym is known if the left hand side is a variable.
317
318A particular situation when it is necessary to refer to a 
319relation by its synonym is when it is being joined with itself
320(the synonym allows the attributes of the two occurrences of the 
321relation to be distinguished in the selection conditions).
322
323\subsection{Removing a Relation}
324\index{relation - remove}
325\index{$<=>$/2}
326Temporary relations are automatically deleted at the end of the session
327or when the database is closed.
328Permanent ones can be removed from the database by using
329the {\bf \verb-<=>/2-} predicate. The call 
330\begin{verbatim}
331Relation_Name <=> [].
332\end{verbatim}
333will destroy the given relation if it exists; otherwise the call fails.
334
335\subsection{Examining the Database State}
336Two simple predicates are available that give some basic information
337about the current state of the database: 
338{\bf helpdb/0} lists the names of all the relations in the database; 
339{\bf helprel/1} gives information about a single relation. 
340\index{helpdb/0} \index{helprel/1}
341
342\begin{verbatim}
343?- helprel(department).
344
345
346RELATION : department    [real name: department]
347
348ARITY: 3
349
350ATTRIBUTES :
351    atom(dname, 20, +)
352    integer(floor, 4, +)
353    atom(manager, 20, +)
354
355NUMBER OF TUPLES : 3
356
357yes
358\end{verbatim}
359
360To get a complete listing of all the tuples stored in a
361relation along with the above information the {\bf printrel/1}
362predicate can be used.  The argument is the name of any relation in the
363database. \index{printrel/1}
364
365
366The arity and cardinality (number of tuples) of a relation may 
367be queried through two similarly named predicates
368\index{arity/2} \index{cardinality/2}
369\begin{verbatim}
370?- arity(manager, Ary), cardinality(manager, NoTups).
371Ary = 2
372NoTups = 5
373	more? -- 
374
375yes
376\end{verbatim}
377
378
379Another useful predicate is a utility provided for the comparison
380of relational schemas. The call
381\index{schema comparisons}
382\index{$<$@$>$/2}
383\begin{verbatim}
384relation1 <@> relation2
385\end{verbatim}
386will succeed if the number and types of the attributes of the 
387two relations are the same. 
388
389
390\section{Data Manipulation - Relational Algebra}
391\label{alg1}
392Data can be retrieved from the database by using either
393relational algebra or tuple-at-a-time operations.  We start
394with relational algebra.
395
396\label{isr}
397Just as a predicate {\bf is/2} is provided to allow the writing of arithmetic 
398expressions in Prolog, the predicate {\bf isr/2} \index{isr/2}
399permits the inclusion of relational algebraic \index{relational algebra}
400expressions in \eclipse programs.  
401The following sections will illustrate
402its use, and introduce some additional predicates specific to \eclipse.
403A call to the {\bf isr} predicate has the form
404\begin{verbatim}
405Relation_Name   isr   Relational_Expression
406\end{verbatim}
407
408This creates a temporary relation and uses the {\bf Relational\_Expression}
409to determine which tuples from other relations should be inserted into it. 
410If {\bf Relation\_Name} is an atom the relation takes it for its name, 
411and if it is a variable then \eclipse generates its own name for the relation.  
412In both cases the relation is temporary, being destroyed when the database 
413is closed. If the relation already exists the tuples given by
414{\bf Relational\_Expression} are added to it.
415The {\bf Relational\_Expression} consists of relations, relational operators, 
416and conditions.  The operators are given in table~\ref{ops}.
417The following sections demonstrate how different operators are used
418to retrieve the required sets of data.
419
420\begin{table}
421\centering
422\begin{tabular}{||c|c||}        
423 \multicolumn{2}{c}{\em Operators}  \\ \hline
424 :\verb+*+: & join \\
425 :\verb-+-: & union  \\
426 :\verb+-+: & difference  \\
427 :\verb+^+: & projection   \\ \hline
428 \multicolumn{2}{c}{ } \\
429 \multicolumn{2}{c}{\em Selection}  \\ \hline
430 \verb-where- & selection condition \\
431 \verb-and- & conjunction \\  \hline
432 \multicolumn{2}{c}{ } \\
433 \multicolumn{2}{c}{\em Predicates}  \\ \hline
434 \verb+isr+ & relational assignment \\
435 \verb+<--+ & set deletion \\
436 \verb-<++- & set insertion \\
437 \verb-++>- & set retrieval  \\ \hline
438\end{tabular}
439\caption{ Relational Operators and Predicates in \eclipse}
440\label{ops}
441\end{table}
442
443\subsection{Selection Conditions}
444\label{selection} \index{selection}
445
446When we want  tuples of a relation
447satisfying some condition (e.g. `All employees earning over 100000 DM', or
448`Any pupil who studies English'),  a boolean condition may  be
449used in an {\bf isr} relational expression to perform selection
450on a relation. This condition is introduced by the word {\bf where}.
451\index{where condition}
452\begin{verbatim}
453high_paid isr employee where salary >= 100.
454% All employees earning over 100 thousand
455
456EngStudent isr pupil where degree == 'English'
457% Any pupil who studies English
458\end{verbatim}
459
460Both these examples have {\em simple attribute expressions} for their
461conditions; these are
462a single comparison of attributes and atomic Prolog terms using the operators
463\verb-==-, \verb-\==-, \verb->=-, \verb-=<-, \verb-<- and \verb->-. 
464Any operator may compare either two attributes or an attribute with
465a constant. Note that attributes of 
466type term cannot be used in these expressions.
467
468{\em Complex attribute expressions} can be formed to give a conjunction 
469by using the connective {\bf and}. Further examples
470\index{attribute expression}
471\index{conjunction} 
472\begin{verbatim}
473?- grade2 isr emp where salary < 100 and salary > 50.
474
475?- f_depts isr department where manager == 'F*'.
476\end{verbatim}
477
478The last example illustrates the use of the wild character `\verb-*-' in 
479\index{wild character}
480comparisons involving attributes of atom type. When a string containing
481the wild character is compared against an attribute, the 
482wild character matches any string of zero or more characters. So, in 
483the example above, {\bf \verb-f_depts-} is created from all tuples of 
484{\bf department} whose manager attribute is a string beginning
485with `F'. Note that the wild character `\verb-*-' may only occur as last
486character of the pattern string.
487
488
489
490Similar the wild character `\verb-?-' in an atom comparison matches
491exactly one character, e.g. in the example below all names of three
492characters length are selected. 
493\begin{verbatim}
494?- three_long isr department where name == '???'.
495\end{verbatim}
496
497The wild characters are only interpreted in equality and inequality
498conditions. In range 
499conditions (\verb->=-, \verb-=<-, \verb-<- and \verb->-)
500they are taken as normal characters,
501since the interpretation would make no sense.
502
503
504
505There is no disjunctive `or' for complex attribute expressions,
506but disjunctive conditions can be formed as follows:
507
508\begin{verbatim}
509% Giving: answer isr Rel1 where ( Cond1 or Cond2 )
510
511% poor performance solution
512
513?- X1 isr Rel1 where Cond1,
514   X2 isr Rel1 where Cond2,
515   answer isr X1 :+: X2.
516
517% better performance
518
519?- answer isr Rel1 where Cond1,
520   answer <++ Rel1 where Cond2,
521
522\end{verbatim}
523
524The first solution inserts each tuple twice in a relation. Tuple
525insertions are fairly expensive and much more expensive than 
526the retrievals. Therefore the second solution requires only about
527half the time.
528
529\subsection{Join}
530\label{joins} \index{join} \index{$:*:$}
531
532The join of two relations is performed by the operator \verb-:*:-.
533An optional {\bf where} condition is given to indicate the condition on which
534the join is made. Thus a join takes the form
535\begin{verbatim}
536ResultRel isr JoinRel1 :*: JoinRel2 where Condition
537\end{verbatim}
538where {\bf JoinRel1} and {\bf JoinRel2} are relations 
539which are joined according to  {\bf Condition}
540to produce the relation {\bf ResultRel}. Some examples of 
541joins are
542\begin{verbatim}
543manager_grade1 isr employee :*: manager 
544               where employee_id == manager_id and salary > 200.
545Manages isr manager :*: department where manager_department == department_id.
546\end{verbatim}
547
548The join condition may compare attributes from both relations
549with each other or with constants using any comparison operator.
550This allows all kind of joins, including {\em theta joins}.
551\index{join theta}
552If no join condition is given a {\em cartesian product} is performed.
553
554It is possible for an ambiguity to arise in the  expression
555specifying the join condition if the relations participating in the
556join have commonly named attributes.\index{join ambiguity (\verb-^-)}
557To resolve the ambiguity the notation \verb-^- is introduced. 
558If the relations {\em r1(a, b)} and {\em r2(a, c)} exist, they may be 
559joined on their first attributes by:
560\begin{verbatim}
561?- rel isr r1 :*: r2 where r1^a == r2^a.
562\end{verbatim}
563
564\subsection{Difference}
565\index{difference of relations} \index{$:-:$}
566The difference of two relations is performed by the operator \verb+:-:+. 
567An optional {\bf where} condition is given to indicate the condition on which
568the difference is made. Thus a difference takes the form
569\begin{verbatim}
570ResultRel isr DiffRel1 :-: DiffRel2 where Condition
571\end{verbatim}
572The simplest example of a difference is
573\begin{verbatim}
574?- X isr a :-: b.
575X = p2249r7
576
577yes
578\end{verbatim}
579which creates the temporary relation `p2249r7' containing those
580tuples of `a' which do not occur in `b'.
581
582The difference condition may compare attributes from both relations
583with each other or with constants using any comparison operator.
584This allows not only simple differences as above but also general
585difference, which are also called {\em complementary joins}.
586\index{join complementary}
587If no difference condition is given the implicit condition is `all
588attributes are equal'.
589
590\subsection{Union}
591\index{union of relations} \index{$:+:$}
592The union of two relations is performed by the operator \verb-:+:-.
593An optional {\bf where} condition is given to indicate the condition on which
594the union is made. Thus a union takes the form
595\begin{verbatim}
596ResultRel isr UnionRel1 :+: UnionRel2 where Condition
597\end{verbatim}
598
599Note that argument names appearing in {\bf Condition} are used to select
600in {\em both} {\bf UnionRel1} and {\bf UnionRel2}, therefore the
601{\bf Condition} can only refer to attributes of both relations.
602Actually the union is only included for syntactical completeness, a sequence
603of {\bf isr} and {\bf \verb-<++-} achieves the same effect.
604\begin{verbatim}
605ResultRel isr UnionRel1 where Condition
606ResultRel <++ UnionRel2 where Condition
607\end{verbatim}
608
609
610\subsection{Projection}
611\index{projection} \index{$:\verb-^-:$}
612The operations presented so far create relations with all the attributes
613of the relations on the right hand side of the {\bf isr} 
614predicate. We usually want our result relation to have only some of
615these attributes. The projection operator {\bf \verb-:^:-} acts as a filter
616on the relation created by the selections, joins etc. in an {\bf isr}
617call, and lets the result relation consist of only those attributes
618in a given list, called the {\em projection list}. The projection
619operator is used as follows:
620\begin{verbatim}
621Result_Relation isr Projection_List :^: Relational_Expression
622\end{verbatim}
623
624First a relation is constructed from the {\bf Relational\_Expression} as 
625described above (i.e. as if there 
626was no projection operator), and then the result relation 
627is produced by extracting from it the attributes listed in the projection list.
628The attributes in the result relation are in the same order as in the
629{\bf Projection\_List}. 
630We can adapt the example given in section \ref{joins} so that
631only the attributes of name and salary are put in the result relation
632\begin{verbatim}
633man_grade1 isr [name, salary] :^: 
634        employee :*: manager where employee_id == manager_id and salary > 200
635\end{verbatim}
636This creates the relation {\bf man\_grade1/2}, with a schema that satisfies 
637\begin{verbatim}
638man_grade1 <=> [atom(name,_,_), real(salary,_,_)].
639\end{verbatim}
640
641
642The {\bf \verb-^-} notation may also be used in a projection list if there
643is an ambiguity about which attributes are to be projected.
644
645
646\subsection{Adding to Relations}
647\label{insertion into rels}
648\index{relation - set insertions} \index{$<++$/2}
649Adding tuples to a relation is performed with the \verb-<++/2- predicate,
650which takes either a relational expression or a list of tuples and puts them
651in the specified relation.
652The relation is named on the left hand side of the operator and it
653must already exist. If the relation does not exist the goal will fail.
654
655\begin{verbatim}
656highpaid <++ [name, salary] :^: emp where salary > 20.
657
658employee <++  [ [5421, aaron, 3, 22],
659                [4529, schindler, 1, 30],
660                [8796, deuker, 4, 51] ].
661\end{verbatim}
662In the first example tuples are selected from a relation and projected
663into the target. The syntax allowed on the right hand side of the insertion
664operator is the same as the syntax used on the right hand side of the
665{\bf isr/2} predicate.
666
667The second example inserts tuples explicitly given in a list.
668Each tuple is in the form of a list of values enclosed 
669in square brackets. If any tuple does not match the schema in the 
670relation declaration, for instance does not have the correct
671number of attributes, an error exception is raised. In this case the remaining
672tuples in the list are not inserted.
673
674\subsection{Retrieval from Relations}
675\index{relation - set retrieval}
676The operator \verb-++>- \index{$++>$/2} retrieves a set of tuples
677as a Prolog list. 
678This is in a sense the inverse of the \verb-<++/2- predicate above.
679The set retrieval operator takes the form
680\begin{verbatim}
681Projection_List :^: Relational_Expression ++> List
682\end{verbatim}
683The syntax of {\bf Relational_Expression} is described above, the
684{\bf Projection_List} is optional. For example
685\begin{verbatim}
686?- employee where name \== schindler ++> X.
687
688X = [ [5421, aaron, 3, 22], [8796, deuker, 4, 51] ]
689\end{verbatim}
690This retrieval is much more efficient than retrieving tuple-at-a-time
691inside a {\bf findall/3} operator as in the next example.
692
693\begin{verbatim}
694?- findall([A,B,C,D],
695           (retr_tup(employee,[A,B,C,D]),B \== schindler),
696           X).
697X = [ [5421, aaron, 3, 22], [8796, deuker, 4, 51] ]
698\end{verbatim}
699Note that set retrieval is limited by the about of global stack
700that is available. If there are more tuples selected from
701the relation than fit into the main memory stack, a stack overflow
702will be signaled.
703
704\subsection{Deleting from Relations}
705\index{relation - set deletions}
706The operator \verb+<--+ \index{$<--$/2} performs the deletion of 
707tuples from a relation. 
708It may be used to delete tuples from a relation that are given 
709either by a relational expression or as an
710explicit list. For example
711\begin{verbatim}
712employee <-- emp2 where number > 5000.
713
714employee <-- [ [5421, aaron, 3, 22],
715               [4529, schindler, 1, 30] ].
716\end{verbatim}
717Again, as illustrated in the second case above, the right hand side
718of the deletion operator may be any expression that is legal for 
719the right hand side of  {\bf isr/2}.
720
721Some attributes may be left uninstantiated, as in the following
722example:
723
724\begin{verbatim}
725?- employee <-- [ [Number, eder, _] ].
726Number = Number
727	more? -- 
728
729yes
730\end{verbatim}
731
732The result of such a goal is to delete {\em all} the tuples 
733that have attribute values that match the instantiated values given
734in the goal.  Any variables in the goal
735are left {\em uninstantiated} and the goal cannot be resatisfied.
736So in the example all the tuples with 
737the second attribute equal to `eder' will be removed,
738and Number is returned as an uninstantiated variable.
739
740The \verb+<--+ predicate may  be used to empty a relation. 
741\begin{verbatim}
742r <-- r where true
743\end{verbatim}
744will delete all tuples from {\bf r} which are in {\bf r}, leaving the
745relation empty. Note that it is much more efficient to remove the relation
746and to create it again.
747
748\subsection{Aggregates}
749\index{aggregates}
750An attribute of an \eclipse database relation may have an aggregating
751property. This means that on each tuple insertion an aggregating
752operation is performed between the tuples in the relation and
753the new inserted tuple. There are four aggregates in \eclipse relations.
754
755\paragraph{min}
756This attribute will always hold the minimum value of all inserted
757tuples. This is possible on attributes of type real, integer and
758atom.
759
760\paragraph{max}
761This attribute will always hold the maximum value of all inserted
762tuples. This is possible on attributes of type real, integer and
763atom.
764
765\paragraph{sum}
766This attribute will always hold the sum of all inserted
767tuples. This is possible on attributes of type real and integer.
768
769\paragraph{count}
770This attribute will count the number of tuples inserted.
771This requires an integer type attribute.
772
773
774If a relation has {\em only} aggregating attributes it will
775contain at most one tuple. The more interesting case is when 
776some attributes are aggregating and others not. The
777non aggregating attributes are called grouping attributes, as
778they serve to compute the aggregates over several groups of tuples.
779
780
781In the example below there is an employee relation with three attributes.
782To compute the average salary of employees per department a temporary
783relation with two aggregating attributes is setup.
784The average is then simply computed by retrieving each tuple from
785the result relation and a simple division.
786
787\begin{verbatim}
788?- employee <=> Format.
789Format = [atom(name,30,+),
790          atom(department,30,+),
791          integer(salary,4,-)]
792
793?- R <=> [atom(department,30,+),
794          sum(integer(salary_sum,4,-)),
795	  count(integer(employees_per_department,4,-))],
796   R <++ [department, salary, salary] :^: employee.
797R = p456r2
798
799?- retr_tup(p456r2,[Dep,Sum,Count]),
800   Avg is Sum / Count.
801\end{verbatim}
802
803
804\section{Data Manipulation - Tuple-at-a-Time Operations}
805\index{tuple-at-a-time operations}
806The operations of section \ref{isr} are performed on relations, that
807is, upon whole sets of tuples. In a logic programming environment 
808we need to be able to extract single instantiations of a predicate and to
809use backtracking to find all the instantiations that satisfy a goal.
810This leads to a `tuple-at-a-time'
811approach, where the tuples of a relation are accessed one at a 
812time.  
813
814\subsection{Adding to Relations}
815\index{relation - tuple insertion}
816\index{ins_tup/1}
817The predicates {\bf ins\_tup/1} and {\bf ins\_tup/2} insert single
818tuples into a relation. In the first form of the predicate the
819tuple is viewed as a Prolog structure; in the second, as a list
820of attribute values.
821\begin{verbatim}
822ins_tup( employee(8282, mann, 1, 43) ),
823ins_tup( employee, [2324, doerr, 3, 22]).
824\end{verbatim}
825In both cases a single tuple is added to the employee relation,
826which has four attributes. The next example shows the insertion
827of values into a relation with an attribute of type term:
828\begin{verbatim}
829data <=> [ integer(id,2,+), term(info,_,X) ],
830
831ins_tup( data(1, smith(male,single,young)) ),
832ins_tup( data(2, (young(Y) :- Y =< 35 )) ).
833\end{verbatim}
834    
835\subsection{Retrieving Data}
836\index{relation - tuple retrieval}
837\index{retr_tup/2}
838We may view a database relation as a definition of a predicate,
839in which case a retrieval of values from the database may be seen
840as the satisfaction of a goal. The predicate {\bf retr\_\/tup/2} 
841is used to express this goal. The first argument names the relation from which
842tuples are to be taken. This name may be the relation name occurring
843in the database, or a synonym from a frame declaration. 
844The second argument is a list of length equal 
845to the arity of the relation. The elements of this list are matched
846against the tuples of the relation. Thus
847\begin{verbatim}
848?- retr_tup(employee, [Number, Name, Dept, Salary]).
849Number = 5426
850Name = 'acher'
851Dept = 2
852Salary = 19
853        more? -- ;
854Number = 4342
855Name = 'adolf'
856Dept = 4
857Salary = 34
858        more? -- 
859
860yes
861
862?- retr_tup(data, [2, (young(20) :- Body)]), call(Body).
863Body = 20 =< 35)
864	more? -- 
865
866yes
867\end{verbatim}
868The second example is an extension of the example in the previous section, 
869and shows how Prolog clauses can be saved in the database and then
870retrieved and executed.
871
872The predicate {\bf retr\_\/tup} is resatisfiable. Successive calls 
873to {\bf retr\_\/tup} through backtracking will retrieve successive tuples 
874from the relation in question.
875
876If the arity of the relation is unknown, it is also possible to ask
877\begin{verbatim}
878?- retr_tup(employee, Tuple).
879Tuple = [5426, 'acher', 2, 19]
880	more? -- 
881
882yes
883\end{verbatim}
884thus returning the tuple as a list.
885
886
887A variant is {\bf retr\_tup/1}. This views the tuples of the relation as 
888Prolog structures, for example
889\begin{verbatim}
890?- retr_tup(employee(A,B,C,D)).
891A = 5426
892B = 'acher'
893C = 2
894D = 19
895	more? -- 
896
897
898yes
899\end{verbatim}
900
901A third variant {\bf retr\_tup/3} allows the inclusion
902of a condition restricting the tuples considered to a 
903subset of the relation. 
904\begin{verbatim}
905retr_tup(employee, [A, B, C, D], salary > 3001.20).
906\end{verbatim}
907The condition in the third argument is any condition that
908is valid in an {\bf isr} clause behind the {\bf where}.
909
910An equality retrieval condition can either be given as third argument
911to {\bf retr\_tup/3} or as instantiation of the second argument.
912\begin{verbatim}
913retr_tup(employee, [A, acher, C, D]).
914retr_tup(employee, [A, B, C, D], name == acher).
915\end{verbatim}
916These two forms are identical, both in semantics and in the performance
917of execution. However, there is one exception. 
918The wild characters `\verb-*-' and `\verb-?-' are interpreted only in
919the second form. In the first form wild characters are taken as
920normal characters.
921
922Both tuple access and set oriented operations can be used to perform
923the same operations. Below we illustrate this duality with an example
924\begin{verbatim}
925tmp isr [a1, b3] :^: a :*: b where a2 == b1,
926result isr [a1, c1] :^: tmp :*: c where b3 == c2.
927\end{verbatim}
928is set oriented, while
929\begin{verbatim}
930retr_tup(a(A1,A2,_)), 
931retr_tup(b(A2,_,B3)), 
932retr_tup(c(C1,B3)).
933\end{verbatim}
934is tuple-at-a-time.
935
936In most cases the set oriented operations are more efficient, because
937the database can do more intelligent join than simple nested loops.
938On the other hand the tuple access allows an easy interference with
939further Prolog goals, that can express more complicated conditions.
940The set oriented example needs space for the intermediate relations
941{\bf tmp} and {\bf result}. If these relations are very big, a
942space-time tradeoff may favour spending more time by using tuple
943access that does not need these intermediate relations.
944
945To make the retrieval of tuples from the database transparent (i.e. so there
946is no difference between main-memory and database access of unit clauses) 
947a predicate can be defined as follows:
948\index{database transparency}
949\begin{verbatim}
950p(X,Y) :- retr_tup(p(X,Y)).
951\end{verbatim}
952
953Then a tuple can be retrieved from the relation {\bf p/2} with the 
954following form of goal instead of having to use the {\bf retr\_tup/1}
955predicate each time:
956
957\begin{verbatim}
958?- p(X,a).
959\end{verbatim}
960
961    
962\subsection{Deleting from Relations}
963\index{relation - tuple deletion}
964\index{del_tup/1}
965\index{del_tup/2}
966\index{del_tup/3}
967The predicates {\bf del\_tup/1} and {\bf del\_tup/2} delete single
968tuples from a relation. In the first form of the predicate the
969tuple is viewed as a Prolog structure; in the second, as a list
970of attribute values.  
971\begin{verbatim}
972del_tup(employee(4576, hainz, 2, 23)),
973del_tup(employee,  [9939, N, _, _]).        
974N = 'beck'
975        more? -- 
976
977yes
978\end{verbatim}
979There exists also a predicates {\bf del\_tup/3}, which is like 
980{\bf del\_tup/2} but takes a condition as third argument.
981
982An attribute can be left uninstantiated in the goal, as in the above example,
983and when a matching tuple is found the variable will become bound.
984Backtracking can then be invoked to delete other tuples from the database
985that match the goal.
986