Documente Academic
Documente Profesional
Documente Cultură
This section is a reference for the SQL grammar that Mckoi SQL Database supports.
Mckoi SQL Database supports a subset of entry level ANSI SQL-92. This section is not
intended to be a tutorial for learning SQL. For SQL books and online tutorials see the
links section on the home page.
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 2 of 11
triggered_action :: =
NO ACTION | SET NULL | SET DEFAULT | CASCADE
When declaring string or binary column types the maximum size must be specified. The
following example declares a string column that can grow to a maximum of 100
characters,
When handling strings the database will only allocate as much storage space as the string
uses up. If a 10 character string is stored in str_col then only space for 10 characters will
be allocated in the database. So if you need a column that can store a string of any size,
use an arbitrarily large number when declaring the column. Mckoi SQL Database does not
use a fixed size storage mechanism when storing variable length column data.
JAVA_OBJECT is a column type that can contain serializable Java objects. The
JAVA_OBJECT type has an optional Java class definition that is used for runtime class
constraint checking. The following example demonstrates creating a JAVA_OBJECT
column.
If the Java class is not specified the column defaults to java.lang.Object which
effectively means any type of serializable Java object can be kept in the column.
String types may have a COLLATE clause that changes the collation ordering of the string
based on a language. For example, the folling statement creates a string that can store and
order Japanese text;
The 'jaJP' is an ISO localization code for the Japanese language in Japan. Other locale
codes can be found in the documentation to java.text.Collate.
Unique, primary/foreign key and check integrity constraints can be defined in the CREATE
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 3 of 11
ALTER is used to add / remove / modify the columns and integrity constraints of a table.
The ADD [COLUMN] form adds a new column definition to the table (using the same
column declaration syntax in the CREATE command). The DROP [COLUMN] form drops the
column with the name from the table. ALTER [COLUMN] column_name SET
default_expr alters the default value for the column. ALTER [COLUMN] column_name
DROP DEFAULT removes the default value set for the column.
The other form of this statement is ALTER CREATE TABLE .... This alters the table to the
specification of the given CREATE statement. Any columns that are in the original table are
not lost provided the column name is in the new table specification. Any columns that
were not in the original table are set to the default value.
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 4 of 11
col2 NUMERIC,
col3 VARCHAR(90000) )
The ALTER CREATE TABLE ... syntax is an extension to the SQL-92 standard.
Removes the table(s) from the database. The IF EXISTS clause will drop the table only if
it exists. If this clause is not present an error is generated if the table does not exist. Any
data that was in a dropped table is lost so use with care.
Creates a new view. A view is a virtual table based on the result of a SELECT query. The
content of a view may reference any number of other tables and views.
A view acts like a regular table and can be queried as you would a table made with the
CREATE TABLE statement. Views are read-only.
Removes a view from the database. A view can be changed by dropping and recreating it.
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 5 of 11
Creates a new sequence generator that can be used to generate an iterative sequence of
values. Sequence generators have a number of uses including the creation of primary keys
for a table. The INCREMENT, MINVALUE, MAXVALUE, START, and CACHE
values are all optional.
The INCREMENT value specifies how the sequence increments each iteration. By default
a sequence generator increments by 1. The MINVALUE and MAXVALUE values
specify the bounds of the sequence generator. By default MINVALUE and MAXVALUE
are 0 and Long.MAX_VALUE respectively. The START value specifies the first key
(exclusive) of the generator. The CACHE value specifies how many keys should be
cached ahead of time.
Below is an example that creates a new sequence generator called 'seq_key_1' that starts
at 10 and increments by 2 each iteration;
A sequence generator is accessed by a call to the NEXTVAL function. The NEXTVAL function
iterates the generator and returns the next value from the sequence. The NEXTVAL function
is an atomic operation and guarantees that no two identical values will be returned
regardless of the frequency or concurrency of calls to the function. Below is a simple
example;
SELECT NEXTVAL('seq_key_1')
Drops a sequence generator previously created with the CREATE SEQUENCE statement. A
sequence generator may be changed by dropping the sequence and then recreating it.
Compacts the table data file in the file system. This removes all unused space from the
table file and may rearrange the structure of the table to a form that better fits the
characteristics of the data being stored.
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 6 of 11
Creates a schema with the given name. By default a database has three schema initially
defined, SYS_INFO, SYS_JDBC and APP. The SYS_INFO and SYS_JDBC schema contain a
number of important system tables and the APP schema is the default user schema.
Drops the schema with the given name. A schema may only be dropped if it contains no
tables. The SYS_INFO and APP schema may not be dropped.
This is the SQL command to insert records into a table in the database. This statement
comes in three forms. The first inserts data from a VALUES clause;
The second form is used to copy information from a SELECT query into the table specified
in the INSERT statement. For example;
The third form uses a list of column SET assignments. For example;
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 7 of 11
If a column of the table is not specified in an INSERT the default value declared for the
column is used. If no default value was declared a NULL value is inserted in the column.
If the column is declared as NOT NULL the insert operation fails.
Deletes all the rows from the table that match the WHERE clause. An optional LIMIT clause
specifies the maximum number of matched rows to be removed. An example of using the
DELETE statement;
Updates information in a table. The SET clause is a list of assignments that describe how
the columns of the data matched by the WHERE clause are to be updated. Any columns not
assigned in the SET clause are left unchanged. Examples of using UPDATE;
UPDATE Employee
SET salary = salary * 1.25
WHERE name = 'Bob'
UPDATE Order
SET id = id + 3, part = CONCAT(part, '-00')
WHERE part LIKE 'PO-%'
LIMIT 10
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 8 of 11
The SELECT statement is used to form queries for extracting information out of the
database. The following example query will return the number, quantity and price of all
orders for more than 5 items sorted in descending order by order number. In addition it
rounds the order price to two decimal places and applies a dollar ($) sign to the output.
The ORDER BY and GROUP BY clause may refer to a column, a column alias, or an
expression. The HAVING clause is evaluated after the grouping and aggregate columns
have been resolved.
For examples of using SELECT with aggregate functions see the 'Internal SQL Functions'
section.
Transactional operations for closing a transaction and either committing all the changes
made or rolling back and disposing all changes. COMMIT may cause a concurrent
transaction conflict exception to be thrown. If a conflict is detected the transaction is
automatically rolled back. See the 'Transactions' section of the documentation for further
details of how Mckoi handles transactions.
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 9 of 11
These are user management commands for creating/altering and dropping users in the
system. Only members of the 'secure access' group are permitted to perform these
operations, which includes the administrator user that is setup when the Mckoi database is
created.
The following example creates a user called 'harry' with the password 'cat';
See the 'JDBC Driver' section for documentation on connecting to a database using a
username and password to connect to a database.
Grants or revokes types of access on a table or view to a user. When a table or view is
created the system gives full grant options to the user that created the object. The user is
given the option to grant other users selective access to the object through the GRANT and
REVOKE syntax. For example, the follow statement shows how a user would grant user
'toby' permission to SELECT from a table called MyTable;
The GRANT command allows granting all users access to an object. The following
statement makes MyTable globally readable;
If you wish to give a user the option of granting a privilege to another user, add WITH
GRANT OPTION to the GRANT statement.
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 10 of 11
Makes a change to the state of the connection. SET AUTO COMMIT is used to switch
transaction 'auto commit mode' on or off. When auto commit mode is on the engine
commits after every statement. By default, a connection starts with auto commit mode
switched on. SET TRANSACTION ISOLATION LEVEL currently only supports the
SERIALIZABLE isolation level. See the 'Transactions' section of the documentation for
details of how Mckoi handles transactions.
This command provides information about the columns of the table. It shows the column
names, the type / size and scale (if applicable) and other useful information.
Shows internal information about the database system. SHOW TABLES returns a list of
tables in the database. SHOW STATUS returns debugging and statistical information about
the internal state of the database engine. SHOW CONNECTIONS returns a snapshot of the
current connections on the database. SHOW SCHEMA lists all the schema defined.
Shuts down the database. If the database is running as a server the database shuts down
cleanly and the process is stopped. If the database is embedded in a Java application it is
cleanly put into a shut down state.
Only a user with the correct grants may successfully execute this command.
file://E:\sandy\SQL%20Syntax.htm 6/19/2004
SQL Syntax Page 11 of 11
file://E:\sandy\SQL%20Syntax.htm 6/19/2004