User Guide
The definitions and examples below depict mSQL key words in upper case, but no such restriction is placed on the actual queries.
col_name col_type [ not null ]
[ , col_name col_type [ not null ] ]**
)
CREATE [ UNIQUE ] INDEX index_name ON table_name (
field_name
[ , field_name ] **
)
CREATE SEQUENCE ON table_name [ STEP step_val ] [ VALUE initial_val ]
An example of the creation of a table is shown below:
first_name char(15) not null,
last_name char(15) not null,
comment text(50),
dept char(20),
emp_id int
)
The available types are:-
|
|
char (len) | String of characters (or other 8 bit data) |
text (len) | Variable length string of characters (or other 8 bit data) The defined length is used to indicate the expected average length of the data. Any data longer than the specified length will be split between the data table and external overflow buffers. Note : text fields are slower to access than char fields and cannot be used in an index nor in LIKE tests. |
int | Signed integer values |
real | Decimal or Scientific Notation real values |
uint | Unsigned integer values |
date | Date values in the format of ?DD-Mon-YYYY? such as ?1-Jan-1997? |
time | Time values stored in 24 hour notation in the format of ?HH:MM:SS? |
money | A numeric value with two fixed decimal places |
These indices will be used automatically whenever
a query is sent to the database engine that uses those fields in its WHERE
clause. The user is not required to specify any special values in the query
to ensure the indices are used to increase performance.
Sequences provide a mechanism via which a sequence value can be maintained by the mSQL server. Sequences are a numeric value that can be used as serial numbers, staff identifiers, invoice numbers, or any other application that requires a unique numeric value. Having the server maintain the index allows for atomic operations (such as getting the next sequence value) and removes the concerns associated with performing these operations in client applications. A client application would need to send two queries (one to read the current value and one to update the value) which introduces a "race condition" and the potential for the same sequence value to be assigned to multiple items.
A sequence is associated with a table and a table may
contain at most one sequence. Once a sequence has been created it can be
accessed by SELECTing the _seq system variable from the table in which
the sequence is defined. For example
Using the STEP and VALUE options a sequence can be created that starts at any specified number and is increased or decreased by any specified value. The value of a sequence would decrease by 5 each time it was accessed if it was defined with a step of -5.
The drop clause cannot be used to remove an entire database. Dropping a database is achieved by using the msqladmin utility program that is included in the software distribution.
The syntax of the drop clause as well as examples of its
use are given below.
VALUES ( value [ , value ] ** )
VALUES ( 'David', 'Hughes', 'Development',12345.00)
INSERT INTO emp_details
VALUES ('David', 'Hughes', 'Development',12345.00)
FROM table [ = alias] [ , table [ = alias] ]**
[ WHERE [table.] column OPERATOR VALUE
[ AND | OR [table.]column OPERATOR VALUE]** ]
[ ORDER BY [table.]column
[DESC] [, [table.]column [DESC] ]
OPERATOR can be <, >,
=, <=, =, <>, LIKE, RLIKE, CLIKE or SLIKE
VALUE can be a literal
value or a column name
A simple select that returns the first and last names
of anybody employed in the finance department would be
WHERE dept = 'finance'
WHERE dept = 'finance'
ORDER BY last_name, first_name
DESC
WHERE dept = 'finance'
ORDER BY last_name, first_name
DESC
The standard SQL syntax provides a very simplistic regular
expression capability that does not provide the power nor the flexibility
of which UNIX programmers or users will be accustomed. mSQL supports the
"standard" SQL regular expression syntax, via the LIKE operator, but also
provides further functionality if it is required. The available regular
expression operators are:
LIKE and CLIKE utilise the regular expression syntax as
specified in the ANSI SQL standard. As mentioned above, the ANSI standard
regular expression feature provides only a very simplistic implementation
of regular expressions. It provides for only single and multiple character
wildcards. It does not include enhanced features such as value ranges,
value exclusions or value groups. The syntax of the LIKE and CLIKE operators
is provided in the following table.
|
|
|
matches any single character |
|
matches 0 or more characters of any value |
|
escapes special characters (e.g. '\%' matches % and '\\' matches \ ). All other characters match themselves |
WHERE dept = 'finance' and
last_name like '_ughes'
SELECT first_name, last_name FROM emp_details
WHERE dept = ?finance? and
last_name like ?Mc%?
Because RLIKE
utilises a complete UNIX regex implementation, the evaluation of a condition
containing the RLIKE operator is quite complex. The performance of searches
using the RLIKE operator will be slower than those using the LIKE or CLIKE
operator. You should only use the RLIKE operator if you cannot achieve
your desired matching using the more simplistic LIKE or CLIKE operators.
|
|
|
The dot character matches any single character |
|
When used as the first character in a regex, the caret character forces the match to start at the first character of the string |
|
When used as the last character in a regex, the dollar sign forces the match to end at the last character of the string |
|
By enclosing a group of single characters within square brackets, the regex will match a single character from the group of characters. If the ']' character is one of the characters you wish to match you may specify it as the first character in the group without closing the group (e.g. '[]abc]' would match any single character that was either ']', 'a', 'b', or 'c'). Ranges of characters can be specified within the group using the 'first-last' syntax (e.g. '[a-z0-9]' would match any lower case letter or a digit). If the first character of the group is the '^' character the regex will match any single character that is not contained within the group. |
|
If any regex element is followed by a '*' it will match zero or more instances of the regular expression. To match any string of characters you would use ?.*? and to match any string of digits you would use ?[0-9]*? |
Relational joining is one of the most powerful features of a relational query language. The concept of "joining" relates to "merging" multiple database tables together and extracting fields from the merged result. As an example, if you had two tables defined, one containing employee details and another containing a list of all current, you may wish to extract a list of the projects that each employee was working on. Rather than duplicating the employee details in the projects table you could simply include the employees staff ID number in the projects table and use a join to extract the first and last names.
The query below is an example of such an operation. The
logic behind the query is that we want to extract the first and last names
of the employee, plus the name of the project on which the employee is
working. We can identify which combinations of the merged table we are
looking for as they will have a common value for the employee?s staff ID
value. Because we are referencing multiple tables in the query, we must
include the table name for each field when it is included in the query
(e.g. emp_details.first_name rather than just first_name)
FROM emp_details, project_details
WHERE emp_details.emp_id = project_details.emp_id
ORDER BY emp_details.last_name,
emp_details.first_name
mSQL places no restriction on the number of tables "joined" during a query so if there were 15 tables all containing information related to an employee ID in some manner, and each table included the employee ID field to identify the employee, data from each of those tables could be extracted, by a single query. As mentioned before, a key point to note regarding joins is that you must qualify all field names with a table name. Remember that you must qualify every column name as soon as you access more than one table in a single select.
mSQL also supports table aliases so that you can perform
a join of a table onto itself. This may appear to be an unusual thing to
do but it is a very powerful feature if the rows within a single table
relate to each other in some way. An example of such a table could be a
list of people including the names of their parents. In such a table there
would be multiple rows with a parent/child relationship. Using a table
alias you could find out any grandparents contained in the table using
the query below. The logic is to find any person who is the parent of someone?s
parent.
WHERE t1.child = t2.parent
WHERE column OPERATOR value
[ AND | OR column OPERATOR
value ]**
OPERATOR can be <, >,
=, <=, =, <>, LIKE, RLIKE, CLIKE, or SLIKE
DELETE FROM emp_details WHERE salary > 20000 and salary < 30000
WHERE column OPERATOR value
[ AND | OR column OPERATOR value ]**
OPERATOR can be <, >,
=, <=, =, <>, LIKE, RLIKE, CLIKE or SLIKE
UPDATE emp_details SET salary=35000, dept=?Development? where emp_id = 1234