English 中文(简体)
如何设计一个单位有 mu字的表格?
原标题:Best way to design a table in which a cell has mutiple references?

My question regards how to design a database.

I have one table, called posts, with columns:

ID, subject, keywords, (and a few other columns)

and another table called keywords with:

kw_id, keyword.

Now, each "post" has several keywords and, sometimes, keywords are deleted because they don t make sense or are duplicates.

My question is:

  • Can keywords column in table posts be a foreign key? (each row will have multiple keywords)

  • If I can t, what is the best way to ensure data integrity (specially when a keyword is deleted)?

thanks in advance

EDIT: Can you point me any books or documents I should read about database design? It seems I m laking key knowledge about database design.

最佳回答

你们必须lip: 表KEYWORD应参照员额。 您可能希望找到特定关键词(假冒代码)的所有职位。

POST
    POST_ID
    ...

KEYWORD
    KEYWORD_ID
    NAME        /* the keyword */

POSTKEYWORDREL    /* relationship */
    POST_ID     /* foreign key to POST */
    KEYWORD_ID  /* foreign key to KEYWORD */

Now you can easily delete a keyword from a given post by just removing the relationship in POSTKEYWORDREL.

EDIT:就文件而言,请允许我指出:Wikipedia 。 http://en.wikipedia.org/wiki/Database_normalization”rel=“nofollow” (我认为,数据库设计方面最重要的概念)。

问题回答

to follow your design, in the posts table you remove the column keywords.

you create then another table to make the many-to-many relationship, something like

PostKeywords

表2 Id和kw_id,两者都是本国表格的外在钥匙。

本人 我也创建了一个地方PK一栏,它没有参与到许多方面,例如,一个后关键词。 仅此表的地方自动加油。

页: 1 本表有一行显示,已指定一个特定的<>序号>。 本表的主要要点是post_id ,keyword_id。 这两栏均为主次的外国钥匙。

这是许多至男性关系的标准设计做法。

如果您的keyword items are to be presented in a particular order, 增加order栏。

如欲取得最佳结果,请在<>后表格和显示的所有其他表格中点名。 这样,各种机械化工具就能够显示你重新做些什么。





相关问题
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 ...