Sunteți pe pagina 1din 7

--UTILIZAREA COMENZII UPDATE SI A VEDERILOR

use dbFacultati

--1. Sa se creeze tabelul tRestantieri cu studentii ce au obtinut note mai mici decat 5

-- Rezolvare: Utilizam comanda select cu clauza into

select codSpec,A.codStud,nume,C.codCurs,denumire,nota

into tRestantieri

from tStudenti as A inner join tNote as B on A.codStud=B.codStud

inner join tCursuri as C on B.codCurs=C.codCurs

where nota<5

--2. Sa se afiseze continutul tabelului tRestantieri

select * from tRestantieri

--3. Sa se stearga continutul tabelului tRestantieri

delete tRestantieri

--4. Sa se adauge tabelului tRestantieri constrangerea primary key

alter table tRestantieri add constraint PK_rest primary key (codStud,codCurs)

--5. Sa se insereze in tabelul tRestantieri studentii restantieri

-- vom folosi comanda insert cu clauza select

insert into tRestantieri

select codSpec,A.codStud,nume,C.codCurs,denumire,nota
from tStudenti as A inner join tNote as B on A.codStud=B.codStud

inner join tCursuri as C on B.codCurs=C.codCurs

where nota<5

select * from tRestantieri

--6. Sa se stearga restantierii la OOP din fisierul tRestantieri

delete tRestantieri where codCurs='OOP'

--7. Sa se stearga continutul tabelului tRestantieri

truncate table tRestantieri

--8. Sa se adauge constrangerea check pentru coloana nota

alter table scoala.tNote add constraint check_nota check (nota between 1 and 10)

--9. Sa se mareasca notele studentului S04 cu un punct

update tNote set nota=nota+1 where codStud='S04' and nota<10

select * from tNote

--10. Sa se modifice notele studentului S04 astfel incat la OOP sa aibe nota 9 si la AG nota 8

update tNote set nota=(case when codCurs='AG' then 8

when codCurs='OOP' then 9

else nota

end)

where codStud='S04'
--11. Sa se afiseze notele studentilor de la specializarea Mate

select B.codStud,nume,codSpec,codCurs,nota from tNote as A inner join tStudenti as B on


A.codStud=B.codStud

where codSpec='Mate'

--12. Varianta 1 : Sa se mareasca notele studentilor de la mate cu un punct

update tNote set nota=nota+1 where codStud in (select codStud from tStudenti where
codSpec='Mate') and nota<10

--12. Varianta a 2-a : Sa se mareasca notele studentilor de la mate cu un punct utilizand clauza from
in comanda update

update tNote set nota=nota+1

from tStudenti as A inner join tNote as B on A.codStud=B.codStud

where codSpec='Mate' and nota<10

--13. Sa se creeze tabelul tNoteProiect cu coloanele : codStud, codCurs, si nota

create table tNoteProiect (codStud char(10) constraint fk_studproiect foreign key references
scoala.tStudenti(codStud),

codCurs char(10) constraint fk_cursproiect foreign key references


scoala.tCursuri(codCurs),

nota tinyint constraint ck_notapr check (nota


between 1 and 10),

constraint pk_proiect primary key


(codStud,codCurs))

insert into tNoteProiect

values ('S01','BD',9),('S02','BD',8),('S03','BD',10)
--14. Sa se adauge tabelului tNote coloana notaProiect

alter table scoala.tNote add notaProiect tinyint

--15. Sa se adauge constrangerea corespunzatoarea coloanei notaProiect

alter table scoala.tNote add constraint ck_notapr2 check (notaProiect between 1 and 10)

--16. Sa se actualizeze tabelul tNote cu notele la proiect memorate de la tabelul tNoteProiect

update tNote set notaProiect=B.nota

from tNote as A inner join tNoteProiect as B on A.codCurs=B.codCurs and A.codStud=B.codStud

select * from tNote

--Partea a 2-a: Utilizarea vederilor

/*1. Sa se determine mediile studentilor la nivel de specializare

Metoda 1:

Pentru rezolvare, vom crea o vedere care sa ne furnizeze mediile studentilor, care va fi folosita
apoi

pentru determinarea mediei mediilor la nivel de specializare.

*/

create view vMediiStudenti

as

select A.codSpec,denumire,B.codStud,avg(convert(decimal,nota)) as Media

from tSpecializari as A inner join tStudenti as B on A.codSpec=B.codSpec inner join tNote as C on


B.codStud=C.codStud
group by B.codStud,A.codSpec,denumire

select * from vMediiStudenti

select codSpec,denumire,avg(media) as [Media mediilor]

from vMediiStudenti

group by codSpec,denumire

/* Metoda 2:

Utilizarea in clauza from a unei subinterogari care ne furnizeaza mediile la nivel de student.

*/

select codSpec,denumire,avg(media) as [Media mediilor]

from (select A.codSpec,denumire,B.codStud,avg(convert(decimal,nota)) as Media

from tSpecializari as A inner join tStudenti as B on A.codSpec=B.codSpec inner join tNote as C on


B.codStud=C.codStud

group by B.codStud,A.codSpec,denumire) as T

group by codSpec,denumire

--2. Sa se modifice denumirea specializarii din Informatica in Mate-info prin intermediul unei vederi

--Incercam sa folosim vederea vMediiStudenti

select * from vMediiStudenti

update vMediiStudenti set denumire='Mate-Info' where codSpec='Info'

--Nu merge, deoarece vederea vMediiStudenti contine o functie de agregare

--Nu ar fi mers nici daca ar fi continut clauzele dinstinct sau group by sau campuri calculate

--Creeam o vedere vStudenti pentru exemplificarea update-ului prin intermediul vederii


create view vStudenti

as

select A.codSpec,A.denumire as [Denumire specializare],B.codStud,nume,D.codCurs,D.denumire as


[Denumire Curs],nota

from tSpecializari as A inner join tStudenti as B on A.codSpec=B.codSpec inner join tNote as C on


B.codStud=C.codStud

inner join tCursuri as D on C.codCurs=D.codCurs

select * from vStudenti

update vStudenti set [Denumire specializare]='Mate-Info' where codSpec='Info'

select * from tSpecializari

--3. In exemplul de mai jos, vom crea o vedere a carui cod este criptat

go

create view vStudenti2 with encryption

as

select A.codSpec,A.denumire as [Denumire specializare],B.codStud,nume,D.codCurs,D.denumire as


[Denumire Curs],nota

from tSpecializari as A inner join tStudenti as B on A.codSpec=B.codSpec inner join tNote as C on


B.codStud=C.codStud

inner join tCursuri as D on C.codCurs=D.codCurs

--4. Sa se creeze vederea vStudenti3 cu studentii de la info

go

create view vStudenti3

as

select * from tStudenti where codSpec='Info'


select * from vStudenti3

--5. Sa se mute studentul Florin de la Info la Mate prin intermediul vederii

update vStudenti3 set codSpec='Mate' where nume='Florin'

--6. Sa se creeze vederea vStudenti4 inpunand conditia with check option

go

create view vStudenti4

as

select * from tStudenti where codSpec='Info'

with check option

update vStudenti4 set codSpec='Mate' where codStud='S02'

S-ar putea să vă placă și