Foreign key may not be primary key?

Asked

Viewed 5,898 times

4

I’m new to databases. My teacher asked us to link to 3 movie tables:

  • Titulos, with names and link to movies
  • Categorias of the film and link to the films
  • A middle table that would link category to movie title.

Then, the middle table must have two primary keys for the tables Títulos and Categorias.

What he wants:

He who wants our comic allows the search of the film by name (title) and appear the category at the same time.

What will be my foreign key: the movie link or the name? Obs: the primary key is the name and not the link.

I may have a foreign key that isn’t a primary key?

  • I think it is not a good idea that the primary key of the Titles table is the name. First because it’s a string later because there might be more than one movie with the same title.

  • what is the need for the middle table ?

  • we can see what you have so far ?

  • Up_one, I put down what I did. Thank you for trying to help.

2 answers

3


In fact what you want to do is a link N to N between 2 tables, which is already known that it is not possible, so you do the trick of using a third table between the 2 tables that will contain the Id and the Id of another, so you would have something like this:

TABELA TITULO:
IdTitulo inteiro CHAVE PRIMARIA,
Nome string,
Link string,

TABELA CATEGORIA:
IdCategoria inteiro CHAVE PRIMARIA,
Nome string,
Link string,

TABELA TITULO_CATEGORIA:
IdTituloCategoria inteiro CHAVE PRIMARIA,
IdTitulo inteiro CHAVE ESTRANGEIRA,
IdCategoria inteiro CHAVE ESTRANGEI

RA

In the Table we have the Id of the tables Title and Category, the links will be like this:

Titulo  1---------------N TituloCategoria N-------------1 Categoria

An example of how a link looks like this (the tables are different but the case is identical to yours):

Exemplo

I must remind you that every foreign key is a primary key as well, and every primary key can be a foreign key from another table. You cannot say that the Title name is a foreign key because it is not a primary key. Primary keys should always be integer, DBMS tool can even allow it to be a string but this is completely wrong. Primary keys should always be values that never will repeat themselves or pass close to repeat themselves.

  • 3

    This is half right. Primary keys do not necessarily need to be integers. Suffice it to be values that do not repeat, that is, identify a record only (see set theory).

  • So, as I said "the DBMS tool may even allow it to be a string but this is completely wrong". It is possible, but every professional knows that PK needs to be a unique, self-improved integer ID.

  • Wrong again. There are several approaches, one of them being the table indexed by a string with reverse index, which has no performance problem (unlike the simple use of string). There is also the use of UUIDs, or else Guids, which are hexadecimal strings replacing the whole numbers, with the advantage that there are no sequences or IDENTITIES for control of primary key generation.

  • 3

    Primary keys need not necessarily be of the whole type. The recommendation for primary keys is, the smaller (the number of characters) the better.

  • Gypsy, wanting to use a GUID for a table that will have an astronomical number of Inserts is not at all feasible, is that Marcos Xavier said, the recommendation is the smaller the better. Guids are recommended for very specific cases.Using GUID has the following consequences: The large data type increases the size of the clustered index, which can adversely affect common operations such as junctions. The unordered generation of Guids causes lines to be inserted at random locations in the clustered index, which may affect the I/O required to query the underlying table.

2

What will be my foreign key: the movie link or the name? Obs: the primary key is the name and not the link.

The right answer, considering the standards and good practices of database modeling is: none of them.

The correct is that your associative table already makes this connection of foreign keys.

A DDL of your database would be something like this (I don’t know what database technology you use, so I’m using something close to SQL ANSI):

create table Titulos {
    TituloId int primary key auto increment,
    NomeTitulo varchar(255) not null
};

create table Categorias {
    CategoriaId int primary key auto increment,
    NomeCategoria varchar(255) not null
};

create table TitulosAssocCategorias {
    TitulosAssocCategoriasId int primary key auto increment,
    TituloId int not null,
    CategoriaId not null
};

alter table TitulosAssocCategorias 
add constraint TitulosAssocCategorias_Titulo_FK foreign key (TituloId) references Titulos (TituloId);

alter table TitulosAssocCategorias 
add constraint TitulosAssocCategorias_Categoria_FK foreign key (CategoriaId) references Categorias (CategoriaId);

Having this, the following select brings the data together:

select tc.*, c.*, t.*
from TitulosAssocCategorias tc
inner join Categorias c on c.CategoriaId = tc.CategoriaId
inner join Titulos t on t.TituloId = tc.TituloId
where t.NomeTitulo like '%Título de um Filme%';

I may have a foreign key that isn’t a primary key?

No. A foreign key is necessarily a primary key from another table.

  • 1

    ta very facil asim ! na boa !

  • our, thank you very much. I managed to understand a little. rsr

  • @Did Andressa like the answer? Please mark the answer as accepted for the benefit of the community (it is the V-shaped icon below the reply score). Thank you!

  • I believe that it would be better to explain to her how to do (even if it were with other examples) than to already give the complete answer...this does not encourage learning.

Browser other questions tagged

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