Oracle database tips logo
Article categories | Blog and News Page | SQL | SQL*Plus | DBA Scripts | Standby dbs | Database Links | Techno webcasts | Oracle Magazine | Free IT Magazines | Oracle Database Hosting Companies | Site Search |
RSS
XML RSS
What is this?
AddThis Feed Button

Social Bookmarking
- Found on the internet ! -

Oracle workbooks

Download Oracle Books



- Featured ebook -

Database Normalization
by Alf Pedersen

Database Normalization ebook Understand and master how to normalize a database using methods richly documented with graphical ERD and server diagram examples

NoAdware Free Trial

NoAdware Remove
harmful
adware,
spyware,
trojans,
dialers
and worms!
-- FREE --
IT Magazine
Subscriptions

WebSite Magazine WebSite Magazine Practical advice, helpful tools and insights for website owners

Oracle Magazine Oracle Magazine Contains technology strategy articles, sample code, tips, Oracle and partner news, how-to articles for developers and DBAs

Dr Dobb's Journal Dr Dobb's Journal enables coders to write the most efficient programs and help in daily programming quandaries

DM Review DM Review is recognized as the premier business intelligence, analytics and data warehousing publication
Various other Free IT magazine subscriptions

Oracle Update
examples and syntax explanation



Always test these 'oracle update' commands on a test server
before implementing them on a production server.

The Oracle update statement allows you to change the data inside a table.

Syntax

update <table_name> set <column list> = <value list> where <restriction clause>;

If you do not specify a where clause, all rows in the table will be updated.


Permissions

You must have update privileges to update another schema's table.

If you want to update through a view, the view's owner needs to have
update permissions on the base table and you need update privileges on the view.

The 'update any table' privilege sidesteps the 2 requirements above by combining them in one privilege.


Examples

Let's use table SCOTT.DEPT as our example table.

DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON


DEPT contains 3 columns (deptno, dname and loc) and 4 rows.


SQL> select * from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON


I want to update the location for department 'SALES' to 'LONDON'.


SQL> update dept set loc = 'LONDON' 
where dname = 'SALES'; 1 row updated. SQL> commit; Commit complete. SQL> select * from dept; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES LONDON 40 OPERATIONS BOSTON


Let's update 2 columns at the same time:


SQL> update dept set loc = 'MIAMI', deptno = 50 
where dname = 'SALES'; 1 row updated. SQL> select * from dept; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 50 SALES MIAMI 40 OPERATIONS BOSTON SQL> commit; Commit complete.


Let's use a function to change the value of the location column:


SQL> update dept set loc = lower(loc) ;

4 rows updated.

SQL> commit;

Commit complete.

SQL> select * from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     new york
        20 RESEARCH       dallas
        50 SALES          miami
        40 OPERATIONS     boston


Lastly: do not forget to commit to make your changes permanent.

Recommended reading:


  • Oracle Online Documentation: SQL Reference, Update statement


  • Return from 'oracle update' back to 'oracle sql tutorials'