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


-- FREE --
IT Magazine
Subscriptions

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

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

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
NoAdware Free Trial

NoAdware Remove
harmful
adware,
spyware,
trojans,
dialers
and worms!
- 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

Oracle Delete
Examples and syntax explanation



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


The Oracle Delete statement allows you to remove rows from a table.

The syntax is : delete from <table_name> (where <restriction clause>);

If you specify the delete statement without the where clause, you will delete all rows from a table.

Specifying the where clause will restrict the deletion to the rows which match the restriction clause.

Here is the sample table:


SQL> select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTORD
        70 CALLCENTRE
        80 SUPPORT        NONE
        90                BOISE
        91 SHIPPING       DETROIT
        92 FINANCE        DETROIT

11 rows selected.





Let's delete all rows that are located in Detroit.


SQL> delete from dept where loc = 'DETROIT';

2 rows deleted.

SQL> select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTORD
        70 CALLCENTRE
        80 SUPPORT        NONE
        90                BOISE

9 rows selected.




Now let's select all rows that have a null value in either the dname column or the deptno column.


SQL> delete from dept where dname is null or loc is null;

2 rows deleted.

SQL> select * from dept order by deptno;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
        50 MARKETING      MELBOURNE
        60 IT             HARTORD
        80 SUPPORT        NONE

7 rows selected.





Now let's delete all the remaining rows from this table:


SQL> delete from dept;

7 rows deleted.

SQL> select * from dept order by deptno;

no rows selected




Lastly: do not forget to commit to make your changes permanent after you use the Delete statement.


Recommended reading:


  • Oracle Online Documentation: SQL Reference, Delete statement


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