If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > UPDATE with two tables and FOREIGN KEY

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-04, 16:37
Lukas_Adamek Lukas_Adamek is offline
Registered User
 
Join Date: Aug 2004
Posts: 3
UPDATE with two tables and FOREIGN KEY

I have database which has two tables:
CREATE TABLE sport (idsport INT, name VARCHAR(20), PRIMARY KEY (idsport));
INSERT INTO sport (idsport, title) VALUES (1,'football'), (2,'hockey'), (3,'box');

CREATE TABLE adressbook (idperson INT, name VARCHAR(30), idsport INT, PRIMARY KEY(idperson),
FOREIGN KEY(idsport) REFERENCES sport(idsport));
INSERT INTO adressbook (idperson,name,idsport) VALUES (1,'Tom', 2);

And now
I need with one command UPDATE change row in table adressbook, that way in column idsport I enter title e.g. 'box'. I mean something like this:
UPDATE adressbook SET name='Tomas, idsport='Box' WHERE idperson=1;
Of course this doesn`t work, because idsport is FOREIGN KEY.
How make it?
Reply With Quote
  #2 (permalink)  
Old 08-15-04, 07:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
UPDATE adressbook, sport
SET adressbook.name = 'Tomas'
, adressbook.idsport = sport.idsport
WHERE adressbook.idperson=1
and sport.title='box'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On