My suggestion is that you remove the entire table presence.
You already have a table called pessoa_has_curso
- I suggest you create a table called pessoa_has_aula
, with FOREIGN KEYS
of pessoa
and of aula
.
I don’t have the Mysql Workbench to demonstrate, but created a reduced version of your DB.
Table curso
:
CREATE TABLE curso (
curso_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
curso_nome VARCHAR(45),
PRIMARY KEY (curso_id)
);
Table aula
:
CREATE TABLE aula (
aula_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
aula_nome VARCHAR(45),
curso_id INT UNSIGNED NOT NULL,
PRIMARY KEY (aula_id),
FOREIGN KEY (curso_id) REFERENCES curso (curso_id)
);
Table pessoa
:
CREATE TABLE pessoa (
pessoa_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
pessoa_nome VARCHAR(45),
PRIMARY KEY (pessoa_id)
);
Table pessoa_has_curso
:
CREATE TABLE pessoa_has_curso (
pessoa_id INT UNSIGNED NOT NULL,
curso_id INT UNSIGNED NOT NULL,
FOREIGN KEY (pessoa_id) REFERENCES pessoa (pessoa_id),
FOREIGN KEY (curso_id) REFERENCES curso (curso_id)
);
And now, the new table, pessoa_has_aula
:
CREATE TABLE pessoa_has_aula (
pessoa_id INT UNSIGNED NOT NULL,
aula_id INT UNSIGNED NOT NULL,
FOREIGN KEY (pessoa_id) REFERENCES pessoa (pessoa_id),
FOREIGN KEY (aula_id) REFERENCES aula (aula_id)
);
I made several insertions of people, courses and classes - but I will not put here for brevity. The placement of a pessoa
in a curso
, happens like this:
INSERT INTO pessoa_has_curso (pessoa_id, curso_id) VALUES (3, 1);
I also put pessoa
in aula
:
INSERT INTO pessoa_has_aula (pessoa_id, aula_id) VALUES (1, 6);
Now, by making the right inquiries, I believe you get them all
the information you need.
For example, list items from pessoa
who are also in curso
:
SELECT pessoa.pessoa_nome AS Pessoas, curso.curso_nome AS Cursos
FROM pessoa_has_curso
INNER JOIN pessoa
ON pessoa_has_curso.pessoa_id = pessoa.pessoa_id
INNER JOIN curso
ON pessoa_has_curso.curso_id = curso.curso_id;
Upshot:
Listing now items from pessoa
who are also in aula
:
SELECT pessoa.pessoa_nome AS Pessoas, aula.aula_nome as Aulas
FROM pessoa_has_aula
INNER JOIN pessoa
ON pessoa_has_aula.pessoa_id = pessoa.pessoa_id
INNER JOIN aula
ON pessoa_has_aula.aula_id = aula.aula_id;
Upshot:
Now, to answer your question, you said you need IDs
of
aula
in which pessoa
is participating.
As now we have a table pessoa_has_aula
, this survey is using only it, without involving the table curso
:
SELECT aula_id AS AulaID
FROM pessoa_has_aula
INNER JOIN pessoa
ON pessoa_has_aula.pessoa_id = pessoa.pessoa_id
WHERE pessoa.pessoa_nome = 'Azurelle';
Upshot:
Take a look at the SQL Fiddle.
Turn on the table
presenca
withpessoas_has_curso
that has two primary keys? So in the tablepresenca
these keys would also be primary as well as foreign and would have a primary sequence key to not duplicate values.– Giancarlo Abel Giulian
And how do I get the class ID?
– Azurelle
Foreign key without being Primary key rntre class table and presenca.
– Giancarlo Abel Giulian
by doing this in entering data in table presence appear Ids of classes that do not belong to the course in which the person is enrolled :S
– Azurelle