Doubt about BBDD

2

At the time of relating two tables, it is used.

foreign key (atributo) references [tabla] (atributo);

The foreign key of that table is related to the primary key of the other table, but whenever I see examples, the foreign key of the table I am going to relate coincides with the primary key of the example table:

foreign key (id_cap) references jugador (id_cap);

Can I relate a table whose foreign key does not equal the primary key of the other table?

    
asked by Ángel 03.04.2017 в 16:58
source

2 answers

0

If you refer to the name of the attributes, they can be called differently, so in your case it could look like this:

foreign key (id_cap) references jugador (id);

The only drawback of having different names is that if you perform a natural join it does not work because the field of the primary key must be named just like the field < strong> foreign key , in these cases just use a inner join and indicate the names of the fields that the tables are related to.

If you mean the values that have the tuples the value of a foreign key must always exist how primary key     

answered by 03.04.2017 / 17:12
source
0

Yes, you can.

Usually one tends to use the same field names, because they identify the same data.

In some cases, the name of the table was used as a prefix to the name of the field, so it was not unusual to see things like foreign key (t1_id_cap) references jugador (t2_id_cap); , however it stopped being used.

Field names do not impact relationships, and therefore are explicitly declared. Some databases with graphic designers can suggest the relationships or try to infer them based on the field names, but this does not affect either the performance of the same or the use.

    
answered by 03.04.2017 в 17:03