SPS Home    >   Dgreath    >   RDBMS    >   Data Manipulation Language

DATA MANIPULATION LANGUAGE


Language Structure

SQL is a keyword based language. Each statement begins with a unique keyword. SQL statements consist of clauses which begin with a keyword. SQL syntax is not case sensitive.

The other lexical elements of SQL statements are:

  • Names of database elements: tables, columns, views, users, schemas; names must begin with a letter (a - z) and may contain digits (0 - 9) and underscore (_)
  • Literals: quoted strings, numeric values, datetime values
  • Delimiters & Operators:  + - , ( ) = < > <= >= <> . * / || ? ;

Basic database objects (tables, views) can optionally be qualified by schema name. A dot "." separates qualifiers:

schema-name . table-name

Column names can be qualified by table name with optional schema qualification.

Note: Names can be case sensitive and contain spaces and other delimiters and can use keywords, by surrounding them with double quotation marks ("). For example, "1 Name w/spaces" "SELECT"
Quoted names must match exactly on case.

Select

Content

Insert

INSERT Statement

The INSERT Statement adds one or more rows to a table. It has two formats:

INSERT INTO table-1 [(column-list)] VALUES (value-list)

and,

INSERT INTO table-1 [(column-list)] (query-specification)

The first form inserts a single row into table-1 and explicitly specifies the column values for the row. The second form uses the result of query-specification to insert one or more rows into table-1. The result rows from the query are the rows added to the insert table. Note: the query cannot reference table-1.

Both forms have an optional column-list specification. Only the columns listed will be assigned values. Unlisted columns are set to null, so unlisted columns must allow nulls. The values from the VALUES Clause (first form) or the columns from the query-specification rows (second form) are assigned to the corresponding column in column-list in order.

If the optional column-list is missing, the default column list is substituted. The default column list contains all columns in table-1 in the order they were declared in CREATE TABLE or CREATE VIEW.

VALUES Clause

The VALUES Clause in the INSERT Statement provides a set of values to place in the columns of a new row. It has the following general format:

VALUES ( value-1 [, value-2] ... )value-1

and

value-2

are Literal Values or Scalar Expressions involving literals. They can also specify NULL.

The values list in the VALUES clause must match the explicit or implicit column list for INSERT in degree (number of items). They must also match the data type of corresponding column or be convertible to that data type.

INSERT Examples

INSERT INTO p (pno, color) VALUES ('P4', 'Brown')

Before   After
pno descr color
P1 Widget Blue
P2 Widget Red
P3 Dongle Green

=>

pno descr color
P1 Widget Blue
P2 Widget Red
P3 Dongle Green
P4 NULL Brown
INSERT INTO sp SELECT s.sno, p.pno, 500 FROM s, p WHERE p.color='Green' AND s.city='London'
Before   After
sno pno qty
S1 P1 NULL
S2 P1 200
S3 P1 1000
S3 P2 200

=>

sno pno qty
S1 P1 NULL
S2 P1 200
S3 P1 1000
S3 P2 200
S2 P3 500

Update

UPDATE Statement

The UPDATE statement modifies columns in selected table rows. It has the following general format:
UPDATE table-1 SET set-list [WHERE predicate]
The optional WHERE Clause has the same format as in the SELECT Statement. See WHERE Clause. The WHERE clause chooses which table rows to update. If it is missing, all rows are in table-1 are updated.

The set-list contains assignments of new values for selected columns. See SET Clause.

The SET Clause expressions and WHERE Clause predicate can contain subqueries, but the subqueries cannot reference table-1. This prevents situations where results are dependent on the order of processing.

SET Clause

The SET Clause in the UPDATE Statement updates (assigns new value to) columns in the selected table rows. It has the following general format:
SET column-1 = value-1 [, column-2 = value-2] ...column-1 and column-2 are columns in the Update table. value-1 and value-2 are expressions that can reference columns from the update table. They also can be the keyword -- NULL, to set the column to null.

Since the assignment expressions can reference columns from the current row, the expressions are evaluated first. After the values of all Set expressions have been computed, they are then assigned to the referenced columns. This avoids results dependent on the order of processing.

UPDATE Examples

UPDATE sp SET qty = qty + 20
Before   After
sno pno qty
S1 P1 NULL
S2 P1 200
S3 P1 1000
S3 P2 200

=>

sno pno qty
S1 P1 NULL
S2 P1 220
S3 P1 1020
S3 P2 220

UPDATE s SET name = 'Tony', city = 'Milan' WHERE sno = 'S3'
Before   After
sno name city
S1 Pierre Paris
S2 John London
S3 Mario Rome

=>

sno name city
S1 Pierre Paris
S2 John London
S3 Tony Milan

Delete

DELETE Statement

The DELETE Statement removes selected rows from a table. It has the following general format:

DELETE FROM table-1 [WHERE predicate]

The optional WHERE Clause has the same format as in the SELECT Statement. See WHERE Clause. The WHERE clause chooses which table rows to delete. If it is missing, all rows are in table-1 are removed.

The WHERE Clause predicate can contain subqueries, but the subqueries cannot reference table-1. This prevents situations where results are dependent on the order of processing.

DELETE Examples

DELETE FROM sp WHERE pno = 'P1'
Before   After
sno pno qty
S1 P1 NULL
S2 P1 200
S3 P1 1000
S3 P2 200

=>

sno pno qty
S3 P2 200

DELETE FROM p WHERE pno NOT IN (SELECT pno FROM sp)
Before   After
pno descr color
P1 Widget Blue
P2 Widget Red
P3 Dongle Green

=>

pno descr color
P1 Widget Blue
P2 Widget Red

Transactions

SQL-Transaction Statements

SQL-Transaction Statements control transactions in database access. This subset of SQL is also called the Data Control Language for SQL (SQL DCL).

There are 2 SQL-Transaction Statements:

Transaction Overview

A database transaction is a larger unit that frames multiple SQL statements. A transaction ensures that the action of the framed statements is atomic with respect to recovery.

A SQL Modification Statement has limited effect. A given statement can only directly modify the contents of a single table (Referential Integrity effects may cause indirect modification of other tables.) The upshot is that operations which require modification of several tables must involve multiple modification statements. A classic example is a bank operation that transfers funds from one type of account to another, requiring updates to 2 tables. Transactions provide a way to group these multiple statements in one atomic unit.

In terms of direct effect on the database, it is the SQL Modification Statements (DML) that are the main consideration since they change data. The total set of changes to the database by the modification statements in a transaction are treated as an atomic unit through the actions of the transaction. The set of changes either:

  • Is made fully persistent in the database through the action of the COMMIT Statement, or
  • Has no persistent effect whatever on the database, through:
    • the action of the ROLLBACK Statement,
    • abnormal termination of the client requesting the transaction, or
    • abnormal termination of the transaction by the DBMS. This may be an action by the system (deadlock resolution) or by an administrative agent, or it may be an abnormal termination of the DBMS itself. In the latter case, the DBMS must roll back any active transactions during recovery.

The DBMS must ensure that the effect of a transaction is not partial. All changes in a transaction must be made persistent, or no changes from the transaction must be made persistent.

Transaction Isolation

In most cases, transactions are executed under a client connection to the DBMS. Multiple client connections can initiate transactions at the same time. This is known as concurrent transactions.

In the relational model, each transaction is completely isolated from other active transactions. After initiation, a transaction can only see changes to the database made by transactions committed prior to starting the new transaction. Changes made by concurrent transactions are not seen by SQL DML query and modification statements. This is known as full isolation or Serializable transactions.

SQL92 defines Serializable for transactions. However, fully serialized transactions can impact performance. For this reason, SQL92 allows additional isolation modes that reduce the isolation between concurrent transactions. SQL92 defines 3 other isolation modes, but support by existing RDBMSs is often incomplete and doesn't always match the SQL92 modes.

Transaction isolation controls the visibility of changes between transactions in different sessions (connections). It determines if queries in one session can see changes made by a transaction in another session. There are three types of read:

  • Dirty Read -- a session can read rows changed by transactions in other sessions that have not been committed. If the other session then rolls back its transaction, subsequent reads of the same rows will find column values returned to previous values, deleted rows reappearing and rows inserted by the other transaction missing.
  • Non-repeatable Read -- a session can read a row in a transaction. Another session then changes the row (UPDATE or DELETE) and commits its transaction. If the first session subsequently re-reads the row in the same transaction, it will see the change.
  • Phantoms -- a session can read a set of rows in a transaction that satisfies a search condition (which might be all rows). Another session then generates a row (INSERT) that satisfies the search condition and commits its transaction. If the first session subsequently repeats the search in the same transaction, it will see the new row.

There are four levels of transaction isolation.

Level One - Serializable: This level provides the greatest isolation from other transactions. At this transaction isolation level, a transaction is fully isolated from changes made by other sessions. Queries issued under Serializable transactions cannot see any subsequent changes, committed or not, from other transactions. The effect is the same as if transactions were serial, that is, each transaction completing before another one is begun. In Serializable, Dirty Reads, Non-repeatable Reads, and Phantoms are not possible. 

Level Two - Repeatable Read: This level does not read uncommitted changes so dirty reads are impossible. In Repeatable Read isolation level, Dirty Reads and Non-repeatable Reads are not possible but Phantoms are.

Level Three - Read Committed: This level also does not read uncommitted changes so dirty reads remain impossible but non-repeatable reads and phantoms are possible.

Level Four - Read Uncommitted: At the opposite end of the spectrum is Read Uncommitted. It is the lowest level of isolation. With Read Uncommitted, a session can read (query) subsequent changes made by other sessions, either committed or uncommitted. Read uncommitted transactions have the following characteristics:

The isolation provided by each transaction isolation level is summarized below:

  Dirty Reads Non-repeatable Reads Phantoms
Read Uncommitted Y Y Y
Read Committed N Y Y
Repeatable Read N N Y
Serializable N N N

Note: SQL92 defines the SET TRANSACTION statement to set the transaction isolation level for a session, but most DBMSs support a function/method in the Client API as an alternative.

SQL-Schema Statements in Transactions

The 3rd type of SQL Statements - SQL-Schema Statements, may participate in the transaction mechanism. SQL-Schema statements can either be:
  • included in a transaction along with SQL-Data statements,
  • required to be in separate transactions, or
  • ignored by the transaction mechanism (can't be rolled back).

SQL92 leaves the choice up to the individual DBMS. It is implementation defined behavior.

Begin Transaction

BEGIN TRANSACTION Statement

Marks the beginning of a group of statements to be executed as a block. It is the target of a ROLLBACK statement.

End

END Statement

Marks the end of a group of statements to be executed as a block. It stops execution of the block without causing either a COMMIT or ROLLBACK to occur.

Commit

COMMIT Statement

The COMMIT Statement terminates the current transaction and makes all changes under the transaction persistent. It commits the changes to the database. The COMMIT statement has the following general format:
COMMIT [WORK]

WORK is an optional keyword that does not change the semantics of COMMIT.

Rollback

ROLLBACK Statement

The ROLLBACK Statement terminates the current transaction and rescinds all changes made under the transaction. It rolls back the changes to the database. The ROLLBACK statement has the following general format:

ROLLBACK [WORK]

WORK is an optional keyword that does not change the semantics of ROLLBACK. When nesting transactions, ROLLBACK WORK always rolls back to the outermost BEGIN TRANSACTION statement and decrements the @@TRANCOUNT system function to 0.

Example Tables

In the subsequent text, the following 3 example tables are used:

p Table (parts) s Table (suppliers)
pno descr color
P1 Widget Blue
P2 Widget Red
P3 Dongle Green
sno name city
S1 Pierre Paris
S2 John London
S3 Mario Rome

CREATE TABLE p
(
pno VARCHAR(5) NOT NULL PRIMARY KEY,
descr VARCHAR(16),
color VARCHAR(8)
)

CREATE TABLE s
(
sno VARCHAR(5) NOT NULL PRIMARY KEY,
name VARCHAR(16),
city VARCHAR(16)
)
sp Table (suppliers & parts)  
sno pno qty
S1 P1 NULL
S2 P1 200
S3 P1 1000
S3 P2 200
 

CREATE TABLE sp
(
sno VARCHAR(5) NOT NULL REFERENCES s,
pno VARCHAR(5) NOT NULL REFERENCES p,
qty INT,
PRIMARY KEY (sno, pno)
)