English 中文(简体)
关于多个表格的协会(数据库)
原标题:About associations with multiple tables (Database)

我想在我的数据库中向城市、国家或国家提供物品。 然而,在项目表中,我只想有一个领域来与城市、州或与之相关的国家建立联系(而不是有三个条目城市:id, State_id and country_id)。 见图像:

我知道,在把桌子放在桌旁时有点,但我还是发现这种模式。

感谢很多!

J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.J.

“Database

问题回答

如你所说,你可以采用中间表格:

CREATE TABLE regions (
region_id  INT NOT NULL AUTO_INCREMENT,
country_id INT NOT NULL,
state_id   INT,
city_id    INT,
PRIMARY KEY (region_id),
UNIQUE  KEY (country_id, state_id, city_id)
CONSTRAINT FOREIGN KEY (country_id)           REFERENCES countries (id),
CONSTRAINT FOREIGN KEY (country_id, state_id) REFERENCES states (country_id, id),
CONSTRAINT FOREIGN KEY (  state_id,  city_id) REFERENCES cities (  state_id, id)
)

页: 1 以各种可能(国家、州、城市)组合更新的表格,包括国家或城市的表格<>NUL。

您是否考虑增加第五个表格:place。 每一<条码>项目均与<条码>有关。

这种状况仍然没有正常化。 你们必须努力确保三个表格中的<编码>>>>>代号/编码”数值不相干。 在将<条码>项目并入适当表格方面,你还有很多工作要做。 (当然,你会遇到另一组困难)

这里还有一个想法,告诉我你的想法。

由于“项目”领域是任择的,而且国家、州和城市之间的关系已经界定,我或许可以尝试这样:

国家、州和安营的主要支柱;城市将通过这三个表格成为独一无二的国际发展指标(前文:任何国家的国民发展体系都不会像任何城市或国家一样具有同样的主要关键地位)。

然后,我可以在“项目”表和“类型”中提出这一独特的识别资料,以备看(城市、州、国家)。

由于可能会出现一些变化(例如,区域、大陆等),因此也许最好是简单地“增加”一个新的表格和类型,而不是重新编制相关的表格。

这是否有意义?

感谢!





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签