BruceyBandit
BruceyBandit

Reputation: 4334

What is a Parent table and a Child table in Database?

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

Upvotes: 45

Views: 143478

Answers (5)

GilmoursBlackStrat
GilmoursBlackStrat

Reputation: 201

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

Upvotes: 20

pehur
pehur

Reputation: 171

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

Upvotes: 14

KaungKhant MinTun
KaungKhant MinTun

Reputation: 51

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 ]

Upvotes: 3

Martin Bean
Martin Bean

Reputation: 39399

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.

Upvotes: 48

Marc B
Marc B

Reputation: 360762

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.

Upvotes: 5

Related Questions