Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Closed Thread
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:
  #1  
Old November 9th, 2005, 06:02 AM
patrick.gill's Avatar
patrick.gill patrick.gill is offline
Sleepwalker
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: The Tangent Universe
Posts: 386 patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 8 h 36 m 56 sec
Reputation Power: 51
Send a message via Skype to patrick.gill
Adding column and populating from existing FK

Hello everyone. I hope someone can help me with this one. I have three tables:

(pseudo-code)

TABLE PROJECTS {
COLUMN id
COLUMN company FK to COMPANIES.ID
...
}

TABLE COMPANIES {
COLUMN id
...
}

TABLE COST_ITEMS {
COLUMN id
COLUMN project FK to PROJECTS.ID
...
}

In the last table COST_ITEMS I want to add a new column with a foreign key to COMPANIES (I know, I know, but apparently now it CAN be assigned to companies other than the project's). So, how do I auto-populate the new column with the value currently associated to the project? I'm doing something like:

Code:
ALTER TABLE COST_ITEMS ADD company NUMBER;

UPDATE COST_ITEMS SET company = (
    SELECT com.id FROM COMPANIES com, PROJECTS pro, COST_ITEMS cst
     WHERE cst.project = pro.id AND pro.company = com.id);

ALTER TABLE COST_ITEMS MODIFY company NUMBER NOT NULL;


Obviously the UPDATE statement isn't working, but I have no idea how to do it. Can anyone help?

Reply With Quote
  #2  
Old November 9th, 2005, 08:55 AM
shammat shammat is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 1,043 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 6 h 52 m 33 sec
Reputation Power: 68
Haven't tested it, but shouldn't that be:
Code:
UPDATE cost_items
   SET company = (SELECT com.id
                  FROM companies com,
                       projects pro
                  WHERE cost_items.project = pro.id
                  AND   pro.company = com.id);
Note that I left out the cost_items from the correlated sub-query. If the sub-query could potentially return more then one row, you'll need a max(com.id) insteadd to ensure that it's a single row query.
Comments on this post
patrick.gill agrees: Yes! You are completely right! Just what I wanted

Reply With Quote
  #3  
Old November 9th, 2005, 09:27 AM
patrick.gill's Avatar
patrick.gill patrick.gill is offline
Sleepwalker
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: The Tangent Universe
Posts: 386 patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level)patrick.gill User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 8 h 36 m 56 sec
Reputation Power: 51
Send a message via Skype to patrick.gill
Quote:
Originally Posted by shammat
Note that I left out the cost_items from the correlated sub-query. If the sub-query could potentially return more then one row, you'll need a max(com.id) insteadd to ensure that it's a single row query.


No, that's correct - the project->legal entity and cost item->project relations are 1 to 1, so it will always return a single row.

Thank you so much for that!

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsDatabasesOracle Development > Adding column and populating from existing FK


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 3 hosted by Hostway
Stay green...Green IT