简体   繁体   中英

How to store Foreign Keys via PhpMyAdmin?

In a relational database which is the best way to store a foriegn key, or relate a key, not sure how best to phrase it.

For example if you had tables:

User

  • ID
  • Name
  • Email

Email:

  • ID
  • Email

This is not a table I am actually creating just simple enough to get the idea across.

In the user table if I would create a key with the ID then when I select the user table the ID of the email is the value in the email field. I can reference the actual email field, this fixes that issue, but is that not wasteful?

I am using PHPMyAdmin to setup the databases and keys.

Any advice the best way to do this?

PhpMyAdmin provides a Graphical User Interface (GUI) to MySQL operations.

The best way to do this is databases, which I presume you will have already created, is to simply run a quick query!

MySQL looks pretty scary, but its actually really simple, once you grasp the concept of it!

By looking at your example, you'll most likely be looking to run a query like this one. (This is rough pseudo and may not run in itself)

ALTER TABLE User
ADD FOREIGN KEY (ID) REFERENCES Email(ID);

Easy peasy! :)

The best resource for finding out more about the syntax required is over on W3 Schools! This is the article you'll want to look at!

Edit

As a general database rule, I'd recommend making all your column names (fields) unique! This avoids confusion. For example, you could use UserID and EmailID .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM