Mysql - Foreign Key constraint between tables

Asked

Viewed 57 times

1

Below I created 4 dummy tables for illustration

ilustração The Teacher and Student tables are referenced by Address and Documents. However, how can I create a foreign key in the last two tables referencing Teacher OR Student? If I create a key for each, can the bank be inconsistent? There is another way?

1 answer

2


Two alternatives:

1 - Create another table TB_PESSOA with id,id_address and id_document

TB_ALUNO and TB_PROFESSOR receive the id_person field

It gets easier if you want to create a third type of user, in addition to getting easier to convert to object-oriented programming logic

2 - Create another table TB_PESSOA, and move all fields from TB_ENDERTO and TB_DOCUMENTO to TB_PESSOA and make them optional. Gets less fragmented and easier to manage.

  • I didn’t even think about it haha I’ll implement and come back to mark the answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.