I have the following tables: conteúdo
, aulas
, curso
, turma
, grupo_User
where the logic will be as follows:
If I am right, these relationships are all N: N , I also know that one more table is needed to bind the tables.
Below I'll leave the structure of the tables:
Content:
CREATE TABLE conteudo (
id INT(12) PRIMARY KEY,
autor VARCHAR(50),
conteudo MEDIUMTEXT,
data_criacao DATETIME,
ultima_edicao VARCHAR(50),
data_edicao DATETIME
);
Classrooms:
CREATE TABLE aulas (
id INT(12) PRIMARY KEY,
autor VARCHAR(50),
nome VARCHAR(50),
data_criacao DATETIME,
ultima_edicao VARCHAR(50),
data_edicao DATETIME
);
Course:
CREATE TABLE curso (
id INT(12) PRIMARY KEY,
autor VARCHAR(50),
nome VARCHAR(50),
data_criacao DATETIME,
ultima_edicao VARCHAR(50),
data_edicao DATETIME
)
Classes:
CREATE TABLE turmas(
id INT(12) PRIMARY KEY,
autor VARCHAR(50),
nome VARCHAR(50),
data_criacao DATETIME,
ultima_edicao VARCHAR(50),
data_edicao DATETIME
)
Grupo_User:
CREATE TABLE grupo_User(
id INT(12) PRIMARY KEY,
nome_Grupo VARCHAR(50),
id_Users INT(20)
)
I even managed to link the content to classes using the following table:
CREATE TABLE aula_has_conteudo (
idAula INT(12),
idConteudo INT(12),
CONSTRAINT PK_aulas_has_conteudo PRIMARY KEY (idAula, idConteudo),
CONSTRAINT FK_aulas_has_conteudo_aula FOREIGN KEY (idAula) REFERENCES aulas (id),
CONSTRAINT FK_aulas_has_conteudo_conteudo FOREIGN KEY (idConteudo) REFERENCES conteudo (id)
);
Using the following SELECT
I can get the data you want:
SELECT a.autor as autorAula, c.conteudo as conteudo FROM aulas a INNER JOIN aula_has_conteudo h ON a.id = h.idAula
INNER JOIN conteudo c ON c.id = h.idConteudo
And I was able to bind the aulas
to the cursos
using the following table:
CREATE TABLE curso_has_aula (
idCurso INT(12),
idAula INT(12),
CONSTRAINT PK_curso_has_aula PRIMARY KEY (idCurso, idAula),
CONSTRAINT FK_curso_has_aula_Curso FOREIGN KEY (idCurso) REFERENCES curso (id),
CONSTRAINT FK_curso_has_aula_Aula FOREIGN KEY (idAula) REFERENCES aulas (id)
)
But I can not connect between the other tables.
How can I relate all of these tables knowing the above requirements?
After related, how can I make a select
to get data from the five tables?