# SQL Syntax of the Txt-Db-Api (Limitations)

##

## Abstract

The Txt-Db-Api does not support all SQL keywords/statements nor the SQL Standard.
On this Page the sql syntax for the txt-db-api and its limitations are listed.
Everything in [] is optional, "..." means that any more elements can follow,
| means OR (AND|OR => you can put AND or OR at this place).

As in Standard SQL: Do always enclose values for str (String) fields in quotes (' ' or " ") !

## SELECT

### Syntax

    SELECT [DISTINCT]
    [function(][table_name.]column[)] [AS alias_name] [, [function(][table_name].column[)] [AS alias_name] , ...]
    FROM tablex [AS table_alias] [, tabley [AS table_alias], ... ]
    [[LEFT|RIGHT|INNER] [OUTER] JOIN [table] ON table2, ... ]
    [WHERE column=value AND|OR column=column AND|OR ...]
    [GROUP BY column, ...]
    [ORDER BY column [ASC | DESC] ,...]
    [LIMIT [offset,]rows [;]

### Supported

- look at the syntax...

- Instead of column names (column in the syntax above) you may also use direct values.
This can be numbers or strings in ' ' or " ", for example SELECT 1234, 'bla', name FROM people;

- The following functions: UNIX_TIMESTAMP(), MD5(column), NOW(), ABS(column), LCASE(column), UCASE(column), LOWER(column), UPPER(column), EVAL(column *-+/ value|column...)
- The functions in the line above are also supported in the WHERE, ORDER BY and GROUP BY part.
- The following grouping functions: MAX(column), MIN(column), COUNT(column), SUM(column), AVG(column)
- You can also use direct values as parameters for the functions, instead of columns, for example md5('bla')

- * instead of field names
- table.*, combinations are also allowed, for example: SELECT table1.*, people.Name, table3.* ...
- SELECT COUNT([table.]*)

- Aliases for Tables

- In the WHERE part, the following operators are supported: =,>,<,>=,<=, !=, <>, LIKE / NOT LIKE (you can use % or _ as wildcards), IN / NOT IN
- In the WHERE part, you can use field=value, value=value or field=field conditions
(to query tables linked, e.g. "person.typ_id = types.id", for field=field conditions all the above
listed operators can be used)
- In the WHERE part you can use countless AND's and OR's
- In the WHERE part brackets can be used, and also be nested

- JOIN's (INNER, LEFT [OUTER] and RIGHT [OUTER])
- DISTINCT: only unique rows are returned
- GROUP BY: the result will be grouped by this columns
- LIMIT: only rows Rows are returned, starting with offset (The first Row has offset 0!)
- When using DISTINCT and GROUP BY always the first value is taken
Example: "SELECT time, ip FROM log GROUP BY time"

time | ip 10348395 | 192.168.0.1 10348395 | 192.168.0.3 | > | time | ip 10348395 | 192.168.0.1

### Not supported

- Appending of Fields like SELECT name & " " & prename AS whole_name or
SELECT name + prename AS whole_name
- Everything not found under Syntax or supported !

### Examples

SELECT prename, person.name, types.typ AS person_typ FROM person, types
WHERE person.typ_id = types.id AND (person.Nr>=10 OR person.prename='Hans')
ORDER BY prename

SELECT COUNT(m.name), AVG(m.age) AS avgAge, c.name FROM members AS m, course AS c
WHERE m.course_nr=course.id GROUP BY c.name;

SELECT md5('bla');

### Tip's

ATTENTION: Before and after each OR or AND must be a Space (" "). This is
requied for the parser, that can distinguish between field names containing
OR or AND as part of it, or real OR or AND's. The same applies for LIKE.
Examples:
Wrong: Name='Test'OR Nr>20
Right: Name='Test' OR Nr>20

ATTENTION: Use LIKE only if it's really necessary. Normal String compares are much
faster then LIKE. For example, if you want to query all records where the name is exactly
'tom', do it like this: SELECT * FROM table WHERE name='tom'
AND NOT: SELECT * FROM table WHERE name LIKE 'tom'
AND ALSO NOT: SELECT * FROM table WHERE name LIKE '%tom%'

## INSERT

### Syntax

    INSERT [INTO] table [(column1, column2, column_n, ...)]
    VALUES  (  [function(] value [)] [, [function(] value2 [)], value_n, ...]) [;]
    	or
    INSERT [INTO] table
    SET column=[function(]value[)] [,column2= [function(]value2[)], column_n= [function(] value_n [)] , ...] [;]

### Supported

- Look at syntax
- The following functions: UNIX_TIMESTAMP(), MD5(value), NOW(), ABS(value), LCASE(value), UCASE(value), LOWER(value), UPPER(value), EVAL(column *-+/ value|column...)
- Only values can be used as parameters for the functions in INSERT (an NOT columns)

### Not supported

- INSERT with SELECT
- Insert more then one Record per SQL Statement
- Grouping functions MAX(), MIN() etc.
- Everything not under Syntax

### Examples

INSERT INTO person (prename, name) VALUES ('hans','meier');

INSERT INTO passwords (user, password) VALUES ( 'user1', md5('mypassword') );

INSERT INTO person SET prename=upper('hans'), age=abs(-55), added=NOW() ;

## DELETE

### Syntax

    DELETE FROM table [WHERE <same as in SELECT>] [;]

###
Supported

- Look at Syntax
- Deleting the whole Table (drop the WHERE part, for example: "DELETE FROM tableXY;")
- Same WHERE Syntax as in SELECT (functions are also supported!)

### Not supported

- LIMIT
- Same restrictions for the WHERE part as by SELECT
- Everything not found in the Syntax above..

### Examples

DELETE FROM person WHERE prename='Hans';

DELETE FROM person WHERE password=md5('mypw') AND abs(age)=abs(-5);

## UPDATE

### Syntax

    UPDATE table SET column=[function(]value[)] [,column2=[function(]value2[)], column_n=[function(]value_n[)], ...]
    [WHERE <same as in SELECT>] [;]

###
Supported

- Look at Syntax..
- The following functions for the values: UNIX_TIMESTAMP(), MD5(column or value), NOW(), ABS(column or value), LCASE(column or value),
UCASE(column or value), LOWER(column or value), UPPER(column or value)
- Update the whole Table (drop the WHERE part)
- Assign the value of one column to another
- Same syntax for the WHERE part as SELECT

### Not supported

- LIMIT
- ORDER BY
- Everything not found in the Syntax above..

### Examples

UPDATE person SET prename='Hans2000' WHERE prename='Hans';
UPDATE person SET prename=name WHERE prename='Hans';
UPDATE person SET password=md5(password);
UPDATE person SET age=abs(-20), name=upper(name);

## CREATE TABLE

### Syntax

    CREATE TABLE (column_name  type [DEFAULT 'defaultValue'] [, column_name2  type2 [DEFAULT 'defaultValue'] , ... ]) [;]

###
Supported

- Look at Syntax
- DEFAULT (Enclose the Default-Value in ' ')
- ONLY the following Synatx: (column_name type , column_name type , ....), and the Keyword DEFAULT, and thats all !
- If you specify an Default-Value for an inc column, the first records inc values will be set to DefaultValue+1,
and for all other Records the Default-Value is ignored.

### Not supported

- Everything not found under Syntax !!
- SQL Standard types, type can only be inc (Autoincrement), int (Number) or str (String,Text).

### Example

CREATE TABLE people (id inc, prename str DEFAULT 'john', name str);

## DROP TABLE

### Syntax

    DROP TABLE table1 [, table2, ...][;]

###
Supported

- Look at Syntax
- Drop 1 or more tables in one statement

### Not supported

- Everything not found under Syntax !!

### Example

DTOP TABLE people;

## LIST TABLES

### Syntax

    LIST TABLES [WHERE ...]
    [GROUP BY spalten_name, ...]
    [ORDER BY spalten_name [ASC | DESC] ,...]
    [LIMIT [offset,]anzahl] [;]

###
Supported

- Look at Syntax
- Returns a ResultSet Object containing one Column with the name "table",
which contains all Table names of the Database as String.
- For the WHERE, GROUP BY, ORDER BY and LIMIT syntax see SELECT

### Not Supported

- Everything not found under Syntax !!
- Functions

### Example

LIST TABLES;

## CREATE DATABASE

### Syntax

    CREATE DATABASE database_name [;]

###
Supported

- Look at Syntax
- Only one Database can be created in one Statement.
- This Statement should only be executed on a ROOT_DATABASE Database!
You can get an instance of it with a $db=new Database(ROOT_DATABASE); call.
ROOT_DATABASE is a defined constant.

### Not Supported

- Everything not found under Syntax !!

### Example

CREATE DATABASE myDatabase;

## DROP DATABASE

### Syntax

    DROP DATABASE database_name [;]

###
Supported

- Look at Syntax
- Only one Database can be deleted in one Statement.
- This Statement should only be executed on a ROOT_DATABASE Database!
You can get an instance of it with a $db=new Database(ROOT_DATABASE); call.
ROOT_DATABASE is a defined constant.

### Not Supported

- Everything not found under Syntax !!

### Example

DROP DATABASE myDatabase;