0% found this document useful (0 votes)
49 views4 pages

Laborator Baze de Date

This document contains Transact-SQL code that defines triggers and functions to validate student data across multiple tables in a school database. It includes triggers to validate CNP numbers on insert/update of students, maintain average grades when notes are modified, and handle modifications made to a view that combines data from the students and RMU tables.

Uploaded by

Husker Dou
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Laborator Baze de Date

This document contains Transact-SQL code that defines triggers and functions to validate student data across multiple tables in a school database. It includes triggers to validate CNP numbers on insert/update of students, maintain average grades when notes are modified, and handle modifications made to a view that combines data from the students and RMU tables.

Uploaded by

Husker Dou
Copyright
© Public Domain
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

alter function validareCNP (@CNP char(13)) returns int as begin

declare @pondere char(12) = '279146358279'


declare @s int = 0, @i int = 1
declare @cc int

while @i < 13 begin


if substring(@CNP, @i, 1) not between '0' and '9'
return 0
set @s += convert(int, substring(@CNP, @i, 1)) *
convert(int, substring(@pondere, @i, 1))

set @i += 1
end

set @cc = @s % 11
if @cc = 10
set @cc = 1

if substring(@CNP, 13, 1) not between '1' and '9'


return 0

if @cc != convert(int, substring(@CNP, 13, 1))


return 0

if convert(int, substring(@CNP, 4, 2)) not between 1 and 12


return 0
if convert(int, substring(@CNP, 6, 2)) not between 1 and 31
return 0

return 1
end

print dbo.validareCNP('1941217070217')

select * from tStudenti

alter trigger scoala.tg_validareCNP on scoala.tStudenti


for insert, update
as begin
if not update(CNP)
return

declare crs cursor for select CNP from inserted

declare @cnp char(13)


declare @ok int = 1

open crs
fetch next from crs into @cnp

while @@fetch_status = 0
begin
if dbo.validareCNP(@cnp) = 0 begin
print @cnp + ' CNP eronat'
set @ok = 0
end

fetch next from crs into @cnp


end
if @ok = 0 begin
rollback
print 'Actualizare cu esec, toate modificarile au fost anulate'
end
close crs
deallocate crs
end

select * from tStudenti


update tStudenti set CNP = '1111111111111' where codStud = 'CIG001'

insert into tStudenti (codStud, nume, CNP, codJudet, localitate, codSpec) values
('1001', 'Alina', '1941217070217', 'AG', 'Paduresti', 'info'),
('1000', 'Alina', '6030817416006', 'AG', 'Paduresti', 'info')

select * from tStudenti


select * from tNote

alter trigger scoala.tg_Actualizare_medie


on scoala.tNote
for insert,update,delete
as begin
set nocount on
begin try
declare @codStud char (10)
if exists (select * from inserted)
declare crs cursor for select codStud from inserted
else
declare crs cursor for select codStud from deleted
open crs
fetch next from crs into @codStud
declare @medie decimal(4,2)
while @@fetch_status=0 begin
set @medie=(select avg(convert(decimal,nota))from tNote where codStud=@codStud)
update tStudenti
set media=@medie where codStud=@codStud
fetch next from crs into @codStud
end

close crs
deallocate crs
end try
begin catch
print error_message()
close crs
deallocate crs
end catch
end

select * from tStudenti


select * from tNote
order by codStud

update tNote
set nota=7 where codCurs='POO' and codStud='I001'
close crs deallocate crs

--creare tabel RMU


-- COloanele : Codstud, liceu absolvit

create table RMU (


CodStud char(10) constraint pk_codStudRMU primary key,
LiceuAbsolvit char(30)
--...
)

insert into RMU


select CodStud,null from tStudenti

select * from RMU

create view v_studenti(CodStud,Nume,localitate,LiceuAbsolvit)


as select A.CodStud,Nume,Localitate,LiceuAbsolvit
from tStudenti as A left join RMU as B on A.CodStud = B.CodStud

create trigger v_tg_del


on v_studenti
instead of delete
as
begin
delete tStudenti
from tStudenti as A inner join deleted as B
on A.CodStud = B.CodStud
delete RMU
from RMU as A inner join deleted as B
on A.CodStud = B.CodStud
end

alter trigger v_tg_insert


on v_studenti
instead of insert
as begin
insert into tStudenti(codStud,Nume,CNP,localitate)
select CodStud,Nume,CNP,Localitate from inserted
insert into RMU(codStud, LiceuAbsolvit)
select CodStud,LiceuAbsolvit from inserted
end

alter trigger dbo.v_tg_update


on v_studenti
instead of update
as begin
alter table scoala.tNote drop constraint fk_studN
delete scoala.tStudenti from scoala.tStudenti as A inner join deleted as B
on A.CodStud = B.CodStud
delete RMU from RMU as A inner join deleted as B
on A.CodStud = B.CodStud
insert into scoala.tStudenti(codStud,Nume,CNP,localitate)
select CodStud,Nume,CNP,Localitate from inserted
insert into RMU(codStud, LiceuAbsolvit)
select CodStud,LiceuAbsolvit from inserted
alter table scoala.tNote add constraint fk_studN foreign key(CodStud)
references scoala.tStudenti(CodStud)
end
select * from v_Studenti
select * from RMU

delete v_studenti where CodStud = '1000'


update v_studenti set nume = 'Alinuta',liceuAbsolvit='zinca'
where codStud = '1001'
insert into v_studenti (CodStud,Nume,CNP,Localitate,LiceuAbsolvit)
values('2000','Alex','6220512018263','Pitesti','Barbu'),
('2001','Alin','6220510016547','Valcea','Zinca')

alter view v_studenti(CodStud,Nume,CNP,localitate,LiceuAbsolvit)


as select A.CodStud,Nume,CNP,Localitate,LiceuAbsolvit
from tStudenti as A left join RMU as B on A.CodStud = B.CodStud

select * from RMU

alter table RMU


add AnAbsolvire int
check (AnAbsolvire > 1970)

begin tran
begin try
insert into tStudenti(CodStud, Nume, CNP, CodJudet, Localitate)
values ('3002','Bogdanel','6120518019257','AG','Pitesti')
insert into RMU values('3002','Barbu',2000)
commit tran
end try
begin catch
print error_message();
rollback tran;
end catch

You might also like