Manipulating data

advertisement

Manipulating Data

7-2

Objectives

Insert new rows into a table.

Update existing rows in a table.

Delete rows from a table.

Explain transaction controls and their importance.

7-3

Data Manipulation and

Transaction Control Commands

Command

INSERT

UPDATE

DELETE

COMMIT

SAVEPOINT

ROLLBACK

Description

Adds a new row to the table.

Modifies existing rows in the table.

Removes existing rows from the table.

Makes all pending changes permanent.

Allows a rollback to that savepoint marker.

Discards all pending data changes.

7-4

Inserting New Rows into a Table:

Syntax

Add new rows to a table by using the INSERT command.

INSERT INTO

VALUES

table [(column [, column...])]

(value [, value...]);

Only one row is inserted at a time with this syntax.

7-5

Inserting New Rows: Example

Insert a new row containing values for each column.

Optionally list the columns in the INSERT clause.

INSERT

VALUES

1 row created.

INTO s_dept

(11, 'Finance', 2);

List values in the default order of the columns in the table.

Enclose character and date values within single quotation marks.

7-6

Inserting New Rows with Null Values

Implicit Method

Omit the column from the column list.

INSERT INTO

VALUES

1 row created.

s_dept (id, name)

(12, 'MIS');

Explicit Method

Specify the NULL keyword or the empty string ('') in the VALUES list.

INSERT INTO

VALUES

1 row created.

s_dept

(13, 'Administration', NULL);

7-7

Copying Rows from Another Table

Write your INSERT command with a subquery.

INSERT INTO history(id, last_name, salary, title, start_date)

SELECT id, last_name, salary, title,start_date

FROM s_emp

WHERE start_date < '01-JAN-94';

10 rows created.

Do not use the VALUES clause.

Match the number of columns in the INSERT clause to those in the subquery.

7-8

Updating Rows in a Table: Syntax

Modify existing rows with the UPDATE command.

UPDATE table

SET column = value [, column = value]

[WHERE condition];

7-9

Updating Rows: Examples

Transfer employee number 2 to department 10.

UPDATE

SET

WHERE s_emp dept_id = 10 id = 2;

1 row updated.

Transfer employee number 1 to department 32 and change the salary to 2550.

UPDATE

SET s_emp dept_id = 32, salary = 2550

WHERE id = 1;

1 row updated.

7-10

Updating All Rows in the Table

All rows in the table will be updated if you do not add the WHERE clause.

UPDATE s_emp

SET commission_pct = 10;

25 rows updated.

7-11

Deleting Rows from a Table: Syntax

Remove existing rows by using the DELETE command.

DELETE [FROM]

[WHERE table

condition];

Remove all information about employees who started after January 1, 1996.

DELETE FROM s_emp

WHERE start_date > '01.01.1996';

1 row deleted.

7-12

Deleting Rows: Example

Delete all the rows in the table by excluding the

WHERE clause.

DELETE FROM test;

25,000 rows deleted.

Confirm the deletions.

SELECT * FROM no rows selected test;

7-13

Database Transactions

Contain one of the following statements:

DML commands that make up one consistent change to the data

One DDL command

One DCL command

Begin when the first executable SQL command is executed.

End with one of the following events:

COMMIT or ROLLBACK

DDL or DCL command executes (automatic commit)

Errors, exit, or system crash

7-14

Advantages of COMMIT and ROLLBACK

Ensure data consistency.

Preview data changes before making changes permanent.

Group logically related operations.

7-15

Controlling Transactions

COMMIT

INSERT

UPDATE

Savepoint

Marker A

Savepoint

Marker B

INSERT

DELETE

ROLLBACK

ROLLBACK ROLLBACK to A ROLLBACK to B

Download