Вы находитесь на странице: 1из 10

Author

Ashley G. Trter

Date

23-Aug-2004

Version

2.0

REBDB

SQL Guide
Structured Query Language Guide
This document provides a brief introduction to the Structured Query Language
(SQL) used to access the database. Those familiar with MySQL, Oracle, DB2,
FileMaker or MS*Access (amongst others) should note that only a subset of the
full ANSI-standard SQL operations are currently supported.

Table of Contents
SQL Statements.................................................................................................................................... 3
Row Retrieval.................................................................................................................................. 3
Lookup........................................................................................................................................ 3
Select........................................................................................................................................... 3
Row Management............................................................................................................................ 3
Delete.......................................................................................................................................... 3
Insert............................................................................................................................................3
Truncate.......................................................................................................................................3
Update......................................................................................................................................... 3
Table Management...........................................................................................................................3
Close............................................................................................................................................4
Commit........................................................................................................................................4
Create.......................................................................................................................................... 4
Drop............................................................................................................................................ 4
Rollback...................................................................................................................................... 4
Informational....................................................................................................................................4
Describe.......................................................................................................................................4
Explain........................................................................................................................................ 4
Rows............................................................................................................................................4
Show............................................................................................................................................4
Table?..........................................................................................................................................4
Tables.......................................................................................................................................... 5
Aggregation.......................................................................................................................................... 5
Distinct.............................................................................................................................................5
Functions..........................................................................................................................................5
Count........................................................................................................................................... 5
Min.............................................................................................................................................. 5
Max............................................................................................................................................. 5

Sum............................................................................................................................................. 5
Avg..............................................................................................................................................5
Std............................................................................................................................................... 5
Group By..........................................................................................................................................5
Having..............................................................................................................................................6
Conditions.............................................................................................................................................6
Arithmetic........................................................................................................................................ 6
True / False...................................................................................................................................... 6
Search...............................................................................................................................................6
Not................................................................................................................................................... 6
And / Or........................................................................................................................................... 6
Types of values..................................................................................................................................... 6
String................................................................................................................................................6
Integer.............................................................................................................................................. 6
Decimal............................................................................................................................................ 6
Date.................................................................................................................................................. 6
Money.............................................................................................................................................. 6
Expressions...........................................................................................................................................7
Arithmetic........................................................................................................................................ 7
Positional......................................................................................................................................... 7
String................................................................................................................................................7
SQL Client............................................................................................................................................7
Statements........................................................................................................................................7
Commands....................................................................................................................................... 7
Echo............................................................................................................................................ 7
Exit.............................................................................................................................................. 7
Help............................................................................................................................................. 7
Run.............................................................................................................................................. 8
Set................................................................................................................................................8
Settings.............................................................................................................................................8
Address........................................................................................................................................9
Browser....................................................................................................................................... 9
HTML......................................................................................................................................... 9
Spool........................................................................................................................................... 9
Username.....................................................................................................................................9
Comments........................................................................................................................................ 9
SQL Scripts...........................................................................................................................................9
login.sql..........................................................................................................................................10
Sample Client Script................................................................................................................. 10
Sample Server Script.................................................................................................................10
Reserved Words..................................................................................................................................10

SQL Statements
SQL statements consist of one or more space separated tokens terminated by a carriage
return. In the following examples an italicized token indicates a value that you must
provide (eg. a table name), while a plural indicates that one or more such space separated
tokens may be provided (eg. values) within opening and closing brackets.
Tokens within curly braces indicate optional choices.

Row Retrieval
Lookup
Returns first row that matches a key.
lookup table {key [keys]}

Select
Returns columns and rows from a table.
select {distinct aggregate} {* column [{rowid} columns]} from table
where {key [keys] [conditions]}
group by {column [columns]} {having [conditions]}
order by {column [columns]} {desc}

Row Management
Delete
Deletes row(s) from a table.
delete from table {key [keys]}
delete from table where {key [keys] [conditions]}

Insert
Appends a row of values to a table.
insert into table values [{next} values]

Truncate
Deletes all rows from a table.
truncate table

Update
Updates row(s) in a table.
update
update
update
update
update
update

table
table
table
table
table
table

set
set
set
set
set
set

column to
column to
[columns]
[columns]
[columns]
[columns]

value {key [keys]}


value where {key [keys] [conditions]}
to [values] {key [keys]}
to [values] where {key [keys] [conditions]}
to [expressions] {key [keys]}
to [expressions] where {key [keys] [conditions]}

Table Management
These statements enable you to manage tables.

Close
Closes a table with no changes pending.
close {table *}

Commit
Saves a table with changes pending.
commit {table *}

Create
Creates a table.
create table [columns]

Drop
Drops a table.
drop table

Rollback
Closes a table with changes pending.
rollback {table *}

Informational
These statements give you information about various data structures.

Describe
Information about the columns of a table.
{desc describe} table

Explain
Executes statement and returns plan.
explain statement

Preceding a statement with explain causes the statement to be executed as per normal,
then return with a statement trace in lieu of its normal result(s).

Rows
Number of rows in table.
rows table

Show
Database statistics.
show

Table?
Returns true if table exists.
table? table

Tables
Information about currently open tables.
tables

Aggregation
Distinct
The distinct clause causes the entire result set to be sorted with all duplicate rows
removed. Some examples:
select distinct * from table
select distinct col1 from table

Functions
The select statement supports a number of aggregation functions.

Count
Number of values meeting criteria.
select count column from table

Min
Minimum value meeting criteria.
select min column from table

Max
Maximum value meeting criteria.
select max column from table

Sum
Summation of values meeting criteria.
select sum column from table

Avg
Arithmetic mean (average) of values meeting criteria.
select avg column from table

Std
Standard deviation of values meeting criteria.
select std col from table

Group By
If multiple columns appear in the select list then all columns prior to the last must be
grouped. Aggregate functions are always applied to the last column appearing in the
select list. Some examples:
select count [column1 column2] from table group by column1
select count [column3 column1 column2] from table group by [column1 column3]

Having
The having clause is similar to a where clause except it is applied to the aggregated
column of a group by. Some examples:
select count [col1 col2] from table group by col1 having [count > 20]
select sum [col1 col2] from table group by col1 having [any [sum < 4 sum > 8]

Conditions
Arithmetic
column {= <> < > <= >=} value

True / False
{zero? odd? even? none? empty?} column
{string? integer?} column

Search
find column string
find [values] column

Not
not condition

And / Or
{any all} [conditions]

Types of values
This section details the most common types of values.

String
A sequence of characters delimited with double quotes.
A string

Integer
A whole number.
42

Decimal
A number with at least one decimal place.
3.14

Date
A date in the form DD-MON-YYYY.
1-Jan-2000

Money
A dollars and cents amount.

$6.50

Expressions
Arithmetic
column {+ - / // **} value
column {+ - / // **} column

Positional
{first second third last} column
pick column position

String
(uppercase lowercase} column
copy/part column length

SQL Client
A SQL client is a software program that provides an environment in which you can
interactively run scripts and execute statements against the database. The standard
RebDB client is a simple console client where commands are entered at a prompt and
results displayed below that. These commands fall into four groups as detailed below.

Statements
An SQL statement, such as a select, which is sent to the database for processing.

Commands
A command that is processed by the client. The following commands are supported.

Echo
A line commencing with the echo command will display the string that follows it.
SQL> echo Have a nice day.
Have a nice day.

Exit
Exit a client by clicking the close icon of the window or typing exit at the console
prompt.
SQL> exit

Help
Typing help will display a concise summary of available statements, commands and
settings.
SQL> help
+-----------------------------------------------------------------------------+
| Row Retrieval
|
+-----------------------------------------------------------------------------+
| LOOKUP table {key [keys]}
|
| SELECT {* column [{rowid} columns]} FROM table
|
|
WHERE {key [keys] [conditions]}
|
|
GROUP BY {column [columns]} (HAVING [conditions]}
|

|
ORDER BY {column [columns]} {DESC}
|
+-----------------------------------------------------------------------------+
| Row Management
|
+-----------------------------------------------------------------------------+
| DELETE FROM table {key [keys]}
|
|
WHERE {key [keys] [conditions]}
|
| INSERT INTO table VALUES [{next} values]
|
| TRUNCATE table
|
| UPDATE table SET {column [columns]} TO {value [values]} {key [keys]}
|
|
WHERE {key [keys] [conditions]}
|
+--------------------------------------+--------------------------------------+
| Table Management
| Informational
|
+--------------------------------------+--------------------------------------+
| CLOSE {table *}
| {DESC DESCRIBE} table
|
| COMMIT {table *}
| EXPLAIN statement
|
| CREATE table [columns]
| ROWS table
|
| DROP table
| SHOW
|
| ROLLBACK {table *}
| TABLE? table
|
|
| TABLES
|
+--------------------------------------+--------------------------------------+
| Commands
| Settings
|
+--------------------------------------+--------------------------------------+
| ; {comment}
| SET ADDRESS url
|
| ECHO {text "text"}
| SET BROWSER string
|
| EXIT
| SET HTML {on off}
|
| HELP
| SET SPOOL {file off}
|
| RUN {script script.sql}
| SET USERNAME string
|
+--------------------------------------+--------------------------------------+

Run
Typing run {script script.sql} at the console prompt will run the nominated script.
SQL> run login.sql
RebDB v2.0
RUN> set browser explorer.exe
login.sql ran in 0:00 second(s)

Set
Typing set followed by a setting and value will change the setting, while entering set by
itself will display the current settings.
SQL> set
SQL> set
Address
Browser
HTML
Spool
Username

username Bob
none
explorer.exe
none
none
Bob

Settings
Settings control how the client behaves, and are typically specified in a login.sql script
(see later). All settings default to none.

Address
This setting determines what IP address and port number the client should send its
statements to.
set address tcp://127.0.0.1:1000

Alternatively, if no IP address is provided, it determines what port number the client


should listen on for incoming requests.
set address tcp://:1000

This latter setting, used in a login.sql script, is how a server is launched.

Browser
This setting determines the browser to be used when HTML output is on.
set browser explorer.exe
set browser "c:\Program Files\Opera7\opera.exe"

HTML
This setting determines whether HTML output is on or off.
set html query.html
set html off

The default file suffix, if not otherwise specified, is .html.


set html query

Spool
This setting determines whether spool output is on or off.
set spool session.txt
set spool off

The default file suffix (if not one of .txt, .lst or .log) is .txt.
set spool session

Username
This setting sets the username sent to a RebDB server, which can be useful in a multiuser environment when trying to determine who did what when.

Comments
A comment begins with a semi-colon (;) and can appear anywhere on a line. The
comment character and everything else after it on the same line are ignored by the SQL
client.
;
this comment spans an entire line
select * from table ; this comment occurs after a valid statement

These allow you to comment your scripts and / or comment out a statement that you
want to bypass.

SQL Scripts
An SQL script is a text file with a .sql extension. The SQL client runs these scripts by
reading and executing each line in turn until no more lines are present or an error is
encountered.

login.sql
This script, if present, will be run prior to the SQL client accepting commands. Typically it
contains a number of set commands.

Sample Client Script


A typical client login.sql script, connecting to a RebDB server, would contain the
following at a minimum.
set address tcp://127.0.0.1:1000
set browser explorer.exe
set username Bob

Whereas a standalone client would only need the browser setting.

Sample Server Script


A typical server login.sql script would contain the following at a minimum.
set address
set spool

tcp://:1000
server.log

Reserved Words
The following words are reserved and may not be used as table or column names:
avg
by
count
desc
distinct
explain
from
group
header
having
into
max
min
order
rowid
set
std
sum
to
values
where

Вам также может понравиться