David
David

Reputation: 1

When adding data to a table, it doesn't show up in the Foreign key of the other table

I have a table COURSE which has two attributes COURSECODE (PK) and COURSENAME

I also have another table COURSEUNIT, which has three attributes COURSECODE (PK) (FK) UNITCODE (PK) (FK) and CREDIT .

When I add data to the table COURSE it doesn't add the data to the COURSEUNIT table. What is the problem?

Upvotes: 0

Views: 117

Answers (4)

HLGEM
HLGEM

Reputation: 96600

You need to write the query to populate the second table, how else will it know what values besides the FK to put in it? Child tables do not auto populate in any database.

Upvotes: 1

OTTA
OTTA

Reputation: 1081

No problem there, that's how SQL works, you'll need a second INSERT statement to populate the COURSEUNIT table.

Upvotes: 2

Samir Adel
Samir Adel

Reputation: 2499

Those are 2 different tables and you have to manage the relation, you have to insert the data first in the course table then take the id that will be generated from this insert and make another insert in the coursecode table, they will not be inserted automatically.

Upvotes: 2

Benoit
Benoit

Reputation: 79225

Foreign key does not mean that you get one row for each key in the referenced table. It only means that any row in the COURSEUNIT table must reference an existing row in COURSE.

Upvotes: 4

Related Questions