Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BD / Labs / English / Lab3-Data manipulation.doc
Скачиваний:
27
Добавлен:
20.02.2016
Размер:
148 Кб
Скачать
☆
      1. Keywords and Parameters

        schema

        is the schema containing the table. If you omit schema, Oracle assumes the table or view is in your own schema.

        table

        is the name of the table into which rows are to be inserted.

        column

         

        is a column of the table. In the inserted row, each column in this list is assigned a value from the values_clause or the subquery.

        If you omit one of the table's columns from this list, the column's value for the inserted row is the column's default value as specified when the table was created. For more information on default column values, see Lab1. If any of these columns has a NOT NULL constraint and it does not have a default value, then Oracle returns an error indicating that the constraint has been violated and rolls back the INSERT statement.

        If you omit the column list altogether, the values_clause or query must specify values for all columns in the table.  

        values_clause

        specifies a row of values to be inserted into the table or view. You must specify a value in the values_clause for each column in the column list. If you omit the column list, then the values_clause must provide values for every column in the table.  

        subquery

         

         

        is a subquery that returns rows that are inserted into the table. If the subquery selects no rows, Oracle inserts no rows into the table.

        When specified with VALUES, the subquery returns values to be inserted into one row.

        When specified without VALUES, the subquery can return values to be inserted into more than one row.

        The subquery can refer to any table, including the target table of the INSERT statement. The select list of this subquery must have the same number of columns as the column list of the INSERT statement. If you omit the column list, then the subquery must values for all columns in a table.

        t_alias

        provides a correlation name for the table to be referenced elsewhere in the statement.  

      2. Examples

Values Examples

The following statement inserts a row into the FACULTY table:

INSERT INTO FACULTY

VALUES (1, 'informatics', 'Sidorov', '5', 93517);

The following statement inserts a row with six columns into the TEACHER table. One of these columns is assigned NULL: Values of two columns (Tel and Commission) are not defined. So Tel column the value NULL is assigned and Commission column takes the value 0 (default value):

INSERT INTO TEACHER (TchNo, DepNo, name, post, hiredate, salary)

VALUES (15, 7, 'Ivanov', NULL, NULL, TO_DATE('25.07.2001', 'DD.MM.YYYY'), 350.00);

Subquery Example

The following statement copies professors whose Commission exceeds 25% of salary into the BONUS table:

INSERT INTO bonus

SELECT *

FROM TEACHER

WHERE Commission > 0.25 * salary AND post 'proessor';

    1. Updating rows in a table

      1. Syntax

      1. Purpose

To change existing values in a table.

      1. Prerequisites

For you to update values in a table, the table must be in your own schema or you must have UPDATE privilege on the table.

The UPDATE ANY TABLE system privilege also allows you to update values in any table or any view's base table.

      1. Keywords and Parameters

        schema

        is the schema containing the table. If you omit schema, Oracle assumes the table is in your own schema.  

        table

        is the name of the table to be updated.

        set_clause

         

         

        column

        is the name of a column of the table that is to be updated. If you omit a column of the table from the set_clause, that column's value remains unchanged.

        subquery

        is a subquery that returns exactly one row for each row updated.

        If you specify only one column in the set_clause, the subquery can return only one value.

        If you specify multiple columns in the set_clause, the subquery must return as many values as you have specified columns.

        If the subquery returns no rows, then the column is assigned a null.

        expr

        is the new value assigned to the corresponding column.

        where_clause

        restricts the rows updated to those for which the specified condition is TRUE. If you omit this clause, Oracle updates all rows in the table or view. See the syntax description of conditions in Lab2 .

        The where_clause determines the rows in which values are updated. If you do not specify the where_clause, all rows are updated. For each row that satisfies the where_clause, the columns to the left of the equals (=) operator in the set_clause are set to the values of the corresponding expressions on the right. The expressions are evaluated as the row is updated.

      2. Examples

Simple Examples

The following statement gives null Commission to all teaches with the post assistant:

UPDATE TEACHER

SET Commission = NULL

WHERE post = 'assistant';

The following statement moves Jones Department 20 with a 100 salary raise (assuming there is only one Jones):

UPDATE TEACHER

SET DepNo =20, salary = salary + 100

WHERE name = 'jones';

Complex Example

This example shows the following syntactic constructs of the UPDATE statement:

  • Both forms of theset_clausetogether in a single statement .

  • A correlated subquery .

  • Awhere_clauseto limit the updated rows .

UPDATE TEACHER a

SET DepNo =

(SELECT DepNo

FROM deptartment

WHERE name = 'data bases and information systems'),

(salary, Commission) =

(SELECT 1.1*AVG(salary), 1.5*AVG(Commission)

FROM TEACHER b

WHERE a.DepNo = b.DepNo)

WHERE DepNo IN

(SELECT DepNo

FROM DEPARTMENT

WHERE FacNo = 1);

The above UPDATE statement performs the following operations:

  • Updates only those teachers that work in faculty with DepNo = 1

  • Sets DepNo for these teaches to the DepNo of'Data bases and information systems'

  • Sets each teachers’s salary to 1.1 times the average salary of their department.

  • Sets each teachers's Commission to 1.5 times the average Commission of their department

Соседние файлы в папке English