| Previous Chapter: Questions & Answers |
Although the present manual is not intended as an SQL tutorial, it has been considered necessary to present the supported SQL commands, together with a brief description of the commands, to inform the user about existing commands and their use. The command syntax, as well as a short example, where necessary, are also provided.
Most of the commands supported by Ovrimos are standard SQL commands. There are a few, however, such as CREATE USER, etc. that are SQL-like, but do not belong to the "body" of SQL.
SQL Commands
Syntax and examples of supported SQL commands
To present the SQL commands and the effect these commands have on a database, we assume that there is a simple application where two tables will exist, one with planets and their data and the other with satellites and their data.
These tables will contain at a certain point the following data:
PLANETS
| planet_id | planet_name | description | planet_image | distance |
| 1 | Mercury | The hot planet | BLOB | |
| 2 | Venus | The greenhouse planet | BLOB | |
| 3 | Earth | Mostly harmless | BLOB | |
| 4 | Mars | The red planet | BLOB | |
| 5 | Jupiter | The giant planet | BLOB | |
| 6 | Saturn | The ringed planet | BLOB | |
| 7 | Uranus | The blue planet | BLOB | |
| 8 | Neptune | The next to last planet | BLOB | |
| 9 | Pluto | The cold planet | BLOB |
SATELLITES
| sat_id | sat_name | planet_id | sat_image |
| 1 | Luna | 3 | BLOB |
| 2 | Phobos | 4 | BLOB |
| 3 | Deimos | 4 | BLOB |
| 4 | Io | 5 | BLOB |
| 5 | Europa | 5 | BLOB |
| 6 | Ganymede | 5 | BLOB |
| 7 | Callisto | 5 | BLOB |
| 8 | Rhea | 6 | BLOB |
| 9 | Dione | 6 | BLOB |
| 10 | Titan | 6 | BLOB |
| 11 | Mimas | 6 | BLOB |
| 12 | Miranda | 7 | BLOB |
| 13 | Triton | 8 | BLOB |
| 14 | Charon | 9 | BLOB |
Later in this section, we will show a way to insert and retrieve BLOBs.
At some point, a user pandora and a user friend are
added to the system.
Pandora user has the same privileges as admin.
Alter Table
The Alter Table command is applied to modify the definition
of an existing table.
ALTER TABLE table name
ADD [COLUMN] column definition ;
Example:
ALTER TABLE planets
ADD (distance real);
The command modifies table planets, adding a new column of real numbers,
called distance.
So, if the table was:
| planet_id | planet_name | Description | planet_image |
| planet_id | planet_name | Description | planet_image | distance |
CALL proc ...
Example:
CALL create_table;
where create_table is a stored procedure (in any language) which creates a table.
Create Domain
The Create Domain command is applied to define a new domain,
i.e. a new data type.
CREATE DOMAIN domain name [ AS ] data type
[ constraint definition ... ] ;
where
constraint definition ::=
check constraint
Example:
CREATE DOMAIN planet_order integer check value 0;
The newly created domain range will be used in the data definition as an integer number greater than 0, i.e. as a natural number.
Create Index
The Create Index command defines a new index on an existing
table.
CREATE [UNIQUE] INDEX index name ON TABLE table name (column name [ASC|DESC].,..);
Example:
CREATE UNIQUE INDEX ind1 ON TABLE planets (planet_id ASC);
A new index, planet_id, will be used as an index in ascending order. Its values are unique.
Note: This is not a standard SQL command.
Create Table
The Create Table command creates a new table. It may be permanent
or temporary.
CREATE TABLE table name
( {column definition | [table constraint] }.,..);
where
column definition ::=
column name { domain name | data type [size] }
[column constraint ...]
[DEFAULT default value ]
Example:
CREATE TABLE planets (
planet_id planet_order;
planet_name varchar(20);
description varchar (100);
planet_image long varbinary;
);
The newly created table planets will be:
| planet_id | planet_name | description | planet_image |
The newly created table satellites will be:
| sat_id | sat_name | planet_id | sat_image |
Create User
The Create User command defines a new database user. It is possible
to use a LIKE clause that creates a user with the privileges of a previously
defined prototype user, thus allowing privilege inheritance.
CREATE USER user name FOR 'full user-name' WITH 'password'|NULL [LIKE prototype user] ;
Example:
CREATE USER pandora FOR 'Mary Adams' WITH 'pass1'
LIKE admin;
This command will create a user with name pandora with initial password pass1. This new user is like admin, which means that she has full privileges. This is an example of privilege inheritance.
CREATE USER friend FOR 'Joe Phillips' WITH 'pass2';
This user has no admin privileges.
Note: This is not a standard SQL command.
Create View
The Create View command defines a view, i.e. a virtual table
on one or more tables or views.
CREATE VIEW table name [(column list)]
AS (SELECT statement);
All views created have as default CASCADED CHECK OPTION.
Example:
CREATE VIEW planet_list
AS (SELECT planet_id, planet_name FROM PLANETS);
The Create View command will make a view called planet_list
with only two columns visible. For the users of this view the virtual table
planet_list will appear like:
| planet_id | planet_name |
Delete Positioned
The positioned Delete command is used to remove a row from a
table. The row is where the cursor has been previously positioned.
DELETE FROM table name
WHERE CURRENT OF cursor name;
Example:
DELETE FROM planets
WHERE CURRENT OF Cur1;
The cursor cur1 is previously defined to traverse the table. The cursor is positioned to a certain row usually after the execution of a select statement. The command will delete the row where the cursor is positioned.
Delete Searched
The searched Delete command is used to remove rows from a table.
If a predicate exists, then the searched rows are deleted, otherwise the
entire table contents are removed.
DELETE FROM table name
[ WHERE predicate ];
Example:
DELETE FROM planets
WHERE id > 8;
This command will delete the rows (if any) where the planet_id is greater than 8, i,e, Pluto will be removed from the table.
Drop Domain
The Drop Domain command is used to discard a previously created
domain.
DROP DOMAIN domain name;
Example:
DROP DOMAIN planet_order;
After the Drop Domain command, the identifier range can no longer be used as a data type. The columns of tables defined by this domain are not affected from the domain deletion, since the domain is used as a macro which is expanded at the time of column definition and does not appear anywhere else.
Drop Index
The Drop Index command is used to discard a previously created
index.
DROP INDEX index name;
Example:
DROP INDEX ind1;
After the Drop Index command, the columns involved in the ind1 index (i.e. planet_id) are no longer used as key. The requirement to use this index as a unique key is also no longer required.
Note: This is not a standard SQL command.
Drop Table
The Drop Table command is used to discard a previously created
table.
DROP TABLE table name CASCADE | RESTRICT;
Example:
DROP TABLE Satelites;
The table satellites is removed and all its data are deleted.
Drop User
The Drop User command is used to remove a database user.
DROP USER user name;
Example:
DROP USER friend;
After the command, user friend is no longer in the system. All tables and views created by user friend are now visible only by the user admin, who may decide to grant privileges to some other user to manager these tables.
Note: This is not a standard SQL command.
Drop View
The Drop View command is used to discard a previously defined
view.
DROP VIEW view name CASCADE | RESTRICT;
Example:
DROP VIEW planet_list;
View planet_list is removed from the database.
Grant
The Grant command is used to give privileges to users.
GRANT privilege.,.. ON object name
TO { grantee.,..} | PUBLIC
[ WITH GRANT OPTION ];
privilege ::=
{ ALL PRIVILEGES }
| { SELECT
| DELETE
| { INSERT [ (column name
.,.. ) ] }
| { UPDATE [ (column name.,..)
] }
| { REFERENCES [ (column
name.,..) ]}}
object name ::=
[ TABLE ] table name
| DOMAIN domain name
Example:
GRANT ALL PRIVILEGES ON planets TO friend;
GRANT SELECT, INSERT ON satellites TO friend with GRANT option;
The first command allows full privileges to user friend on table planets.
The second allows SELECT and INSERT to user friend on table satellites.
The grant option allows the user to pass this privilege to other users
as well.
Insert
The Insert command is used to insert rows into a table.
INSERT INTO table name
[ (column name.,..) ]
query expression | table value constructor;
Example:
INSERT INTO PLANETS (planet_id, planet_name, description) values
(1, 'Mercury', 'The hot planet');
Revoke
The Revoke command is used to deprive a user from previously
granted privileges.
REVOKE [ GRANT OPTION FOR ]
{ ALL PRIVILEGES } | { privilege.,..}
ON object
FROM PUBLIC | {grantee.,..}
CASCADE | RESTRICT;
Example:
REVOKE INSERT, DELETE ON planets FROM friend;
REVOKE GRANT OPTION FOR SELECT ON satellites FROM FRIEND;
The first command revokes the INSERT and DELETE option from user friend, while the second disallows user FRIEND to grant the SELECT option to other users. (User friend retains his/her own privilege to SELECT from satellites).
Select
The Select command is used to return rows from one or more tables.
SELECT [DISTINCT]
{ { aggregate function.. | value expression [AS column
name]}.,..}
| {qualifier.*}
| *
FROM { { table name [AS] correlation name] [ (column
name.,..) ]}
| { subquery [AS] correlation name [column name.,..]}
| joined table }.,..
[WHERE predicate]
GROUP BY {{ [table name | correlation name}.] column name}.,..
[HAVING predicate]
[ UNION [ALL]
select statement | {TABLE table name} | table
value constructor ]
[ORDER BY {{ output column [ASC | DESC]}.,..}
| {{positive integer [ASC | DESC]}.,..};
Example:
SELECT planet_id from planets where planet_name = 'Earth';
| planet_id |
| 3 |
| count |
| 6 |
A Select with a subquery, where the two tables are connected
via the planet_id field.
| planet_name |
| Uranus |
| planet_name |
| Mercury |
| Venus |
| Earth |
| Mars |
| Jupiter |
| Saturn |
| Uranus |
| Neptune |
| Pluto |
SELECT planet_name, sat_name FROM planets, satellites
WHERE planets.planet_id = satellites.planet_id and planets.planet_id
< 9;
| planet_name | sat_name |
| Earth | Luna |
| Mars | Phobos |
| Mars | Deimos |
| Jupiter | Io |
| Jupiter | Europa |
| Jupiter | Ganymede |
| Jupiter | Callisto |
| Saturn | Rhea |
| Saturn | Dione |
| Saturn | Titan |
| Saturn | Mimas |
| Uranus | Miranda |
| Neptune | Triton |
A few more Select examples follow, which have nothing to do with the planets, satellites tables, but are good examples indicating how complex queries may be written in SQL.
In the following examples, tables SALESPEOPLE, ORDERS, CITIES and CUSTOMERS are involved.
Select within a select
SELECT *
FROM ORDERS
WHERE SNUM =
(SELECT SNUM
FROM SALESPEOPLE
WHERE CITY = 'Barcelona');
Select with implicit table join
SELECT Customers.cname, Salespeople.sname,
Salespeople.city
FROM Salespeople, Customers
WHERE Salespeople.city = Customers.city;
SELECT Customers.cname, Salespeople.sname
FROM Customers, Salespeople
WHERE Salespeople.snum = Customers.snum;
Select with ANY
SELECT *
FROM SALESPEOPLE
WHERE CITY = ANY
(SELECT CITY
FROM CUSTOMERS);
Select with HAVING and MAX
SELECT ODATE, SUM (AMT)
FROM ORDERS A
GROUP BY ODATE
HAVING SUM (AMT) >
(SELECT 2000.00 + MAX (AMT)
FROM ORDERS B
WHERE A.ODATE = B.ODATE);
Select with Union
SELECT snum, sname
FROM Salespeople
WHERE city = 'London'
UNIONSELECT cnum, cname
Select for Update
The Select for Update command selects rows of one or more tables
in order to perform an update to the values of the rows returned.
select command FOR UPDATE OF;
where select command::=
SELECT [DISTINCT]
{ { aggregate function.. | value expression [AS column
name]}.,..}
| {qualifier.*}
| *
FROM { { table name [AS] correlation name] [ (column
name.,..) ]}
| { subquery [AS] correlation name [column name.,..]}}.,..
[WHERE predicate]
GROUP BY {{ [table name | correlation name}.] column name}.,..
[HAVING predicate]
[ UNION [ALL]
select statement | {TABLE table name} | table value
constructor ]
[ORDER BY {{ output column [ASC | DESC]}.,..}
| {{positive integer [ASC | DESC]}.,..};
Example:
SELECT distance FROM planets WHERE planet_id = 4 FOR UPDATE OF;
The field distance may now be updated.
Set Time Zone
The Set Time Zone command defines a local time zone displacement.
SET TIME ZONE value | LOCAL;
Example:
SET TIME ZONE LOCAL;
The above command sets the time zone to the local time zone of the server.
SET TIME ZONE '+02:00';
The above command sets the time zone to +2 hours of GMT.
Times are always kept in GMT but they are retrieved as local times
(according to the time zone).
Example of time zone usage. Suppose a table entrance of people entering a place and a timestamp held on their entrance:
Entrance
| Person_Name | Entrance time |
This is local time 11:05:00, which corresponds to GMT 09:05:00.
select * from entrance where person_name = 'John Smith';
| John Smith | 11:05:00 |
select * from entrance where person_name = 'John Smith';
| John Smith | 19:05:00 |
Set Transaction
The Set Transaction command sets the attributes of the next
transaction, i.e. the isolation level, option to read or write into the
database etc.
SET TRANSACTION { ISOLATION LEVEL
{ READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SERIALIZABLE }
| { READ ONLY | READ WRITE};
Example:
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
The next transaction will be performed on a read committed level. See also The Lock Manager.
Update Positioned
The positioned Update command is used to update the contents
of the row where the cursor has been previously positioned.
UPDATE table name
SET { column name = { value expression | NULL
}}
WHERE CURRENT OF cursor name;
Example:
UPDATE planets SET distance = 210000000 WHERE CURRENT OF cur1;
The distance field of the row where cursor cur1 has been placed will be updated.
Update Searched
The searched Update command is used to update the contents of
rows searched by a defined predicate. If the predicate is used, then only
the rows confronting to the predicate are updated, otherwise all rows of
a table are modified.
UPDATE table name
SET { column name = { value expression | NULL
}}
{ WHERE predicate };
Example:
UPDATE planets SET distance = 210000000 where planet_id = 4;
Update User
The Update User command modifies a database user's password.
UPDATE USER user name { WITH 'new password' | NULL }[LIKE prototype] ;
Example:
UPDATE USER pandora WITH 'xyzzy';
UPDATE USER friend LIKE pandora;
Note: This is not a standard SQL command.
Commit
The Commit command terminates a transaction, making the current
changes permanent.
COMMIT [WORK];
Rollback
The Rollback command rolls backs to the beginning of the current
transaction, undoing in the process all uncommitted changes.
ROLLBACK [WORK];
SQL FUNCTIONS
The functions described below are Standard SQL-2 functions. There is another set of supplemental functions, implemented to allow the users as much flexibility as possible from both the SQL Terminal and sqlapp4 command line.
CAST
Converts a scalar value to a specified scalar data type. The syntax is:CAST ( scalar-expression AS data-type )
The value of the specified scalar expression is converted to "data type".CASE
Of course some data types may not be converted to others.
Returns one of the specified sets of values, depending on some conditions. The general format is as follows:POSITIONCASE
when-clause-list
ELSE scalar-expression
END
where a 'when-clause' takes the form:
WHEN conditional-expression THEN scalar-expressionExample:
CASE
WHEN value < 100 THEN 'Cheap'
WHEN value < 200 THEN 'Medium'
ELSE 'Expensive'
END
Returns the position within a specified character string (string2) of another specified character string (string1). Each of string1 and string2 is specified as an arbitrary character string expression. More precisely, the expression POSITION (string2 IN string1) is defined to return a value as follows:SUBSTRINGIf the CHARACTER_LENGTH of string2 is zero, the result is 1.
Otherwise, if string2 occurs as a substring within string1, the result is one greater than the number of characters in string1 that precede the first such occurrence.
Otherwise the result is zero.Example:
POSITION ('morning', 'Good Morning')
returns 6.Position does not apply to bit strings.
Extracts a substring of a string. For example, the expressionTRIM
SUBSTRING ('Good Morning' FROM 3 FOR 4)
extracts 'od M'.
In general, the string argument can be any character or bit string expression, and the FROM and FOR arguments can be any numeric expression. If FOR is ommited, all the right part from FROM is extracted.
Returns a character string that is identical to a specified character string leading and/or trailing pad characters are removed.TRIM (ltb pad FROM string)
ltb = leading, trailing or both. If omitted, option both is assumed.pad is a character string expression that evaluates to a single character. The pad character to be removed is specified. If the pad character is not specified, then space is assumed.
If ltb and pad are omitted, FROM must also be omitted.
Whenever a BLOB needs to be used, it is defined as either long varbinary or long varchar. In the above example of planets and satellites, the image of the planet is defined as :
planet_image long varbinary
To insert into the table values for the BLOB the idea is to use SQL parameters (the ? symbol). Note that the SQL Terminal does not accept parameters, so BLOBS can only be defined by the sqlapp4 application.
For example, if the associated BLOB is a GIF file called earth.gif, then the insertion of the value into the column image is done with the following command:
insert into planets (image) values (? 'image/gif');
Here the ? is the parameter and 'image/gif ' is the mime type image/gif. As soon as this command is typed, the program responds with the prompt:
File to send for BLOB parameter 0?
At this point the user types the name of the GIF file, e.g. c:\nasa\earth.gif.
The file must be typed with the full path name as the operating system requires it for finding the file. No quotes are used for the file name.
Contrary to the insertion of the BLOB from the sqlapp4 program, it is not possible to retrieve a BLOB image from this application. An appropriate browser is required. The BLOB address, however, may be retrieved through the use of a special function, called URI.
URI stands for Uniform Resource Identifier and is used to return the BLOB address as a URL address. For example, the command:
Select planet_name, uri(planet_image) from planets where planet_id = 3;
in the sqlapp4 application program will respond with an output of the following form:
Planet_name |
------------------------------------------------------------
'Earth' | http//128.134.23.140:8181/blobxxxxxxxx
The http address of the BLOB may be used by the HTTP server to display
the BLOB data. See also the URI function.
Supplemental SQL Functions
Besides implementing the basic SQL commands, a wide variety of functions to be used together with SQL Commands is also provided. These functions belong to the following general categories: Mathematical functions, Date and Time functions, String manipulation functions, Blob manipulation functions and System functions.
A brief description of the functions, together with the arguments and
the return values is provided below. Several functions are overloaded.
The user may also note the underscore (_) in the beginning of some function
names. This was necessary to
differentiate the function from SQL reserved words.
Before providing the functions, a short explanation of the arguments used is also necessary:
Expressions can be the names of columns, where this data type is defined. They may also be results of other scalar functions. It is also possible to be literals of the appropriate data type. The underlying data types for the following expressions are those presented below:
Thus, the format for DATE is yyyy-mm-dd, and whenever used in an SQL statement, it must be enclosed in single quotes (') and preceded by the reserved word DATE. Thus, the 2nd of December of the year 1962 is described as:
DATE '1962-12-02'. Legal times range from '0001-01-01' to '9999-12-31'.
The format of TIME is hh:mm:ss or hh:mm:ss.ttt (with the .ttt part, signifying thousandths of a second optional). Whenever used in an SQL statement, it must be enclosed in single quotes (') and preceded by the reserved word TIME. Thus, 9.30 AM is described as: TIME '09:30:00' or '09:30:00.000'. Legal times range from '00:00:00.000' to '23:59:59.999'.
The timestamp is the date and time combined, with an optional time zone interval in the form of +hh:mm. Whenever used in an SQL statement, the timestamp expression must be enclosed in single quotes (') and preceded by the reserved word TIMESTAMP. The time interval is the time difference of a place from the GMT (Greenwich Mean Time). For example, the interval for Athens, Greece is +02:00 and the timestamp for the 11th of September of 1998, at 7.30 PM in Athens is: TIMESTAMP '1998-09-11 19:30:00+02:00'.
If the timestamp interval is not explicitly set, then the local time zone (i.e. the server time zone is used.) The user may modify the local time zone through the command SET TIME ZONE.
Mathematical functions
For making the function examples below more solid, we assume there
is a table tempo, with columns angle (containing angles in
radians) and number (containing numbers).
For a better presentation of the functions below, we assume there exists
a table testpic, created as follows:
create table testpic (
id integer,
picture long varbinary);
Later on, a gif/image type BLOB has been inserted into the table with id =1.
For data definition, the SQL/92 synonyms are supported for the following types:
User management in Ovrimos is performed with simple commands. One extra feature, found in Ovrimos is the option of privilege inheritance. Specific users can be designated as prototype users, typically with a null password that disallows logging in. Users created like this user, inherit his privileges. This makes it easy to administer privileges for groups of people with a simple grant/revoke to/from the prototype user. This hierarchy can be extended to as many levels as we wish. In the future, multiple inheritance will allow the SQL3 "roles" to be implemented.
update user username with 'new-password';e.g. update user admin with 'nebula';
update user identifier [for 'user name'] [with 'password'][like identifier];
create user username for 'Full Name' with 'user password';e.g. create user mary for 'Mary' with 'xyz123';
Full syntax is:
create user username for 'full name' with 'password'|NULL [like prototype-user]e.g create user mary for 'Mary' with 'xyz123' like john.
drop user username;e.g. drop user mary;
The Integrity Enhancement Facility (IEF) is implemented, but with the following shortcomings:
Each database has its own built-in set of system tables, i.e. tables
that contain data about the users, tables, columns, keys, references, views,
etc.
These tables belong to each database exclusively and there is no clash
of namespaces and privileges between databases.
System tables are visible to users and their columns displayed.
The system tables in version 2.5 of Ovrimos are:
sys.users
sys.tables
sys.columns
sys.table_privileges
sys.column_privileges
sys.keys
sys.key_columns
sys.foreign_keys
sys.fkey_columns
sys.blobs
sys.views
sys.view_refs
sys.data_types
sys.domains
Users who wish to explore the system tables have to visit the Roadmap, where the tool for exploration of tables, views and users is described.
Warning! Users
must never directly update system tables through SQL commands. Administrators,
wishing to modify user data must always do so from the specifically provided
Ovrimos SQL commands. See above, Privilege
Inheritance.
| Previous Chapter: Questions & Answers |