Foreign key may not be primary key?

4

I'm new to databases. My teacher asked us to do the linking of 3 movie charts:

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

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

What he wants:

He who wants our BD to search for the film by name (title) and to 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.

Can I have a foreign key that is not a primary key?

    
asked by anonymous 31.03.2014 / 20:37

2 answers

3

Actually what you want to do is a N to N link between 2 tables, which you already know is not possible, so the trick is to use a third table between the 2 tables that will contain the Id E of Id of another, soon 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 Ids of the Title and Category tables, the links will look like this:

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

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

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 can not say that the Title name is a foreign key because it is not a primary key. Primary keys must always be integers , the DBMS tool may even allow it to be string but this is completely wrong. Primary keys should always be values that will be repeated or close to being repeated.

    
31.03.2014 / 21:02
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 best practices of database modeling, is none of them.

The correct thing is that your associative table already binds foreign keys.

A DDL from your database would look something like this (I do not know what database technology you're using, 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%';
  

Can I have a foreign key that is not a primary key?

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

    
31.03.2014 / 20:49