MySQL
  Home arrow MySQL arrow Page 2 - Advanced Query Writing, concluded
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
MYSQL

Advanced Query Writing, concluded
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-03-09

    Table of Contents:
  • Advanced Query Writing, concluded
  • Using SQL to Generate SQL
  • The CASE Expression
  • Quiz

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Advanced Query Writing, concluded - Using SQL to Generate SQL


    (Page 2 of 4 )

    Most RDBMS products come with a set of catalog views that allow query access to the metadata that describes the database objects contained in the database. Experienced database administrators know how to use the data in the catalog to their advantage and actually use SQL statements to generate other SQL statements.

    Generating SQL in Oracle

    In Oracle, the USER_TABLES view contains information about each table that belongs to the current database user. You can use the command “DESCRIBE USER_TABLES” to see the definition of the view, or refer to the Oracle Server Reference manual for descriptions of this and other catalog views.

    Here is an SQL statement that creates a DROP TABLE command for every table found in USER_TABLES. The WHERE clause was added to eliminate some Oracle internal tables that might otherwise appear in the result set. There are also techniques available for sending the query results to a file that can then be used as a script, but they are beyond the scope of this text (see the Oracle SQL*Plus SPOOL command for details).

    SELECT 'DROP TABLE ' || TABLE_NAME ||
          
    ' CASCADE CONTRAINTS;' AS SQL
     
    FROM USER_TABLES
     
    WHERE TABLE_NAME NOT LIKE 'BIN$%';
    SQL
    -------------------------------------------
    DROP TABLE MOVIE_RENTAL CASCADE CONSTRAINTS;
    DROP TABLE CUSTOMER_TRANSACTION CASCADE CONSTRAINTS;
    DROP TABLE CUSTOMER_ACCOUNT_PERSON CASCADE CONSTRAINTS;
    DROP TABLE EMPLOYEE CASCADE CONSTRAINTS;
    DROP TABLE MOVIE_LANGUAGE CASCADE CONSTRAINTS;
    DROP TABLE MOVIE_COPY CASCADE CONSTRAINTS;
    DROP TABLE MOVIE CASCADE CONSTRAINTS;
    DROP TABLE PERSON CASCADE CONSTRAINTS;
    DROP TABLE MPAA_RATING CASCADE CONSTRAINTS;
    DROP TABLE MOVIE_GENRE CASCADE CONSTRAINTS;
    DROP TABLE LANGUAGE CASCADE CONSTRAINTS;
    DROP TABLE CUSTOMER_ACCOUNT CASCADE CONSTRAINTS;

    Generating SQL in Microsoft SQL Server

    Microsoft uses the term “system tables” for the SQL Server tables that contain metadata. You can find descriptions of them in Books Online under the topic “system tables.” Here is the SQL Server equivalent of the previous example (dropping all tables) using the SQL Server SYSOBJECTS table. The WHERE clause filters out all objects except those with the “user table” object type.

    SELECT 'DROP TABLE ' + NAME + ';'
     
    FROM SYSOBJECTS
     
    WHERE XTYPE='U'
    DROP TABLE PERSON;
    DROP TABLE MOVIE;
    DROP TABLE MOVIE_COPY;
    DROP TABLE MOVIE_LANGUAGE;
    DROP TABLE EMPLOYEE;
    DROP TABLE CUSTOMER_ACCOUNT_PERSON;
    DROP TABLE CUSTOMER_TRANSACTION;
    DROP TABLE MOVIE_RENTAL;
    DROP TABLE CUSTOMER_ACCOUNT;
    DROP TABLE LANGUAGE;
    DROP TABLE MOVIE_GENRE;
    DROP TABLE MPAA_RATING;

    NOTE: These generated DROP statements won’t run without some additional work because of the referential constraints. SQL Server doesn’t support the “CASCADE CONSTRAINTS” clause with the DROP command, so the referential constraints would have to be dropped before the tables could be, or the DROP statements would have to be placed in the proper order before being run.

    More MySQL Articles
    More By McGraw-Hill/Osborne


       · This article is an excerpt from the book "SQL DeMYSTified," published by...
     

    Buy this book now. This article is excerpted from chapter six of the book SQL DeMYSTiFied, written by Andrew Oppel (McGraw-Hill/Osborne, 2005; ISBN: 0072262249). Check it out today at your favorite bookstore. Buy this book now.

       

    MYSQL ARTICLES

    - MySQL Query Optimizations and Schema Design
    - MySQL Benchmarking Tools and Utilities
    - MySQL Benchmarking Concepts and Strategies
    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT