Jump to content

Archived

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

lpxxfaintxx

Foreign Keys

Recommended Posts

If I'm not mistaken, MySQL does not support foreign keys. Are there any alternatives to it, and if there is will you please enlighten me with tutorials and links?

Regards,
-lpxxfaintxx

Share this post


Link to post
Share on other sites
foreign keys ARE supported in mysql. check out [a href=\"http://dev.mysql.com/doc/refman/4.1/en/ansi-diff-foreign-keys.html\" target=\"_blank\"]this page[/a] in the mysql manual for more information, but basically, you'd want to set one up in the creation of your table like this:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]CREATE TABLE posts(
id int(11) auto_increment primary key,
user_id int(11) REFERENCES users(id) ON [span style=\'color:blue;font-weight:bold\']DELETE[/span] CASCADE
);
[!--sql2--][/div][!--sql3--]

when you use the REFERENCES keyword, you are creating a foreign key constraint which will allow you to run cascades and on update and on delete queries.

Share this post


Link to post
Share on other sites
[!--quoteo(post=352215:date=Mar 6 2006, 02:55 PM:name=lpxxfaintxx)--][div class=\'quotetop\']QUOTE(lpxxfaintxx @ Mar 6 2006, 02:55 PM) [snapback]352215[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Err... what if you don't have InnoDB? And what [i]is[/i] InnoDB?
[/quote]

How do you know if you dont have InnoDB if you dont know what InnoDB is ;) ?

[a href=\"http://dev.mysql.com/doc/refman/5.0/en/innodb.html\" target=\"_blank\"]Find more about InnoDB here[/a]

just FYI InnoDB is installed by default.

Share this post


Link to post
Share on other sites
if you have MySQL installed, you have InnoDB:
[quote]
Starting from MySQL 3.23.44, InnoDB features foreign key constraints.
[/quote]

if you want to get really technical about what the engine actually is, you can read more about it at [a href=\"http://www.innodb.com/\" target=\"_blank\"]http://www.innodb.com/[/a].

suffice to say (for all intents and purposes), it is the engine that handles your queries and constraints for you. it basically a way of storing the data. the alternative i'm most familiar with in mysql is MyISAM, and if you're running MySQL 5+, you should have FOREIGN KEY constraints in that format as well.

you're actually treading into an area that you'll probably get as much help for directly from the manual or google as you will from most users on here. ;-)

Share this post


Link to post
Share on other sites
[!--quoteo(post=352221:date=Mar 6 2006, 04:09 PM:name=lpxxfaintxx)--][div class=\'quotetop\']QUOTE(lpxxfaintxx @ Mar 6 2006, 04:09 PM) [snapback]352221[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hah, thanks for the extremely helpful replies guys! ;)
[/quote]

lol... glad it was helpful ;-)... we're getting into an area where i feel like i'm spewing out all sorts of references, but i don't know that i could be much more informative than what i already wrote, so i'm glad it helped

Share this post


Link to post
Share on other sites

×

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.