How about changing your database model to simplify your life?!
I noticed that your problem has 4 distinct entities: Aluno
, Materia
, Turma
and Nota
.
These 4 entities can be represented as follows:
-- MATERIAS
CREATE TABLE tb_materia
(
id BIGINT PRIMARY KEY,
descricao TEXT NOT NULL
);
-- TURMAS
CREATE TABLE tb_turma
(
id BIGINT PRIMARY KEY,
descricao TEXT NOT NULL
);
-- ALUNOS
CREATE TABLE tb_aluno
(
id BIGINT PRIMARY KEY,
nome TEXT NOT NULL,
id_turma BIGINT
);
-- NOTAS
CREATE TABLE tb_nota
(
id_materia BIGINT NOT NULL,
id_aluno BIGINT NOT NULL,
UNI REAL,
UNII REAL,
UNIII REAL,
UNIV REAL,
PRIMARY KEY( id_materia, id_aluno )
);
Now, let's go to relationships:
-- ALUNOS -> TURMA (UM aluno possui NENHUMA ou APENAS UMA turma)
ALTER TABLE tb_aluno ADD FOREIGN KEY (id_turma) REFERENCES tb_turma;
-- NOTA -> ALUNO (UMA nota possui obrigatoriamente APENAS UM aluno)
ALTER TABLE tb_nota ADD FOREIGN KEY (id_aluno) REFERENCES tb_aluno;
-- NOTA -> MATERIA (UMA nota possui obrigatoriamente APENAS UMA materia)
ALTER TABLE tb_nota ADD FOREIGN KEY (id_materia) REFERENCES tb_materia;
Registering Data:
-- CADASTRO DAS MATERIAS
INSERT INTO tb_materia ( id, descricao ) VALUES ( 1, 'PORTUGUES' );
INSERT INTO tb_materia ( id, descricao ) VALUES ( 2, 'MATEMATICA' );
INSERT INTO tb_materia ( id, descricao ) VALUES ( 3, 'HISTORIA' );
-- CADASTRO DAS TURMAS
INSERT INTO tb_turma ( id, descricao ) VALUES ( 1, '3-A' );
INSERT INTO tb_turma ( id, descricao ) VALUES ( 2, '2-A' );
INSERT INTO tb_turma ( id, descricao ) VALUES ( 3, '1-B' );
-- CADASTRO DOS ALUNOS
INSERT INTO tb_aluno ( id, nome, id_turma ) VALUES ( 1, 'JOAO', 1 );
INSERT INTO tb_aluno ( id, nome, id_turma ) VALUES ( 2, 'MARIA', 1 );
INSERT INTO tb_aluno ( id, nome, id_turma ) VALUES ( 3, 'JESUS', 2 );
-- CADASTRO DE NOTAS (SOMENTE JESUS do 2-A)
INSERT INTO tb_nota ( id_aluno, id_materia, UNI, UNII, UNIII, UNIV ) VALUES ( 3, 1, 9.0, 8.5, 5.5, 1.4 );
INSERT INTO tb_nota ( id_aluno, id_materia, UNI, UNII, UNIII, UNIV ) VALUES ( 3, 2, 8.5, 4.5, 6.5, 5.2 );
INSERT INTO tb_nota ( id_aluno, id_materia, UNI, UNII, UNIII, UNIV ) VALUES ( 3, 3, 3.0, 9.5, 2.5, 9.4 );
Now let's look at the query that retrieves all Alunos
and Materias
independent of Notas
registered:
SELECT
m.descricao AS "Materia",
a.nome AS "Nome do Aluno",
t.descricao AS "Turma",
n.UNI,
n.UNII,
n.UNIII,
n.UNIV
FROM
tb_aluno AS a
CROSS JOIN
tb_materia AS m
LEFT JOIN
tb_nota AS n ON ( n.id_aluno = a.id AND n.id_materia = m.id )
LEFT JOIN
tb_turma AS t ON ( t.id = a.id_turma )
Output:
| Materia | Nome do Aluno | Turma | uni | unii | uniii | univ |
|------------|---------------|-------|--------|--------|--------|--------|
| PORTUGUES | JOAO | 3-A | (null) | (null) | (null) | (null) |
| PORTUGUES | MARIA | 3-A | (null) | (null) | (null) | (null) |
| PORTUGUES | JESUS | 2-A | 9 | 8.5 | 5.5 | 1.4 |
| MATEMATICA | JOAO | 3-A | (null) | (null) | (null) | (null) |
| MATEMATICA | MARIA | 3-A | (null) | (null) | (null) | (null) |
| MATEMATICA | JESUS | 2-A | 8.5 | 4.5 | 6.5 | 5.2 |
| HISTORIA | JOAO | 3-A | (null) | (null) | (null) | (null) |
| HISTORIA | MARIA | 3-A | (null) | (null) | (null) | (null) |
| HISTORIA | JESUS | 2-A | 3 | 9.5 | 2.5 | 9.4 |
SQLFiddle: link