How to store records in tables when they have Foreign Keys?

Asked

Viewed 29 times

1

My database has 6 tables:

--Movies table

MovieID int PK,
MovieName varchar(50)
MovieDescription text,
MovieCategory varchar(50),
MovieYear varchar(4),
ParticipationID)

--Series table

SeriesData (
SeriesID int PK,
SeriesName varchar(50),
SeriesDescription text,
SeriesCategory varchar(50),
SeriesYear varchar(4),
ParticipationId int FK);

--Actors Table

ActorsData (
ActorID int PK,
ActorName varchar(50),
ActorAge varchar(3),
ParticipationId int FK)

--Seasonsnepisodes Serves to answer the question "How many episodes do you have each season"

SeasonsEpisodes (
SerieID int PK,
SerieSeasons int,
SerieEpisodes int)

--Moviesparticipated This table is used to find out which films the actor participates in and which actors participate in a certain film

MoviesParticipated (
ParticipationId int PK
ActorID int FK
MovieID varchar(50) FK)

--Seriesparticipated This table serves to know in which films the actor participates and which actors participate in a certain film

SeriesParticipated (
ParticipationId int PK
ActorID int FK
SeriesID varchar(50) FK)

When I try to add records it gives me error because of the Foreign Keys. How I add records in tables?

1 answer

2


When there’s a field Forgeign Key the record can only be created if the value of this field is consistent with the referenced field.

Ex: Imagine a table Pedido which has a FK field IdCliente referencing Cliente.Id (field id table cliente)

You can only add in the field Pedido.IdCliente an Id that actually exists in Cliente.Id.


EDIT

Now that you posted the modeling, I could see that it’s the first option you said in the comments: need to review the modeling.

You said that MoviesParticipated serves to inform which actor participates in which film. That is, it is an intermediate table of a link n-n amid Movies and Actors. (the same goes for SeriesParticipated)

So you don’t need to reference her as FK in other tables, you can remove the FK for it

  • That is, as all tables have Foreign Keys, I can not add records. There is a problem... I’ll edit the question to see if you can help me with something

  • 1

    If there is a circular reference, you will need to review the modeling and remove one or more Fks (Ex: Tabela1 has FK pointing to table2, which has FK pointing to Tabela3, which has FK pointing to Tabela1) If it is not the case, you have to start by giving Insert in the table that does not depend on any other. Ex: Se ItemPedido depends on Pedido that depends on Cliente, has to first give Insert in the Cliente, afterward Pedido, then on ItemPedido

  • I edited the answer :)

  • So do I need to remove the FK Participationid? or take the 3?

  • 1

    You can take out the 3. The table MoviesParticipated is dependent on these others, but should not be "dependent" on anyone.

Browser other questions tagged

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