42

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

1
  • 7
    Been googling, not really explaining it really well bud Oct 24, 2011 at 19:39

5 Answers 5

46

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.

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.

Hope this helps you understand better.

3
  • 5
    If you have multiple articles pointing to one author, wouldn't the authors table be the parent table?
    – RoToRa
    Oct 25, 2011 at 13:04
  • 2
    Is there any good book on database for beginners, I would like to know, It is much confusing when searching the books for beginners in amazon.
    – shining
    Nov 15, 2015 at 4:48
  • tldr for a given constraint the parent is the one with the unique key Mar 30 at 13:32
18

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, child records typically don't stand on 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 child records in the CARS table without a parent record in the OWNERS table - it would be nearly useless data.

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 be 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".

2
  • 4
    This should be the accepted answer, as it is the only one that explains the difference between lookup relationships and parent-child relationships. Feb 7, 2014 at 22:40
  • In the "lookup table" example, the "child" record might contain genderId pointing at a lookup table , where genderId=1 is "Male" and genderId=2 is "Female". Does that mean the lookup table is the "parent"? Interesting Salesforce article at this link echoes the idea posted here; that in a "master-child" relationship (strongly coupled), "Parent record is required in order to save a child record.", whereas in a "lookup" relationship (loosely coupled), "Parent record is not required when creating a child record." Aug 1 at 21:12
13

Parent - The entity on the "one" (/1) side of a relation with another table

Child - The entity on the "many" (/N/*) side of a relation with another table

1
  • 14
    Parent <-- Child. The child points to the parent. So each child knows who its parent is. And each child has exactly one parent. A parent entity contains no information as to who its children are.
    – Bob Stein
    May 24, 2018 at 16:19
5

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.

3

Those terms are used in database relationships.

for example u have two table,

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    |                |
+-------------+------------------+------+-----+---------+----------------+
  1. 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    |                |
+-----------------+------------------+------+-----+---------+----------------+

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 ]

1
  • Parent table has no foreign key and connect to other connect to other what?. I I mean table or fields? Sep 3, 2018 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.