Jump to content

Foreign Key


Err

Recommended Posts

Hello, I'm trying to set up a foreign key on two InnoBD tables through PHPMyAdmin. I don't see any options to allow this, and the online help documents are very confusing. I was hoping to get help with this.Here's the deal, I have two tables, one named book and the other user. In book I have an id (for the book, and also is a primary key) and a UserId (for the user). I want to link the UserId in book to the UserId in the user table. This is done with a foreign key, but I don't see the option to allow this.Here are some images to get a better idea with what I'm working with:table: booktable: userboth tablesI know this can be done, but I'm not understanding it. Can someone explain how to do this?

Link to comment
Share on other sites

CREATE TABLE users (...UserId INT(10) REFERENCES user(UserId),...);

By the way, by convention field names are in lowercase, with underscores (e.g. `user_id').

Link to comment
Share on other sites

Since only InnoDB allows foreign keys, I don't think phpMyAdmin has a way to set them up through the interface (although I may be wrong, and maybe the newer versions of phpMyAdmin do have that). You may just need to run an ALTER TABLE query.

Link to comment
Share on other sites

  • 2 weeks later...

Yeah, even the latest version of PHPMyAdmin can't do it (if it can, I can't find it). But thank for your help justsomeguy. I found a way to manage the database without having do this.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...