Home >> FAQs/Tutorials >> Oracle DBA FAQ >> Index

Oracle DBA FAQ - Understanding SQL DML Statements

By: FYIcenter.com

Part:   1  2  3  

(Continued from previous part...)

How To Use Values from Other Tables in UPDATE Statements?

If you want to update values in one with values from another table, you can use a subquery in the SET clause. The subquery should return only one row for each row in the update table that matches the WHERE clause. The tutorial exercise below shows a good example:

UPDATE fyi_links SET (notes, created) = 
  (SELECT last_name, hire_date FROM employees
    WHERE employee_id = id)
  WHERE id < 110;
3 rows updated.

SELECT * FROM fyi_links WHERE id < 110;
  ID URL                      NOTES      COUNTS CREATED
---- ------------------------ --------- ------- ---------
 101 http://dev.fyicenter.com Kochhar       999 21-SEP-89
 102 http://dba.fyicenter.com De Haan         0 13-JAN-93
 103 http://sqa.fyicenter.com Hunold    NULL    03-JAN-90

This statement updated 3 rows with values from the employees table.

What Happens If the UPDATE Subquery Returns Multiple Rows?

If a subquery is used in a UPDATE statement, it must return exactly one row for each row in the update table that matches the WHERE clause. If it returns multiple rows, Oracle server will give you an error message. To test this out, you can try the following tutorial exercise:

UPDATE fyi_links SET (notes, created) = 
  (SELECT last_name, hire_date FROM employees
    WHERE employee_id < id)
  WHERE id < 110;
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row

The problem is the criteria in the subquery: "employee_id < id"

How To Delete an Existing Row from a Table?

If you want to delete an existing row from a table, you can use the DELETE statement with a WHERE clause to identify that row. Here is good sample of DELETE statements:

INSERT INTO fyi_links (url, id) 
  VALUES ('http://www.myspace.com', 301);
1 row created.

SELECT * FROM fyi_links WHERE id = 301;
   ID URL                      NOTES     COUNTS CREATED
----- ------------------------ -------- ------- ---------
  301 http://www.myspace.com    NULL     NULL   07-MAY-06  

DELETE FROM fyi_links WHERE id = 301;
1 row deleted.

SELECT * FROM fyi_links WHERE id = 301;
no rows selected

How To Delete Multiple Rows from a Table?

You can delete multiple rows from a table in the same way as deleting a single row, except that the WHERE clause will match multiple rows. The tutorial exercise below deletes 3 rows from the fyi_links table:

SELECT * FROM fyi_links WHERE id >= 250;
   ID URL                   NOTES        COUNTS CREATED
----- --------------------- ----------- ------- ---------
 1250 Retail Sales.com      Wrong URL       500 07-MAY-06
 1260 Recruiting.com        Wrong URL       520 07-MAY-06
 1270 Payroll.com           Wrong URL       540 07-MAY-06

DELETE FROM fyi_links WHERE id >= 250;
3 row deleted.

SELECT * FROM fyi_links WHERE id >= 250;
no rows selected

How To Delete All Rows a Table?

If you want to delete all rows from a table, you have two options:

  • Use the DELETE statement with no WHERE clause.
  • Use the TRUNCATE TABLE statement.

The TRUNCATE statement is more efficient the DELETE statement. The tutorial exercise shows you a good example of TRUNCATE statement:

SELECT COUNT(*) FROM fyi_links;
  COUNT(*)
----------
         3

TRUNCATE TABLE fyi_links;
Table truncated.

SELECT COUNT(*) FROM fyi_links;
  COUNT(*)
----------
         0

Part:   1  2  3  


Selected Developer Jobs:

More...