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
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.
authors
table be the parent table?
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".
lookup
relationships and parent-child
relationships.
Feb 7, 2014 at 22:40
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
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
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.
May 24, 2018 at 16:19
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.
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 | |
+-------------+------------------+------+-----+---------+----------------+
+-----------------+------------------+------+-----+---------+----------------+ | 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 ]