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 four entities may 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 we go to the consultation that recovers all the 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 )
Exit:
| 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: http://sqlfiddle.com/#! 17/cd898
You want to filter the STUDENTS who have CLASS and SUBJECT registration but have NO GRADE ? Then to insert note, you already know ? Just need the select ?
– rbz
I actually want to insert in the table note the student and the subject if there are not both together in the table note, but I’m having doubts just how to check this.Example in the table has student 2 and subject 1, but there is student 2 and subject 2, then he will show that he does not have this subject for this student
– Daniel Alencar Souza
Note table ?
– Rovann Linhalis
I was going to say exactly that. Missing you post the note table, where you have the necessary links.
– rbz
In the table note has: ID | student | materia | UNI | UNII | UNIII | UNIV ai need only insert the student and the subject, the remainder can be null
– Daniel Alencar Souza