<<

Rel is an open source desktop management system from Dave Voorhis that implements Relational Expressions Relvars + Tutorial D Tutorial D, a relational database language designed by Chris Date and . // Return value of relvar S Tutorial D is not SQL. VAR myVariable REAL RELATION {x INT, y RATIONAL, z CHAR} KEY {x}; For more information, see https://reldb.org, http://thethirdmanifesto.com and S http://www.dcs.warwick.ac.uk/~hugh/TTM/Tutorial%20D%202016-09-22.pdf // Join S and P on common attributes Start Scalar Expressions Tuple Expressions S JOIN P INSERT myVariable RELATION {

// Return tuples of S that match tuples in P, TUPLE {x 1, y 2.3, z 'zap'}, Launching: Download instructions are at 3 + 4 TUPLE {x 1, y 2.3, z 'zap'} // based on common attributes https://reldb.org/c/index.php/download/ Once 7 x 1 y 2.3 z zap TUPLE {x 2, y 3.4, z 'zot'}, downloaded, open the folder – or go to S MATCHING P Applications on macOS – and run the Rel 3.4 + 5.6 TUPLE {x 3, y 4.2, z 'zaz'} executable. 9.0 TUPLE {x 1, y 2.3, z 'zap'} JOIN TUPLE {p 1, q 4.3, r true} // Return tuples of S that do not match tuples in P, Rel command-line: In the upper-right hand x 1 y 2.3 z zap p 1 q 4.3 r true // based on common attributes }; corner of the Rel window, there are these three 3.4 > 5.6 S NOT MATCHING P false icons. TUPLE {x 1, y 2.3, z 'zap'} MINUS TUPLE {p 1, q 4.3, r true} The left icon is for the main Rel user interface, // Join S and P on common attributes; x 1 y 2.3 z zap myVariable the middle icon is the visual query editor, and 1.2 < 3.4 // do not include common attributes S COMPOSE P the right icon is the command-line. true x y z Loading a database script: Go to the command- TUPLE {x 1, y 2.3, z 'zap'} MINUS TUPLE {x 1, y 2.3} // Return tuples of S where STATUS is greater than 10 line, select the Load File icon, load the file. Press "a" || "bcd" z zap INTEGER RATIONAL CHARACTER F5 to execute. abcd S WHERE STATUS > 10 Evaluating expressions and statements: Type 1 2.3 zap TUPLE {x 1, y 2.3, z 'zap'} UNION TUPLE {x 1, y 2.3} the expression at the command-line and press 'a' || 'bcd' // Return tuples of S where SNAME equals F5. Statements always end with a semicolon; abcd x 1 y 2.3 z zap // NAME(‘Smith’). NAME is a user-defined type. 2 3.4 zot expressions do not. S WHERE SNAME = NAME('Smith') SIN(0.25) TUPLE {x 1, y 2.3, z 'zap'} UNION TUPLE {x 1, y 2.3, r 4.5} 3 4.2 zaz NOTE: To run the examples on this page, download and unzip // Return UNION of tuples of S where SNAME equals Rel_ExamplesAndUtilities_3.xxx.zip, load and execute database 0.24740395925452294 x 1 y 2.3 z zap r 4.5 script DateBookSampleRelvars.rel // NAME(‘Smith’) with tuples // of S where STATUS equals 30. (S WHERE SNAME = NAME('Smith')) UNION (S WHERE STATUS = UPDATE myVariable WHERE x > 2: {y := y + 4.2, z := z || 'gurgle'}; User-defined operators and types Flow control 30)

// This is a user-defined operator // IF ... THEN statement // Return tuples of S with S#, SNAME and STATUS IF RANDOM() > 0.5 THEN // attributes converted to a relation-valued attribute X. myVariable OPERATOR myOperator (x INTEGER, y INTEGER) RETURNS INTEGER; WRITELN "heads"; S GROUP {S#, SNAME, STATUS} AS X ELSE x y z RETURN x + y * 2; WRITELN "tails"; // Return tuples of S with S#, SNAME and STATUS INTEGER RATIONAL CHARACTER END OPERATOR; END IF; // attributes converted to a tuple-valued attribute X. S WRAP {S#, SNAME, STATUS} AS X // IF ... THEN expression 1 2.3 zap WRITELN // Get the single tuple from S WHERE STATUS = 10. IF RANDOM() > 0.5 THEN "heads" 2 3.4 zot // Evaluate // Error if there isn’t exactly 1 tuple. ELSE "tails" END IF; myOperator(3, 4) * 2 TUPLE FROM (S WHERE STATUS = 10) 3 8.4 zazgurgle // CASE ... WHEN statement VAR x INIT(RANDOM()); // Get the SNAME attribute from the tuple from // S WHERE STATUS = 10. Error if there isn’t 1. // Execute CASE; WHEN x > 0.5 THEN WRITELN "heads"; SNAME FROM TUPLE FROM (S WHERE STATUS = 10) DELETE myVariable WHERE x = 1; CALL myOperator(3, 4); WHEN x < 0.5 THEN WRITELN "tails"; ELSE WRITELN "on edge"; // Project S on SNAME and STATUS. END CASE; S {SNAME, STATUS} myVariable // Drop operator // CASE ... WHEN expression x y z DROP OPERATOR myOperator(INTEGER, INTEGER); VAR y INIT(RANDOM()); // Return value of S with SNAME renamed to NAME; WRITELN CASE // STATUS renamed to STAT. INTEGER RATIONAL CHARACTER WHEN y > 0.5 THEN "heads" S RENAME {SNAME AS NAME, STATUS AS STAT} WHEN y < 0.5 THEN "tails" ELSE "on edge" 2 3.4 zot // User-defined type // Return the scalar sum of the STATUS attribute of S. END CASE; TYPE myNewTYPE POSSREP {x INT, y CHAR}; SUM(S, STATUS) 3 8.4 zazgurgle // WITH expression WRITELN WITH ( // Return the scalar sum of an expression. SUM(S, STATUS * 2) // Value of type myNewTYPE v := 2.0 * SIN(RANDOM()), q := 3.0 * TAN(RANDOM()) // Describe all relvars in the database myNewTYPE(2, 'zot') ): v * v * q + q; // Obtain total of STATUS attribute grouped by CITY SUMMARIZE S BY {CITY}: {TOTAL := SUM(STATUS)} sys.Catalog // DO loop VAR i INT; // Obtain total of STATUS attribute, and count of tuples, // Relvar using user-defined type DO i := 1 TO 10; // grouped by CITY // Get the names of all relvars in the database VAR myNewRelvar REAL RELATION {x INT, y myNewTYPE} KEY {x}; WRITELN i; SUMMARIZE S BY {CITY}: END DO; {N := COUNT(), TOTAL := SUM(STATUS)} sys.Catalog {Name}

// WHILE loop // Obtain total of STATUS attribute times two, VAR j INIT(10); // and count of tuples, grouped by CITY WHILE j > 0; SUMMARIZE S BY {CITY}: // Get all the operators in the database WRITELN j; j := j - 1; {N := COUNT(), TOTAL := SUM(STATUS * 2)} sys.Operators END WHILE; // Calculate new attribute values from expressions. sys.OperatorsBuiltin EXTEND S: {BIGSTATUS := STATUS * 10, R := 'Test'}