简体   繁体   中英

Should I reference a field id or have the actual data in the same row?

I have two tables, meetings_table and items_table .

items_table has three columns:

id
item
description

meetings_table has two columns:

first_party
second_party

I want to show the names of the two parties and the item they're meeting about. Should I add another column to meetings_table referencing the id of the item in items_table , then pull the item's name and description from there? Or should I add two columns to meetings_table , item and description and just run one query that way? Which is best in principal efficiency?

You should just reference the id of the item. This way, if you need to update anything about the item, you just do it in one location and it will propagate across all queries using that data. You almost never want to store data twice in a database. Every once in awhile you will come across a situation where unnormalized data is acceptable, but this is the exception, not the rule.

The best thing to do is to have an id for each item and add the id to the meetings_table , and query from there like

 SELECT * FROM meetings_table a, items_table b WHERE a.item_id=b.item_id

This will make item and meetings kinda independent of one another, and also saves space.

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