22.6 SQL commands
SML# accept the following subset of SQL commands sqlcommand as well as SELECT queries:
sqlcommand | ::= | sqlinsert | INSERT commands |
---|---|---|---|
sqlupdate | UPDATE commands | ||
sqldelete | DELETE commands | ||
sqltransaction | transaction management commands | ||
(sqlfn; ; sqlfn) | command sequence | ||
... ( exp ) | embedded SQL commands |
The type of INSERT, UPDATE, DELETE, and transaction commands is
(unit, ) SQL.command
Details of these commands are shown in the following subsections.
(sqlfn; ; sqlfn) ( must be greater than 1) is a command consisting of a sequence of commands separated by semicolons. Its type is equal to the type of the last command sqlfn.
...(exp) embeds the result of exp as a command.
22.6.1 INSERT commands
The syntax of the INSERT command is the following:
sqlinsert | ::= | insert into #vid.lab (lab, , lab) |
values insertRow, , insertRow | ||
insert into #vid.lab (lab, , lab) | ||
values insertVar | ||
insert into #vid.lab (lab, , lab) sqlselect | ||
insertRow | ::= | (insertVal, , insertVal) |
insertVal | ::= | sqlexp default |
insertVar | ::= | vid op longvid |
The syntax is restricted by the following rules:
-
•
The labels of (lab, , lab) must be distinct.
-
•
The number of insertVal in insertRow must be equal to the number of labels in (lab, , lab).
An INSERT command inserts the rows in either the values subclause or the result of the sqlselect query into the designated table. If an SML# variable vid or op longvid is written in the values subclause, the list of the records consisting of labels lab, , lab denoted by the variable is inserted into the table. If the rows to be inserted does not cover all the columns in the designated table, the columns not covered are filled with the default values, which is specified when the table is created. If a column is specified as default in a row of the values clause, the default value is also inserted into that column. If a default value is needed but no default value is specified, the database server causes an runtime error and the SQL.Exec exception is raised in SML#.
The typing rules are the following:
-
•
vid must be of the SQL.db type that has at least the table name lab.
-
•
If there is (lab, , lab), either the table #vid.lab refers to or the sqlselect query must have at least columns . Other columns may be included in it. for sqlselect, the column sets of its result and the designated table may be different.
-
•
If no (lab, , lab) appears, the column set of the result of the sqlselect query and the designated table must be identical.
-
•
The type of the -th expression sqlexp in the values columns must be where is the type of the lab column of the designated table.
22.6.2 UPDATE Commands
The syntax of the UPDATE command is the following:
sqlupdate | ::= | update #vid.lab |
set updateRow, , updateRow | ||
sqlWhereClause | ||
updateRow | ::= | lab = sqlexp |
The syntax is restricted by the following rules:
-
•
All the labs of updateRows must be distinct.
An UPDATE command updates the rows in the designated table and matched with the condition of the WHERE clause with the values of the SET clause. The columns not specified in the SET clause are left original. The expressions in the SET clauses may include column references of the table lab in order to refer to the original value.
The typing rules are the following:
-
•
vid must be of the SQL.db type that has at least the table name lab.
-
•
The column lab of each updateRow must be a column in the designated table. The table may have other columns.
-
•
The type of sqlexp of each updateRow must be where is the type of the designated table and is the type of the lab column in .
-
•
If there exists sqlWhereClause, it must be of type ( -> , ) SQL.whr where is the type of the designated table.
22.6.3 DELETE commands
The following is the syntax of the DELETE commands:
sqldelete | ::= | delete from #vid.lab sqlWhereClause |
An DELETE commands deletes the rows matched with the condition of the WHERE clause from the designated table.
The typing rules are the following:
-
•
vid must be of the SQL.db type that has at least the table name lab.
-
•
If there exists sqlWhereClause, its type must be of ( -> , ) SQL.whr where is the type of the designated table.
22.6.4 BEGIN, COMMIT, and ROLLBACK commands
The following transaction control commands are available in SML#:
sqltransaction | ::= | begin | start a transaction |
---|---|---|---|
commit | commit a transaction | ||
rollback | abort a transaction |