Relationship model and entity

Asked

Viewed 84 times

0

Guys, I’m setting up a website that has the artist chart and an artist can play more than one musical genre, as I would put in the database the genres that the artist plays ?

  • It will be necessary to create a third table to mediate the relationship of many to many. Where the artist may have one or more genres and a genre may have none or several artists. https://supporte.scriptcase.com.br/pt-br/article/598-relatedment-n-para-n

  • Thanks Thiago, just one question, in the third table will not have primary key ? Only the two foreign ?

  • @Thiagopires ....

  • I’m preparing an example to show you, in fact the third table will have a composite primary key.

1 answer

1


Here are the tables I created to illustrate.

Please disregard the names, since I did a little running.

Here I create my artist table

CREATE TABLE artistaMusica(
id_artista SERIAL,
nome VARCHAR(10),
 PRIMARY KEY(id_artista)
)

Here the genres

CREATE TABLE generoMusica(
id_genero SERIAL,
nome_genero VARCHAR(20),
PRIMARY KEY(id_genero)
)

Here the table responsible for making the link between the tables

CREATE TABLE genero_artistaMusica(
id_artista INT,
id_genero INT,
CONSTRAINT pk_genero_artista_id_artista PRIMARY KEY(id_artista,id_genero),

 FOREIGN KEY (id_genero)
REFERENCES  generoMusica(id_genero),

  FOREIGN KEY (id_artista)
REFERENCES  artistaMusica(id_artista)

)

Some points:

  1. The relationship I created would be like this, an artist can have no or several genres, and a genre can have no or several artists.

    1. That’s why I created a composite Primary Key, to avoid duplicate data, the kind that the same artist is inserted twice with the same genre.
  • Very good, thank you, now is trying to put foreign key in phpmyadmin, kkk

  • Opa, vlw. Here’s a video that although in English, I think it can help https://www.youtube.com/watch?v=A42FG4LzdbY

Browser other questions tagged

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