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:
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.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Select | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Insert | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
INSERT StatementThe 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 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 ClauseThe 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-1and 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 ExamplesINSERT INTO p (pno, color) VALUES ('P4', 'Brown')
INSERT INTO sp SELECT s.sno, p.pno, 500 FROM s, p WHERE p.color='Green' AND s.city='London'
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Update | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
UPDATE StatementThe UPDATE statement modifies columns in selected table rows. It has the following general format: 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 ClauseThe SET Clause in the UPDATE Statement updates (assigns new value to) columns in the selected table rows. It has the
following general format: 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 ExamplesUPDATE sp SET qty = qty + 20
UPDATE s SET name = 'Tony', city = 'Milan' WHERE sno = 'S3'
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Delete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DELETE StatementThe 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 ExamplesDELETE FROM sp WHERE pno = 'P1'
DELETE FROM p WHERE pno NOT IN (SELECT pno FROM sp)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Transactions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SQL-Transaction StatementsSQL-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 OverviewA 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:
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 IsolationIn 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:
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:
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 TransactionsThe 3rd type of SQL Statements - SQL-Schema Statements, may participate in the transaction mechanism. SQL-Schema statements can either be:
SQL92 leaves the choice up to the individual DBMS. It is implementation defined behavior. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Begin Transaction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BEGIN TRANSACTION StatementMarks the beginning of a group of statements to be executed as a block. It is the target of a ROLLBACK statement. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
End | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
END StatementMarks 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 StatementThe 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: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Rollback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ROLLBACK StatementThe 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: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example Tables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In the subsequent text, the following 3 example tables are used:
|