Jump to content

Recommended Posts

hi, im hoping somebody on this forum might be able to shed some light on how to insert data into a relational database

 

heres how i have my database set up:

https://cid-b405ca050a85fc76.skydrive.live.com/self.aspx/New%20folder/relationhips.jpg

 

so how would i go about inserting a new photo and linking it to an existing user?

 

this for instance doesnt work

INSERT INTO photos (photoName) VALUES ('dog') WHERE users.userId = '1'

 

can anybody help me out on this one?

 

 

USERS(userId)

 

USERSPHOTOS(userId,photoId)

 

PHOTOS(photoId)

 

i can insert users and i can insert photos but i dont understand how to go about associating a photo with a user. the foreign keys in the USERSPHOTOS table are always empty

so i have to manually insert data into the USERSPHOTOS table then. like this:

-insert photoname into photos (which would auto increment photoId)

-then retrive the user id from users

-then insert the userid and photoid into the link table called USERSPHOTOS

 

can you let me know if this is the right way because i was thinking that mysql let you insert data into multiple tables all with one mysql_query thus letting mysql update the USERPHOTOS table for me.

Sounds like your confusing foreign keys a bit. They only have two cascading features. ON UPDATE and ON DELETE.  There is not ON INSERT and therefore your idea won't work inherently. You can accomplishes this while placing the burden on MySql by using a trigger.

 

See:

http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

 

you spelled it out to me there, i was thinking that mysql was capable of cascading updates and thus saving me from typing out more querys. so as i understand it then, using mysql as a relational database is more about setting constraints than actually shortoning the code required to carry out a particular job. might look into this trigger thing but for now im going to keep things simple.

 

so mysql_insert_id will retreive the last id inserted into the database.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.