943,600 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 5256
  • MySQL RSS
Jul 10th, 2008
0

One-To-Many Relationship (adding data)

Expand Post »
I have the following table:

my_contacts
which contains the following columns:

MySQL Syntax (Toggle Plain Text)
  1. contact_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  2. frist_name VARCHAR(20),
  3. interest VARCHAR(20)

MySQL Syntax (Toggle Plain Text)
  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:

MySQL Syntax (Toggle Plain Text)
  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 ???
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
GFXtm is offline Offline
2 posts
since Jul 2008
Jul 13th, 2008
0

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

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:
MySQL Syntax (Toggle Plain Text)
  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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: IF THEN statement would not work
Next Thread in MySQL Forum Timeline: SHA1 encription





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC