Docsity

Manipulating Data, Exams of Finance

Partial preview of the text

Download Manipulating Data and more Exams Finance in PDF only on Docsity!

Schedule: Timing Topic

40 minutes Lecture 30 minutes Practice 70 minutes Total

Manipulating Data

Lesson Aim

In this lesson, you will learn how to insert rows into a table, update existing rows in a table, and delete existing rows from a table. You will also learn how to control transactions with the COMMIT, SAVEPOINT, and ROLLBACK statements.

8-

Objectives

After completing this lesson, you shouldAfter completing this lesson, you should

be able to do the following:be able to do the following:

  • Describe each DML statement
  • Insert rows into a table
  • Update rows in a table
  • Delete rows from a table
  • Control transactions

Adding a New Row to a Table

The slide graphic adds a new department to the DEPT table.

8-

Adding a New Row to a Table

DEPTDEPT

DEPTNO DNAME LOC

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

New row New row

50 DEVELOPMENT DETROIT

DEPT DEPT

DEPTNO DNAME LOC

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

“…insert a new row “…insert a new row into DEPT table…”into DEPT table…”

50 DEVELOPMENT DETROIT

Adding a New Row to a Table (continued)

You can add new rows to a table by issuing the INSERT statement. In the syntax: table is the name of the table column is the name of the column in the table to populate value is the corresponding value for the column Note: This statement with the VALUES clause adds only one row at a time to a table.

8-

The INSERT Statement

  • Add new rows to a table by using the

INSERT statement.

  • Only one row is inserted at a time with

this syntax.

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

VALUES (value [ , value... ]) ;

8-

Inserting Rows with Null Values

  • Implicit method: Omit the column from

the column list.

SQL> INSERT INTO dept (deptno, dname )

2 VALUES (60, 'MIS');

1 row created.1 row created.

  • Explicit method: Specify the NULL

keyword.

SQL> INSERT INTO dept

2 VALUES (70, 'FINANCE', NULL);

1 row created.1 row created.

Methods for Inserting Null Values

Be sure that the targeted column allows null values by verifying the Null? status from the SQL*Plus DESCRIBE command. The Oracle Server automatically enforces all datatypes, data ranges, and data integrity constraints. Any column that is not listed explicitly obtains a null value in the new row.

Instructor Note

Common errors that can occur during user input:

  • Mandatory value missing for a NOT NULL column
  • Duplicate value violates uniqueness constraint
  • Foreign key constraint violated
  • CHECK constraint violated
  • Datatype mismatch
  • Value too wide to fit in column

Method Description Implicit Omit the column from the column list. Explicit Specify the NULL keyword in the VALUES list Specify the empty string ('') in the VALUES list; for character strings and dates only.

8-

Inserting Special Values

The SYSDATE function records theThe SYSDATE function records the

current date and time.current date and time.

SQL> INSERT INTO emp (empno, ename, job,

2 mgr, hiredate, sal, comm,

3 deptno)

4 VALUES (7196, 'GREEN', 'SALESMAN',

5 7782, SYSDATE, 2000, NULL,

1 row created.1 row created.

Inserting Special Values by Using SQL Functions

You can use pseudocolumns to enter special values in your table. The slide example records information for employee Green in the EMP table. It supplies the current date and time in the HIREDATE column. It uses the SYSDATE function for current date and time. You can also use the USER function when inserting rows in a table. The USER function records the current username.

Confirming Additions to the Table

SQL> SELECT empno, ename, job, hiredate, comm 2 FROM emp 3 WHERE empno = 7196;

EMPNO ENAME JOB HIREDATE COMM

7196 GREEN SALESMAN 01-DEC-

8-

Inserting Values by Using

Substitution Variables

Create an interactive script by usingCreate an interactive script by using

SQLPlus substitution parameters.SQLPlus substitution parameters.**

SQL> INSERT INTO dept (deptno, dname, loc)

2 VALUES (&department_id,

3 '&department_name', '&location');

Enter value for department_id: 8080

Enter value for department_name: EDUCATIONEDUCATION

Enter value for location: ATLANTAATLANTA

1 row created.

Inserting Values by Using Substitution Variables

You can produce an INSERT statement that allows the user to add values interactively by using SQL*Plus substitution variables. The slide example records information for a department in the DEPT table. It prompts the user for the department number, department name, and location. For date and character values, the ampersand and the variable name are enclosed in single quotation marks.

Instructor Note

Be sure to mention the following points about the example:

  • The names of the SQL*Plus substitution parameters do not have to match the corresponding column names.
  • Substitution parameters are lexical variables. Whatever characters the user enters are substituted as text for the variable name.
  • The SQL*Plus SET VERIFY command lists the substitution before executing the statement.

8-

Creating a Script

with Customized Prompts

  • ACCEPT stores the value in a variable.
  • PROMPT displays your customized text.

ACCEPT department_id PROMPT 'Please enter the -

department number:'

ACCEPT department_name PROMPT 'Please enter -

the department name:'

ACCEPT location PROMPT 'Please enter the -

location:'

INSERT INTO dept (deptno, dname, loc)

VALUES (&department_id, '&department_name',

'&location');

Creating a Script to Manipulate Data

You can save your command with substitution variables to a file and execute the file. Each time you execute the command, it will prompt you for new values. Customize the prompts by using the SQL*Plus ACCEPT command. The example on the slide records information for a department in the DEPT table. It prompts the user for the department number, department name, and location by using customized prompt messages.

Please enter the department number: 90 Please enter the department name: PAYROLL Please enter the location: HOUSTON

1 row created. Do not prefix the SQLPlus substitution parameter with the ampersand (&) when referencing it in the ACCEPT command. Use a dash (-) to continue a SQLPlus command on the next line.

Instructor Note

Be sure to mention the following points about the script:

  • Do not prefix the SQL*Plus substitution parameter with the ampersand in the ACCEPT command.
  • Use a dash to continue a SQL*Plus command on the next line.
  • Add a space after the colon in the PROMPT command.

Changing Data in a Table

The slide graphic changes the department number for Clark from 10 to 20.

8-

Changing Data in a Table

EMPEMP

“…update a row“…update a row in EMP table…”in EMP table…”

EMPEMP

EMPNO ENAME JOB ... DEPTNO

7839 KING PRESIDENT 10

7698 BLAKE MANAGER 30

7782 CLARK MANAGER 10

7566 JONES MANAGER 20

EMPNO ENAME JOB ... DEPTNO

7839 KING PRESIDENT 10

7698 BLAKE MANAGER 30

7782 CLARK MANAGER 10

7566 JONES MANAGER 20

Updating Rows

You can modify existing rows by using the UPDATE statement. In the above syntax: table is the name of the table column is the name of the column in the table to populate value is the corresponding value or subquery for the column condition identifies the rows to be updated and is composed of column names expressions, constants, subqueries, and comparison operators Confirm the update operation by querying the table to display the updated rows. For more information, see Oracle Server SQL Reference , Release 8, “UPDATE.” Note: In general, use the primary key to identify a single row. Using other columns may unexpectedly cause several rows to be updated. For example, identifying a single row in the EMP table by name is dangerous because more than one employee may have the same name.

Instructor Note

Demo: l9sel.sql, l9upd.sql Purpose: To illustrate displaying the initial state of data, performing updates, and viewing results.

8-

The UPDATE Statement

  • Modify existing rows with the UPDATE

statement.

  • Update more than one row at a time, if

required.

UPDATE table

SET column = value [, column = value, ... ]

[WHERE condition ];

8-

Updating with

Multiple-Column Subquery

SQL> UPDATE emp

2 SET (job, deptno) =

3 (SELECT job, deptno

4 FROM emp

5 WHERE empno = 7499)

6 WHERE empno = 7698;

1 row updated.1 row updated.

Update employee 7698’s job and department Update employee 7698’s job and department

to match that of employee 7499.to match that of employee 7499.

Updating Rows with a Multiple-Column Subquery

Multiple-column subqueries can be implemented in the SET clause of an UPDATE statement. Syntax

Instructor Note

It may be worth showing participants that the results would be the same for the example on the slide if two different subqueries were used in the SET clause as illustrated below:

SQL> UPDATE emp 2 SET job = (SELECT job FROM emp 3 WHERE empno = 7499), 4 deptno = (SELECT deptno FROM dept 5 WHERE dname = 'SALES') 6 WHERE empno = 7698;

UPDATE table

SET ( column, column , ...) =

(SELECT column, column , ...

FROM table

WHERE condition )

WHERE condition ;

8-

Updating Rows Based

on Another Table

Use subqueriesUsesubqueries in UPDATE statements toin UPDATE statements to

update rows in a table based on valuesupdate rows in a table based on values

from another table.from another table.

SQL> UPDATE employee

2 SET deptno = (SELECT deptno

3 FROM emp

4 WHERE empno = 7788)

5 WHERE job = (SELECT job

6 FROM emp

7 WHERE empno = 7788);

2 rows updated.2 rows updated.

Updating Rows Based on Another Table

You can use subqueries in UPDATE statements to update rows in a table. The example on the slide updates the EMPLOYEE table based on the values from the EMP table. It changes the department number of all employees with employee 7788’s job title to employee 7788’s current department number.

Removing a Row from a Table

The slide graphic removes the DEVELOPMENT department from the DEPT table (assuming that there are no constraints defined on the DEPT table).

Instructor Note

After all the rows have been eliminated with the DELETE statement, only the data structure of the table remains. A more efficient method of emptying a table is with the TRUNCATE statement. You can use the TRUNCATE statement to quickly remove all rows from a table or cluster. Removing rows with the TRUNCATE statement is faster than removing them with the DELETE statement for the following reasons:

  • The TRUNCATE statement is a data definition language (DDL) statement and generates no rollback information. It will be covered in a subsequent lesson.
  • Truncating a table does not fire the DELETE triggers of the table.
  • If the table is the parent of a referential integrity constraint, you cannot truncate the table. Disable the constraint before issuing the TRUNCATE statement.

8-

“…“…delete a rowdelete a row

from DEPT table…”from DEPT table…”

Removing a Row from a Table

DEPTDEPT

DEPTNO DNAME LOC

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

50 DEVELOPMENT DETROIT

60 MIS

DEPTDEPT

DEPTNO DNAME LOC

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

60 MIS

Deleting Rows

You can remove existing rows by using the DELETE statement. In the syntax: table is the table name condition identifies the rows to be deleted and is composed of column names, expressions, constants, subqueries, and comparison operators For more information, see Oracle Server SQL Reference , Release 8, “DELETE.”

Instructor Note

The DELETE statement does not ask for confirmation. However, the delete operation is not made permanent until the data transaction is committed. Therefore, you can undo the operation with the ROLLBACK statement if you make a mistake.

8-

The DELETE Statement

You can remove existing rows from aYou can remove existing rows from a

table by using the DELETE statement.table by using the DELETE statement.

DELETE [FROM] table

[WHERE condition ];