php 什么是数据库中的父表和子表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7880921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a Parent table and a Child table in Database?
提问by BruceyBandit
I just want to know what is a parent table and what is a child table in databases. Can you please show me an example so I understand how it works please.
我只想知道什么是父表,什么是数据库中的子表。请你给我看一个例子,让我了解它是如何工作的。
Thank You
谢谢你
回答by Martin Bean
Child tables and parent tables are just normal database tables, but they're linked in a way that's described by a parent–child relationship.
子表和父表只是普通的数据库表,但它们以父子关系描述的方式链接。
It's usually used to specify where one table's value refers to the value in another table (usually a primary key of another table).
它通常用于指定一个表的值在哪里引用另一个表中的值(通常是另一个表的主键)。
For example, imagine a news article. This could be represented by a table called articles
and has fields for id
, headline
, body
, published_date
and author
. But instead of placing a name in the author
field, you could instead put the ID value of a user in a separate table—maybe called authors
—that has information on authors such as id
, name
, and email
.
例如,想象一篇新闻文章。这可以通过代表的表中调用articles
,并具有字段id
,headline
,body
,published_date
和author
。但不是在放置一个名称author
字段,可以代替把用户的ID值在一个单独的表,也可以叫做authors
-即对作家如信息id
,name
和email
。
Therefore, if you need to update an author's name, you only need to do so in the authors
(parent) table; because the articles
(child) table only contains the ID of the corresponding author
record.
因此,如果需要更新作者姓名,只需在authors
(父)表中进行即可;因为articles
(子)表只包含对应author
记录的ID 。
Hope this helps you understand better.
希望这可以帮助您更好地理解。
回答by GilmoursBlackStrat
Be aware you can have relationships that appear to be parent-child but are not, for instance when lookup tables are being used. The distinction is that in a true parent-child relationship, records typically don't stand are their own very well - they are detail records for the parent and are not useful without the parent table info. A person can own multiple cars in the DMV database, but you wouldn't want records in the CARS table without a parent record in the OWNERS table - it would be nearly useless data.
请注意,您可以拥有看似父子但并非如此的关系,例如在使用查找表时。区别在于,在真正的父子关系中,记录通常不能很好地代表它们自己——它们是父的详细记录,如果没有父表信息,则没有用。一个人可以在 DMV 数据库中拥有多辆汽车,但您不希望 CARS 表中的记录没有 OWNERS 表中的父记录 - 这几乎是无用的数据。
On the other hand, if I am using a lookup table to expand a code to something more meaningful, or to constrain data entry to acceptable values, then the "child" record can still useful (can stand alone) if the lookup table is deleted. I could still have the sex information as "M" or "F" even if I no longer have the lookup table to expand that to "Male" or "Female".
另一方面,如果我使用查找表将代码扩展为更有意义的内容,或者将数据输入限制为可接受的值,那么如果查找表被删除,“子”记录仍然有用(可以独立) . 即使我不再拥有将其扩展为“男”或“女”的查找表,我仍然可以将性别信息设为“M”或“F”。
回答by pehur
Parent - The entity on the "one" (/1) side of a relation with another table
父级 - 与另一个表的关系的“一”(/1) 一侧的实体
Child - The entity on the "many" (/N/*) side of a relation with another table
Child - 与另一个表的关系的“多”(/N/*) 一侧的实体
回答by Marc B
A child table tends to be one where it has one or more foreign keys pointing at some other table(s). Note that a child table can itself be a parent to some OTHER table as well.
子表往往是一个或多个外键指向其他表的表。请注意,子表本身也可以是某些 OTHER 表的父表。
回答by KaungKhant MinTun
Those terms are used in database relationships.
这些术语用于数据库关系。
for example u have two table,
例如你有两张桌子,
1.Manifast
1.Manifast
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| manifast_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| description | text | NO | | NULL | |
| title | text | NO | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
- day_sequence
- day_sequence
+-----------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+------------------+------+-----+---------+----------------+ | day_sequence_id | int(11) unsigned | NO | PRI | NULL | auto_increment | | day_number | int(11) | NO | | NULL | | | day_start | int(11) | NO | | NULL | | | manifast_id | int(11) | NO | | NULL | | +-----------------+------------------+------+-----+---------+----------------+
+-----------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------------+------------------+------+-----+---------+----------------+ | day_sequence_id | int(11) unsigned | NO | PRI | NULL | auto_increment | | day_number | int(11) | NO | | NULL | | | day_start | int(11) | NO | | NULL | | | manifast_id | int(11) | NO | | NULL | | +-----------------+------------------+------+-----+---------+----------------+
if u want to connect those two tables,u need to use the command with following format.
如果你想连接这两个表,你需要使用以下格式的命令。
> ALTER TABLE child_table_name ADD FOREIGN KEY (P_ID) REFERENCES
> parent_table_name (P_ID)
and so it become.
就这样变成了。
> ALTER TABLE day_sequence ADD CONSTRAINT fk_manifast FOREIGN KEY
> (manifast_Id) REFERENCES manifast(manifast_Id);
In summary, Child table is a table which has foreign key,and is connected from others table. Parent table has no foreign key and connect to other. [ Note : This ans is just for connecting two tables ]
综上所述,子表是一个有外键的表,与其他表相连。父表没有外键并连接到其他表。[注:此 ans 仅用于连接两个表]