Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 11th, 2003, 06:52 PM
vicncat vicncat is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 vicncat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Insert Into

Is there a way to use the INSERT INTO statement in SQL for more than one table? For example if you are normalizing a database and find that the data in one table needs to be placed in two tables. ....??

Reply With Quote
  #2  
Old February 11th, 2003, 08:55 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 2 m 4 sec
Reputation Power: 44
There is no such thing as an INSERT statement on two tables, but there are various ways of accomplishing your aims, depending on the database system involved.

1. Updateable views. In some database systems you can define a view, with two or more joined tables, and INSERTS or UPDATES into the view can propagate to the sub-tables. This is an iffy proposition, though. There are all kinds of quirks and limitations on view updateability, depending on which system you use.

2. More commonly this would be the job of a trigger, in most modern database systems. (If you are talking about MySQL, then you are out of luck. Actually, MySQL doesn't support any of the methods we discuss here). If you are not familiar with triggers, just think of them as an action that is "pre-set", to happen upon insert or update to a particular table. This pre-set action can be an SQL query or procedural function which carries out any operation you want, such as inserting duplicate data to another table.

3. Stored procedures. Triggers are usually set to call stored procedures. But, you can call a stored procedure directly, without a trigger. This stored procedure can easily insert the parameters you pass to several tables. Some database developers insist that ALL access to tables occur through stored procedures, so that the front-end application developer never even uses one SQL query. This gives the database admin complete flexibility to change internal schema at will, while preserving the external interface.

4. (A unique concept) Query rewrite rules, which is a concept that only exists in my favorite system: PostgreSQL. This is similar to a trigger, but aimed simply at redirecting the action of an SQL query to carry out a completely different query, or an additional query, or a series of queries, using the data from the "input" query. Think of it as being analogous to mod_rewrite for Apache, and you have the idea: anything can be something other than it seems on the outside.

If the database system you are using doesn't support any of these concepts, then you must handle the problem in your front-end application code.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #3  
Old February 12th, 2003, 03:06 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,766 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 6 h 24 m 8 sec
Reputation Power: 37
4) sounds very much like instead of triggers so I would not call it a unique concept, or is there any difference?

Reply With Quote
  #4  
Old February 12th, 2003, 03:14 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,711 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 5 h 22 m 58 sec
Reputation Power: 259
As a sidenote, Oracle 9i and maybe other databases allow some kind of multitable inserts.

Reply With Quote
  #5  
Old February 12th, 2003, 07:42 AM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 2 m 4 sec
Reputation Power: 44
swampboogie -

The RULE concept is very similar to triggers, except it uses a different mechanism internally, and (I believe) has less of a performance hit than a trigger. One difference is that it can be defined for any access to a table or view, including a SELECT. (AFAIK) Many DMBS systems only define triggers for INSERT, DELETE, and UPDATE. This makes it a great way to log all access to a particular table or view, for example. Also, it is the mechanism used to make views updateable, or even insertable, in PostgreSQL.

I guess, to illustrate better, a RULE can take a query that is illegal, such as one that inserts the wrong number of columns into a table, and turn it into a legal one, by inserting those extra columns in another table, or whatever you want. It can be a way to keep the external access to a table, while changing the internals of the table completely. Thus, it allows you to practice a small amount of black magic .

pabloj - I'm curious; what is the syntax for a multitable INSERT, in Oracle.

Really, for all of this, a simple answer to vicncat's question, which even works for MySQL, is to have two insert queries wrapped in a transaction, to preserve the integrity of the operation. Thus if one fails, they both fail.

Reply With Quote
  #6  
Old February 12th, 2003, 08:12 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,711 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 5 h 22 m 58 sec
Reputation Power: 259
Hi rycamor, straight from an Oracle whitepaper:
Quote:
Multi-table inserts offers the benefits of the INSERT . . . SELECT statement
when multiple tables are involved as targets.
.....

INSERT FIRST
WHEN cust_credit_limit >=4500 THEN
INTO customers_special VALUES(cust_id,cust_credit_limit)
INTO customers
ELSE
INTO customers
SELECT * FROM customers_new;


I've found it very useful in datawarehousing.

Reply With Quote
  #7  
Old February 12th, 2003, 09:38 AM
vicncat vicncat is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 vicncat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb

Thank you all...back to the drawing board

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Insert Into


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway