SML# Document Version 4.0.0
22 SQL Expressions and Commands

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, w) SQL.command

Details of these commands are shown in the following subsections.

(sqlfn1; ; sqlfnn) (n 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 sqlfnn.

...(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 lab1, , labn 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 (lab1, , labn), either the table #vid.lab refers to or the sqlselect query must have at least columns 𝑙𝑎𝑏1,,𝑙𝑎𝑏n. Other columns may be included in it. for sqlselect, the column sets of its result and the designated table may be different.

  • If no (lab1, , labn) appears, the column set of the result of the sqlselect query and the designated table must be identical.

  • The type of the i-th expression sqlexpi in the values columns must be ({},w)τi where τi is the type of the labi 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 labi of each updateRowi must be a column in the designated table. The table may have other columns.

  • The type of sqlexpi of each updateRowi must be ({𝑙𝑎𝑏 : τ},w)τi where τ is the type of the designated table and τi is the type of the labi column in τ.

  • If there exists sqlWhereClause, it must be of type (τ -> τ, w) 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 (τ -> τ, w) 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