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 (scheme-independence data). Moreover, attribute (column) name may absent or be multitude.

1.2. Declaring database

Considered structure:
$base B - database B.
$base B ~ - database B 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 statement>;
...
$base "Sample/OldBase";
< Z statement >;
...
$base "Sample/NewBase" ~ ;
< Z statement >;
...

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 R: r (A: { a1, a2 }) in place of R: r (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      415 297-0752  10 Seventh Av
      Smith Robert

Let's create Ex/Interpreter/RelDB database (Ex/Interpreter/RelDB for UNIX).

$base "Ex/Interpreter/RelDB" ~;

Now input the table information. First statement, description of scheme, is optional!!!. Z 1.2 DBMS deduces scheme 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*: 415 297-0752,
	a*: 10 Seventh Av);

1.4. Selecting table relations

Considered query structure:
(…) - any relation of the … .
For example: (A: a, B: b) - anything related with A: a and B: b ; ({ A: a, B: b }) - anything related with A: a or B: b .
= … - any equality of the … .
For example: = R: - anything of the R class; = R: (A: a) - anything of the R class and related with A: a.
= … =/~ … - any equality of the … and equality/inequality of the … .
For example: = R: ~ (B: ) - anything of the R class and unrelated with B class.

Z has unique features: class name may be absent or retrieved by 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.

Following query reports some concepts that belong to indefinite class and begin with 4 (see 1.3. Creating...).

= : 4*;

Result in Report window:

{ address: 4 Broadway Av (note: #3
                         ),
  telephone: { 415 297-0752 (note: #2
                            ),
               415 506-0111 (note: #3
                            )
             }
};

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: #1 (address: 4 Broadway Av,
          telephone: 415 506-0111,
          telephone: 503 743-5226,
          workfellow: Ringer Michael
         );

Finally output all the notes that have relation with 4* telephone, but no relation with 503*.

= note: (tel*: 4*) ~ (: 503*);

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

Z operators, ordered from highest to lowest precedence, are below. Operators of the same precedence are executed from left to right.

<Space>
:
=  ~
,
;

All the complete expressions, separated by ';', are statements. Any expression may be description, query, or procedure. If Z expression begins with =/~ symbol or is in parentheses with no naming expression before, this expression is query. For example: = Q or (Q) is query, whereas D (D1) is description. Description inside of the query should be ended with =/~. For example: = {D =} or (D =). It may be useful to mind structures D :/=/~ (Q), D =/~ D1, = { Q =/~ Q1 }.

1.5. Printing database contents

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

We can use in place of the X words: Line, Relation, Class, Name, or a contraction thereof with *, or nothing. The F is an output file. Empty F parameter denotes Report window. The Q, … are queries or "…" strings. If Q parameters are absent in printRelation, printClass or printName, it assumes printing names of relations, classes or all the database's names correspondingly.

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

$printR*();

note (address,
      telephone,
      workfellow,
      $Contents
     ),
address (note
        ),
telephone (note
          ),
workfellow (note
           ),
$Contents (note
           );

The $Contents is service word. It, as scheme relations, is deduced by system itself and can help to print names of the head relations.

$print(, "DATABASE CONTENTS: ", ($Contents) );
$printL*();
$printL*(, "-----------------------------------------------");

It produces in Report window next result.

DATABASE CONTENTS: note
-----------------------------------------------

You can image all the "class relations".

$printR*(, ($Contents): );

If you want to print contents of the workfellow class relating to the note of 415* telephone in the Ex/Interpreter/wf415.txt, type

$printC*("Ex/Interpreter/wf415.txt",
         workf*: (note: (telephone: 415*) ) );

If you need save all the database names in the Ex/Interpreter/names.txt, need type

$printN*("Ex/Interpreter/names.txt");

1.6. Updating relation

Considered structure:
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.
{ = R: r, … } (A: a, …) - a set, any equal to the R class r …, is a relation of an A class a and ….
{ = R: r, …} = (A: a, …) - a set, any equal to the R class r …, is equal to relation of the A class a and …. It has the same meaning as { =R: r, … } (=A: a, …).
{ R: r, … } ~ (A: a, …) - a set, the R class r or …, is not equal to relation of the A class a and …. The { R: r, …} (~A: a, …) is not correct!

Notice that if value of = : r query is not available, the : r description equals r. If value of = : r is R: r, than : r description equals R: r . The R: description entails R if the = R: query is not available, and entails R: r if the = R: is R: r .

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: { #2 (address: 10 Seventh Av,
            telephone: 415 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 is a set, r0 equaling/unequaling to 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. We can use R: {r0 =/~ {r1, r2}} in place of
R: r0 =/~ R: r1 =/~ R: r2 .
R: r ~ - R class namely r is not equal to any. Deletes relations.
~ R: r - any is not equal R class namely r. Deletes concepts with relations.

We have new sample information. Anne Smith is daughter of Michael Ringer. Michael died and Anne has father's address (see 1.6. Updating...).

note: { (: Smith Anne) = { (: Ringer*) } };
{ = note: (: Ringer*) ~ (: Smith A*) } ~ ;
~ : Ringer*;
= note: ;
note (address,
      telephone,
      workfellow,
      $Contents
     ): { #1,
          #2 (address: 10 Seventh Av,
              telephone: 415 297-0752,
              workfellow: Smith Robert
             ),
          #3 (address: 4 Broadway Av,
              telephone: 415 506-0111,
              telephone: 503 743-5226,
              workfellow: Smith Anne
             )
        };

1.8. Exporting and importing table data

Considered structure:
$printTable(F0, R0: …, A0, A1, …) - print table in F0 file from R0 class … relation with structure: A0 key name (A1 name, …).
$readTable(F1, R1, N0, N1, …) - read table from F1 file in R1 relation with structure: N0 key number (N1 number, …).

The F0, F1 and D are given by the "…" string. F0 file is Report window by default. R1 is a name description. The R0: … , A0, A1, … are queries. The N0, N1, … are column numbers.

If the A0, A1, … are absent, all the attributes, the R0 has, will be output in alphabet order. If the A0 is missing, R0 meanings will be not output. If there are not N0, N1, …, all the columns will be input. If the N0 is missing, each new R1 meaning will a new number for each input line of F1. A first input line of F1 must define the names of input attributes. Delimiter of fields in F0 and F1 may be only semicolon ";".

Consider creating workfellow (telephone, address) class relation in Ex/Interpreter/ObjDB.ass database. Need output relations with all note (address, telephone, workfellow) (see 1.7. Coping...) attributes in a Ex/Interpreter/Note.tab file, then input so that #4 column would be key.

$printT*("Ex/Interpreter/Note.tab", note:);
$base "Ex/Interpreter/ObjDB" ~;
$readT*("Ex/Interpreter/Note.tab", workfellow, #4, #2, #3);
$printT*("Ex/Interpreter/Workfellow.tab", workf*:);
$printT*(, workf*:, workf*, tel*, addr*);
workfellow; telephone; address
Smith Anne; 415 506-0111; 4 Broadway Av
Smith Anne; 503 743-5226; 4 Broadway Av
Smith Robert; 415 297-0752; 10 Seventh Av

1.9. Quoting and numbering

Considered structure:
R: 'S'
R: "S"
R: #N
. (See 1.6. Updating ...).

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 in "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: room': # (company: "Unknown");
off*: # (comp*: "U"*);
= { "Attainable" };
= comp*: "U"*;
= off*: #*;

Derivable result:

"Attainable";
company: "Unknown" (office: room: #1,
                    office: room: #2
                   );
office: room: { #1 (company: "Unknown"
                   ),
                #2 (company: "Unknown"
                   )
              };

Following fragment produces Failure messages.

= "Unattainable";
= : "U"*;
= : #*;