Jump to content

how to insert data into relational database


midgley

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?

 

 

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.