Você está na página 1de 6

Nome: Rodolfo José Moreira / João Victor de Araújo Martins / Kaue

Eduardo Frezzato / Rubia Moreira


Atividade de Banco de Dados
Exercício 1
create table Aluno (
RA int identity(1,1) primary key,
Nome varchar(100),
Cidade varchar(40)
);

create table Disciplina (


ID int identity(1,1) primary key,
Disciplina_Nome varchar(100),
CargaHoraria decimal(3,1)
);

create table Professor (


ID_Prof int identity(1,1) primary key,
Nome varchar(100),
Cidade varchar(40)
);

create table Historico (


CodigoHistorico int identity(1,1),
AlunoRA int,
DisciplinaID int,
ProfessorID_Prof int,
Semestre int,
Nota decimal(3,1),
Faltas int,
primary key (CodigoHistorico, AlunoRA, DisciplinaID, ProfessorID_Prof),
foreign key (AlunoRA) references Aluno(RA),
foreign key (DisciplinaID) references Disciplina(ID),
foreign key (ProfessorID_Prof) references Professor(ID_Prof)
);

Exercício 2
insert into Aluno(Nome, Cidade) values
('João', 'Mogi Mirim'),
('Kaue', 'Santo Antônio de Posse'),
('Pedro', 'Campinas'),
('Rodolfo', 'Mogi das Cruzes'),
('Rubia', 'Mogi Guaçu'),
('Jéssica', 'São Paulo'),
('Márcia', 'Rio de Janeiro'),
('Leandro', 'Itapira'),
('Lucas', 'Santo André'),
('Henrique', 'Conchal');

insert into Disciplina(Disciplina_Nome, CargaHoraria) values


('Banco de Dados', 2),
('Sistemas Operacionais', 4),
('Rede de Computadores', 5),
('Estrutura de Dados', 10);

insert into Professor(Nome, Cidade) values


('Prof. Sandro', 'Mogi Mirim'),
('Prof. Rita', 'Mogi Guaçu'),
('Prof. Nava', 'São Paulo');

insert into Historico(AlunoRA, DisciplinaID, ProfessorID_Prof, Semestre, Nota,


Faltas) values
(1, 1, 1, 2, 6.5 , 2),
(2, 4, 3, 1, 4.0 , 5),
(3, 3, 2, 1, 7.2 , 6),
(4, 2, 1, 2, 7.5 , 4),
(5, 1, 2, 2, 5.0 , 0),
(6, 2, 2, 2, 2.5 , 3),
(7, 4, 3, 1, 4.7 , 1),
(8, 3, 1, 2, 6.2 , 5),
(9, 1, 1, 2, 9.0 , 6),
(7, 2, 3, 2, 6.7 , 2),
(5, 3, 2, 1, 9.0 , 3),
(2, 4, 2, 2, 2.5 , 5),
(6, 1, 1, 2, 3.2 , 2),
(7, 2, 3, 1, 8.5 , 0),
(10, 4, 1, 2, 10.0 , 7);

select * from Aluno;


select * from Disciplina;
select * from Professor;
select * from Historico;

Exercício 3
select Aluno.Nome, Aluno.RA from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Disciplina.Disciplina_Nome = 'Banco de Dados' and Historico.Semestre = 2
and Historico.Nota < 5;

Exercício 4
alter table Historico add Ano int;

Exercício 5
update Historico set Ano = case
when CodigoHistorico = 1 then 2020
when CodigoHistorico = 2 then 2020
when CodigoHistorico = 3 then 2020
when CodigoHistorico = 4 then 2020
when CodigoHistorico = 5 then 2020
when CodigoHistorico = 6 then 2020
when CodigoHistorico = 7 then 2020
when CodigoHistorico = 8 then 2020
when CodigoHistorico = 9 then 2020
when CodigoHistorico = 10 then 2020
when CodigoHistorico = 11 then 2020
when CodigoHistorico = 12 then 2020
when CodigoHistorico = 13 then 2020
when CodigoHistorico = 14 then 2020
when CodigoHistorico = 15 then 2020
else 2020
end;

Exercício 6
select Professor.Nome from Professor
join Historico on Professor.ID_Prof = Historico.ProfessorID_Prof
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Disciplina.Disciplina_Nome = 'Banco de Dados' and Historico.Ano = 2020;

Exercício 7
select Professor.Nome, count(distinct Disciplina.ID) as QuantidadeDisciplinas
from Professor
join Historico on Professor.ID_Prof = Historico.ProfessorID_Prof
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Historico.Ano = 2020 group by Professor.Nome;

Exercício 8
select Aluno.Nome, Aluno.Cidade, Disciplina.ID, Disciplina.Disciplina_Nome from
Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Historico.Semestre = 1 and Historico.Ano = 2020 and Historico.Nota < 5;

Exercício 9
select Aluno.Nome, Aluno.RA from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Disciplina.Disciplina_Nome = 'Estrutura de Dados' and Professor.Nome =
'Marcos' and Historico.Ano = 2019;

Exercício 10
select Aluno.RA, Aluno.Nome, Disciplina.Disciplina_Nome, Historico.Faltas,
Historico.Nota, Historico.Ano, Historico.Semestre from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Aluno.Nome = 'Alex';

Exercício 11
select Nome from Professor
where Cidade = 'Mogi Mirim';

Exercício 12
select Aluno.Nome as Nome_Aluno, Disciplina.Disciplina_Nome as Nome_Disciplina,
Professor.Nome as Nome_Professor from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Disciplina.CargaHoraria < 60;

Exercício 13
select Professor.Nome from Professor
join Historico on Professor.ID_Prof = Historico.ProfessorID_Prof
join Aluno on Aluno.RA = Historico.AlunoRA
where Aluno.Nome = 'Pedro Paulo Cunha' and Historico.Nota < 5;

Exercício 14
select Aluno.RA, Aluno.Nome from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Professor.Nome = 'Sandro';
Exercício 15
select Aluno.RA, Aluno.Nome, avg(Historico.Nota) as MediaNotas from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Professor.Cidade = 'Mogi Mirim'
group by Aluno.RA, Aluno.Nome
having count(distinct Disciplina.ID) = (
select count(distinct Disciplina.ID) from Disciplina
join Historico on Disciplina.ID = Historico.DisciplinaID
join Professor on Professor.ID_Prof = Historico.ProfessorID_Prof
where Professor.Cidade = 'Mogi Mirim'
);

Exercício 16
select count(distinct AlunoRA) as Numero_Alunos from Historico
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Disciplina.Disciplina_Nome in ('Banco de Dados', 'Estrutura de Dados') and
Historico.Ano = 2020 and Historico.Semestre = 1;

Exercício 17
select Disciplina.Disciplina_Nome, avg(Historico.Nota) as MediaNotas from
Historico
join Disciplina on Disciplina.ID = Historico.DisciplinaID
group by Disciplina.Disciplina_Nome
order by MediaNotas desc;

Exercício 18
select Aluno.Nome, Aluno.Cidade, Disciplina.ID, Disciplina.Disciplina_Nome from
Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Historico.Semestre = 1 and Historico.Ano = 2020 and Historico.Nota > 5
order by Disciplina.Disciplina_Nome;

Exercício 19
select Aluno.RA, Aluno.Nome, Disciplina.ID, Disciplina.Disciplina_Nome,
Historico.Faltas, Historico.Nota, Historico.Ano, Historico.Semestre from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Aluno.Nome = 'Alex';

Exercício 20
select count(*) as Quantidade from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Disciplina.ID = Historico.DisciplinaID
where Aluno.Nome = 'José da Silva' and Disciplina.Disciplina_Nome = 'Banco de
Dados';

Exercício 21
select Ano, count(distinct AlunoRA) as Quantidade_Alunos from Historico
join Disciplina on Disciplina.ID =Historico.DisciplinaID
where Disciplina.Disciplina_Nome = 'Banco de Dados' and (Ano = 2019 or Ano =
2020)
group by Ano;
Exercício 22
select Aluno.Nome, Aluno.RA from Aluno
join Historico as BD on Aluno.RA = BD.AlunoRA
join Disciplina as D1 on BD.DisciplinaID = D1.ID
join Professor as P1 on BD.ProfessorID_Prof = P1.ID_Prof
left join Historico as TBD on BD.AlunoRA = TBD.AlunoRA
and TBD.Ano = 2018
and TBD.DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Tópicos em Banco de Dados')
and TBD.ProfessorID_Prof = BD.ProfessorID_Prof
where D1.Disciplina_Nome = 'Banco de Dados' and BD.Ano = 2019 and BD.Nota > 5 and
TBD.CodigoHistorico is null;

Exercício 23
update Historico set Nota = case
when DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Banco de Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome =
'Sandro')
and Nota >= 4.0 and Nota < 5.0 then 4.0
when DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Banco de Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome =
'Sandro')
and Nota >= 5.0 and Nota <= 9.5 then Nota + 0.5
when DisciplinaID = (select ID from Disciplina where Disciplina_Nome =
'Banco de Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome =
'Sandro')
and Nota > 9.5 then 10.0
else Nota
end
where DisciplinaID = (select ID from Disciplina where Disciplina_Nome = 'Banco de
Dados')
and Ano = 2019
and ProfessorID_Prof = (select ID_Prof from Professor where Nome = 'Sandro');

Exercício 24
select Aluno.Nome as NomeAluno, Disciplina.Disciplina_Nome, Historico.Faltas,
Historico.Nota,
case
when Historico.Nota >= 7.0 then 'Aprovado'
else 'Reprovado'
end as Situacao from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Historico.DisciplinaID = Disciplina.ID;

Exercício 25
select Disciplina.Disciplina_Nome as Disciplina, avg(Historico.Nota) as Media
from Aluno
join Historico on Aluno.RA = Historico.AlunoRA
join Disciplina on Historico.DisciplinaID = Disciplina.ID
where Historico.Nota < 5.0
group by Disciplina.Disciplina_Nome;
Exercício 26
update Historico set Nota = Nota + 0.5
where DisciplinaID = (select ID from Disciplina where Disciplina_Nome = 'Banco de
Dados');

Exercício 27
create table CIDADE (
ID_Cid int identity(1,1) primary key,
Cidade_Nome varchar(40)
);

alter table Aluno


add ID_Cid int;

update Aluno set ID_Cid = (


select ID_Cid from CIDADE
where Cidade_Nome = Aluno.Cidade
);

alter table Aluno


drop column Cidade;

Você também pode gostar