简体   繁体   中英

Mysql tables link with each other

I will create 3 tables in mysql:

Movies: id-name-country
Tv-Series: id-name-country
Artists: id-name-country

Instead of entering country information into these tables seperately, i am planning to create another table:

Countries: id-country

And i will make my first three tables take country data from Countries table. (So that, if the name of one country is misspelled, it will be easy just to correct in one place. Data in other tables will be updated automatically.

  • Can i do this with "foreign keys"?
  • Is this the correct approach?

Your approach so far is correct, ONLY IF by "country" in Tv-Series and Artist you mean country ID and NOT a value. And yes you can use foreign keys (country id in tv-series and artist is a foreign key linking to Countries);

Edit: Side note: looking at your edit I feel obliged to point out that If you are planning to link Movie/TV-Show with artist you need a 4th table to maintain normalization you've got so far.

Edit2: The usual way to decide whether you need tables is to check what kind of connection 2 tables or values have.

  1. If it's 1 to many (like artist to country of origin), you are fine.
  2. If you have Many to many, like Movie with Artist where 1 artist can be in multiple movies and 1 movie can have multiple artists you need a linking table.
  3. If you have 1 to 1 relation (like customer_ID and passport details in a banking system, where they could be stored separately in customer and Passport tables, but joining them makes more sense because a banks only hold details of 1 valid passport for each customer and 1 passport can only be used by 1 person) you can merge the tables (at the risk of not meeting Normalization 3 criteria)

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