Oracle
  Home arrow Oracle arrow Page 6 - Introduction to SQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Introduction to SQL
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2005-03-16


    Table of Contents:
  • Introduction to SQL
  • A Brief History of SQL
  • The SELECT Statement
  • Ordering your results
  • The DELETE Statement
  • The MERGE Statement

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introduction to SQL - The MERGE Statement
    ( Page 6 of 6 )

    There are certain situations, especially within Data Warehouse applications, where you may want to either insert a new row into a table or update an existing row depending on whether or not the data already exists in the table. For example, you may receive a nightly feed of parts data that contains both parts that are known to the system along with parts just introduced by your suppliers. If a part number exists in the part table, you will need to update the unit_cost and status columns; otherwise, you will need to insert a new row.

    While you could write code that reads each record from the feed, determines whether or not the part number exists in the part table, and issues either an INSERT or UPDATE statement, you could instead issue a single MERGE statement.*Assuming that your data feed has been loaded into the part_stg staging table, your MERGE statement would look something like the following:

    1  MERGE INTO part p_dest
    2  USING part_stg p_src
    3  ON (p_dest.part_nbr = p_src.part_nbr)
    4  WHEN MATCHED THEN UPDATE
    5    SET p_dest.unit_cost = p_src.unit_cost, 
      p_dest.status = p_src.status
    6  WHEN NOT MATCHED THEN INSERT (p_dest.part_nbr, 
      p_dest.name,
    7    p_dest.supplier_id, p_dest.status, 
      p_dest.inventory_qty,
    8    p_dest.unit_cost, p_dest.resupply_date)
    9    VALUES (p_src.part_nbr, p_src.name,
    10   p_src.supplier_id, p_src.status, 0, p_src.unit_cost, 
       null);

    This statement looks fairly complex, so here is a description of what it is doing:

    Lines 1–3

    For each row in the part_stg table, see if the part_nbr column exists in the part table.

    Lines 4–5

    If it does, then update the matching row in the part table using data from the part_stg table.

    Lines 6–10

    Otherwise, insert a new row into the part table using the data from the part_stg table.

    So Why Are There 17 More Chapters?

    After reading this chapter, you might think that SQL looks pretty simple (at least the DML portion). At a high level, it is fairly simple, and you now know enough about the language to go write some code. However, you will learn over time that there are numerous ways to arrive at the same end point, and some are more efficient and elegant than others. The true test of SQL mastery is when you no longer have the desire to return to what you were working on the previous year, rip out all the SQL, and recode it. For one of us, it took about nine years to reach that point. Hopefully, this book will help you reach that point in far less time.

    While you are reading the rest of the book, you might notice that the majority of examples use SELECT statements, with the remainder somewhat evenly distributed across INSERT, UPDATE, and DELETE statements. This disparity is not indicative of the relative importance of SELECT statements over the other three DML statements; rather, SELECT statements are favored because we can show a query’s result set, which should help you to better understand the query, and because many of the points being made using SELECT statements can be applied to UPDATE and DELETE statements as well.


    * Depending on the purpose of the model, entities may or may not
       correlate to database tables. For example, a logical model depicts
       business entities and their relationships, whereas a physical model
       illustrates tables and their primary/foreign keys. The model in Figure
       1-1 is a physical model.

    * NULL indicates the absence of a value. The use of NULL is covered in
       Chapter 2.

    * MERGE was introduced in Oracle9i.

    This article is excerpted from Mastering Oracle SQL by Sanjay Mishra and Alan Beaulieu (O'Reilly, 2004; ISBN  1590593006). Check it out at your favorite bookstore today. Buy this book now.

     



     
     
    >>> More Oracle Articles          >>> More By O'Reilly Media
     

       

    ORACLE ARTICLES

    - Oracle's Turn to Play in the Sun
    - Implementing and Using Oracle`s Restore Poin...
    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT