fdfdfd
fdfdfd

Reputation: 501

MySQL how to make a column of child table point to the parent table?

I've just started MySQL database and i am confused with what a foreign key could do.

Suppose i have a parent table (teacher) of this and taxno is the primary key;

taxno   Tfname   Tlname   Tgender   Tquali    Thours      

1111    TOH       JIM       M       HONOURS    120

and my childtable (teacsub);

taxno   Tfname   Tlname   Tgender   Tquali    Thours  Subtitle Subtype

How do i make it that if, for example, my input for teacsub is

 taxno   Tfname   Tlname   Tgender   Tquali    Thours  Subtitle   Subtype

 1111   dddd       dddd      F       weqwe      100     HISTORY     3

it will prompmt to me that my input for teacsub is wrong?

i have created a foreign key for taxno but it only applies to taxno.

Upvotes: 0

Views: 108

Answers (1)

pala_
pala_

Reputation: 9010

The beauty of a relational db is that with properly constructed tables, you do not need to repeat information as you have been doing.

If your teacher table contains information like a persons name, gender etc, you do not need to duplicate this information anywhere else.

For example, if your teacsub table is simply teacsub(taxno, subtitle, subtype), then you can get your 'full' information by joining the two tables together with a query like this:

select *
  from teacher
    inner join teacsub
      on teacher.taxno = teacsub.taxno

And that will happily go off and pull all rows from both tables that share a common taxno

Upvotes: 2

Related Questions