One-To-Many Relationship (adding data)

Thread Solved

Join Date: Jul 2008
Posts: 2
Reputation: GFXtm is an unknown quantity at this point 
Solved Threads: 0
GFXtm GFXtm is offline Offline
Newbie Poster

One-To-Many Relationship (adding data)

 
0
  #1
Jul 10th, 2008
I have the following table:

my_contacts
which contains the following columns:

  1. contact_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  2. frist_name VARCHAR(20),
  3. interest VARCHAR(20)

  1. -----------+-----------+----------
  2. contact_id | fisrt_name | interest +
  3. ----------------------------------
  4. 1 | smith | swim +
  5. 2 | mark | dance +
  6. ----------------------------------

I need to move the interests column and make it into a CHILD table:

Here's the code to do that:

  1. CREATE TABLE interests
  2. (
  3. int_id INT NOT NULL AUTO_INCREMENT PRIMARY_KEY,
  4. interest VARCHAR(20) NOT NULL,
  5. contact_id INT NOT NULL,
  6. CONSTRAINT my_contacts_contact_id_fk
  7. FOREIGN KEY (contact_id)
  8. REFERENCES my_contacts (contact_id)
  9. );

Since I can't add information to the child table because of CONSTRAINT how am I goona move the interests from my_contacts to my new table interests so they have a one-to-many relationship ???
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: One-To-Many Relationship (adding data)

 
0
  #2
Jul 13th, 2008
Hi

There is a function LAST_INSERT_ID() what returns the last-created auto-increment value. So first insert parent data what creates a new auto-increment ID. Then insert row in child table where LAST_INSERT_ID() function is put in the values list of the insert statement, for example:
  1. -- first insert parent row
  2. INSERT INTO my_contacts (first_name) VALUES ('GFXtm')
  3. -- this generates a new value for auto-increment contactID
  4.  
  5. -- Now insert child row
  6. INSERT INTO interests (interest, contactID) VALUES ('databasing', LAST_INSERT_ID())
  7. -- this will assign the just generated auto-increment to foreign key contactID
First time, there is no interference between the auto-increment values of both tables: In Mysql all values of the values list will be inserted first. Finally the new auto-increment of table interests will be generated which then become last-inserted ID. That also means that you cannot use above insert statement, if you want to insert multiple children to same parent. For this case you must store the LAST_INSERT_ID() value from parent table into a variable.

krs,
tesu
Information is moving—you know, nightly news is one way, of course, but it's also moving through the blogosphere and through the Internets. I promise you I will listen to what has been said here, even though I wasn't here. Ann and I will carry out this equivocal message to the world. I'm the master of low expectations.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MySQL Forum
Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC