Z Language:

1. Relational Database

1.1. Dissimilarity from SQL

The main merit of Z is its conciseness and ease. Z language makes possible to work with relational database that features multiple values of field and dynamic structure (schema independence data). Moreover, attribute (column) name may absent or be multitude in the query.

1.2. Declaring database

Considered structure:
$base B - B database.

$base B ~ - B database unequal anything (is empty).

Left sentences are Z statements, right sentences - literal translation. The B name of the current database may be missing. Database type is specified by base key word immediately after $.



For example, if you have Sample directory.
< Tests multibase working >
$base ~ ;       < Clears current base >
< Z statements >;
...
$base "Sample/OldBase";
< Z statements >;
...
$base "Sample/NewBase" ~ ;
< Z statements >;
...
The < ... > construction is a comment in Z.

1.3. Creating table relations

Considered description structure:
R: r (A: a, …) - a R class namely r relation of an A class namely a, … .

Considered auxiliary structure:
{a1, …} - a set, a1, … .

For example A: { a1, a2 }, or equal {A: a1, A: a2}.
S* . S denotes sequence of non key symbols.
For example a* in place of { a1, a2, ... an }.


Look at the table with the multiple valued fields.
note  workfellow      telephone     address
#1    Ringer Michael  415 506-0111  4 Broadway Av
                      503 743-5226
#2    Smith Anne      400 297-0752  10 Seventh Av
      Smith Robert
Let's create Ex/Interpreter/RelDB database.
$base "Ex/Interpreter/RelDB" ~;
Now input the table information. Description of schema, first statement, is optional!!!. Z DBMS deduces schema automatically.
note (workfellow, telephone, address);
note: #1 (workfellow: Ringer Michael,
        telephone: { 415 506-0111, 503 743-5226 },
        address: 4 Broadway Av);
note: #2 (w*: Smith Anne, w*: Smith Robert,
        t*: 400 297-0752,
        a*: 10 Seventh Av);

1.4. Selecting table relations

Considered query structure:
= … - anything equal to the … (or the ...).
For example:
= R: -  anything equal to the R class;
= R: (A: a) - the R class namely relation of the A: a ;
= (A: a, B: b) - the relation of the A: a and B: b ;
= ( { A: a, B: b } ) - the relation of the A: a or B: b .
= { … =/~/, … } - the set of the ... and/no/or the ... .
For example: = { R: ~ (B: ) } - anything of the R class no the relation of the B class.
= A: a ^ b - the A class namely range from a to b.

Z has unique features: class name may be absent or retrieved by the query. For example = : a denotes all the a of arbitrary class. The order of the class name and class value is not crucial in the query that begins with ':' symbol, for example = : a: A in place of = A: a.



Following query reports some concepts that belong to the tel* class with values in the range  from 415* (see 1.3. Creating...).
= tel*: 415* ^;
Result in the Report window:
telephone ( note
          ):{ 415 506-0111 ( note:#1
                           ),
              503 743-5226 ( note:#1
                           )
            };
We can image relations with 4* address and with one of the 4* telephones. Attribute name, tel*, put after attribute value, for testing.
= (addr*: 4*, : 4*: tel*);
Result:
note ( address,
       telephone,
       workfellow
     ):#1 ( address:4 Broadway Av,
            telephone:{ 415 506-0111,
                        503 743-5226
                      },
            workfellow:Ringer Michael
          );
Finally output all the notes that have relation with 4* telephone, but no relation with 503* instance.
= { note: (tel*: 4*) ~ (: 503*) };

-----
note ( address,
       telephone,
       workfellow
     ):#2 ( address:10 Seventh Av,
            telephone:400 297-0752,
            workfellow:{ Smith Anne,
                         Smith Robert
                       }
          );


Z operators, ordered from highest to lowest precedence, are below. Operators of the same precedence are executed from left to the right.
 
A   B
A ^ B
A : B
A / B
  = A
A ~ B
A = B
A , B
A ; B

All the complete expressions, separated by the ';', are statements. Expression may be description, query, or procedure. If Z expression begins with = symbol, this expression is a query. For example: = Q is a query, whereas D (D1) is description. Description inside of the query should be ended with =. For example: = { D = }. For the ~ operator the operands are queries, Q ~ Q1 , Q ~ .

1.5. Printing database contents

Considered procedure structure:
$printX($file "F", Q, …) – print X of the Q, … in F.

We can use: print, printLine, printRelation, printClass, printName, or a contraction thereof p, pl, pr, pc, pn. The F is an output file. Missing of the $file parameter denotes output in the Report window. The Q, … are queries or "…" strings. If Q parameters are absent in printRelation, printClass, or printName it assumes printing all the database's relations, classes, or names correspondingly.



Try to print all the "schema relations" (see 1.3. Creating...).

$p("SCHEMA OF ");
$p(*);
$pl();
$pl("------------------------------------------------------");
$pr(*);

SCHEMA OF address, note, telephone, workfellow
------------------------------------------------------
address ( note
        ),
note ( address,
       telephone,
       workfellow
     ),
telephone ( note
          ),
workfellow ( note
           );
The $Content: service class is useful. It is formed by the system itself and can help to print the "head relations".
$pl("CONTENT OF RelDB DATABASE");
$pl("------------------------------------------------------");
$pr($Content: );
The fragment produces in Report window the following result.
CONTENT OF RelDB DATABASE
------------------------------------------------------
$Content:note ( address,
                telephone,
                workfellow
              );
If you want to print contents of the workfellow class relating with the notes of the 415* telephone in the Ex/Interpreter/wf415.txt, type
$pc($file "Ex/Interpreter/wf415.txt",
        workf*: (note: (telephone: 415*) ) );
If you need save all the database's names in the Ex/Interpreter/names.txt, type
$pn($file "Ex/Interpreter/names.txt");

1.6. Updating relation

Considered structure:
{ = ... } (A: a, …) - a set, any equal to the …, is a relation of an A class a and ….

{ … } ~ (A: a, …) - a set, the …, is not equal to relation of the A class a and …. The {…} (~ …) is not correct!

R: #N - R class namely N number. Here N is positive number, 1073741823 maximum by default.
Look at the R: # expression. The indefinite number is last number if the R: # query, and is new number if the R: # description.

Notice that R = : R1 construction is the same as R1, R: R1 sequence, R ~ : R1 is the same as R: R1 ~. If value of the = : r query is not available, the : r = description equals r. The R: = description entails R if the = R: query is not available.



Suppose that Smith couple split. Anne lives in another place. (See 1.3. Creating...).
note: (: Smith*) ~ (: Smith Anne);
note: # (: Smith Anne);
{ = note: # } (t*: 503 743-5226);

Take a look at the result using
= (: Smith*);
note ( address,
       telephone,
       workfellow
     ):{ #2 ( address:10 Seventh Av,
              telephone:400 297-0752,
              workfellow:Smith Robert
            ),
         #3 ( telephone:503 743-5226,
              workfellow:Smith Anne
            )
       };

1.7. Coping and deleting relation

Considered structure:
R: { r0 =/~ r1 }R class r0 is equal/unequal to R class r1. Expression might transform to R: r0 =/~ R: r1. It copies relations from R: r1 to R: r0 or deletes relations of R: r1 in R: r0.

R: r ~ (  ) - R class namely r is not relation. Deletes relations.

R: r ~ - R class namely r is not. Deletes concepts.



We have new sample information. Anne Smith is daughter of Michael Ringer. Michael died and Anne has father's address.
$pr(note: #3);
note: { =#3 = =(: Ringer*) };
$pr(note: #3);
: Ringer* ~;
$pr(note: #3);
note: #1 ~ ( );
$pr(note: );
note:#3 ( telephone:503 743-5226,
          workfellow:Smith Anne
        )
note:#3 ( address:4 Broadway Av,
          telephone:{ 415 506-0111,
                      503 743-5226
                    },
          workfellow:{ Ringer Michael,
                       Smith Anne
                     }
        );
note:#3 ( address:4 Broadway Av,
          telephone:{ 415 506-0111,
                      503 743-5226
                    },
          workfellow:Smith Anne
        );
note:{ #1,
       #2 ( address:10 Seventh Av,
            telephone:400 297-0752,
            workfellow:Smith Robert
          ),
       #3 ( address:4 Broadway Av,
            telephone:{ 415 506-0111,
                        503 743-5226
                      },
            workfellow:Smith Anne
          )
     };

1.8. Exporting and importing table data

Considered structure:
$printTable($file "F0", R0: …, A0, A1, …) - print table in F0 file from R0 class … with column names A0 key, A1, …).

$readTable($file "F1", R1, N0, N1, …) - read table from F1 file in R1 relation with column numbers N0 key, N1, …).

If F0 file parameter is missing Report window is suggested. R1 is name description of a class relation. The A0, A1, … may be queries. The procedures has name contractions pt for printTable, rt for readTable. If the A0, A1, … are absent, all the attributes, the R0 has, will be output in alphabet order. If there are not N0, N1, …, all the columns will be input.

A first input line of F1 file must define the names of columns. Delimiter of the table fields may be only semicolon ";". Values of the field are separated by the ','. The words with the $ * # " ' { [ ( } ] ) ^ : / \ ~ = , ; < > key symbols need be enclosed in the quotes. The price may be without quotes, $10.2. Distinction of the "..." and '...' see in the next section.



Consider creating workfellow (address, telephone) class relation in Ex/Interpreter/ObjDB.ass database. Need output relations note (address, telephone, workfellow) in a Ex/Interpreter/Note.tab file, then input so that #4 column would be key.
$pt($file "Ex/Interpreter/Note.tab", note:);
$base "Ex/Interpreter/ObjDB" ~;
$rt($file "Ex/Interpreter/Note.tab", #4, #2, #3);
$pt($file "Ex/Interpreter/Workfellow.tab", workfellow: );
$pt(workfellow:, workfellow, telephone, address);
workfellow; telephone; address
Smith Anne; 415 506-0111, 503 743-5226; 4 Broadway Av
Smith Robert; 400 297-0752; 10 Seventh Av

1.9. Quoting and numbering

Considered structure:
R: 'S'
R: "S"
R: #N

Here S is any sequence of symbols. Notice that "S" name and #N number may end up space combination, for example R "S", whereas "S" R and "S" "S1" are not correct. System tests existing "S" and #N concepts in a database only when need test its connection or equality. For example "S" will be tested if "S": or = "S" , but not if { "S" } or $print("S") .

Single quotes may be helpful only for using special symbols, for instance R: '*'. Using R: "S", as R: #N, reduces the required size and accelerates running, but a price must be paid in loss of the contractions, :"S", : #N . It may be fit for a big database.


$base "Ex/Interpreter/Test" ~;
office: '#10' (employer: #1, company: "Unknown");
= office: ''*;
= employer: #*;
= company: "U"*;
= { "Attainable" };
= : '#10';
Derivable result:
office ( company,
         employer
       ):'#10' ( company:"Unknown",
                 employer:#1
               );
employer ( office
         ):#1 ( office:'#10'
              );
company ( office
        ):"Unknown" ( office:'#10'
                    );
"Attainable";
office ( company,
         employer
       ):'#10' ( company:"Unknown",
                 employer:#1
               );
Following fragment produces Failure messages.
= "Unattainable";
= : #1;
= : "Unknown";