当前位置:网站首页>SQL -- course experiment examination

SQL -- course experiment examination

2022-06-12 07:13:00 Not Agan

        

 

《 Database principle experiment 》

Course examination report

1、 Simple requirements

The main function of this system is to collect students' personal information , Teachers' teaching situation . System

The main functions of :

a、 Student personal information includes : full name 、 Student number .

b、 The elective list includes : Student number 、 Course no. 、 achievement .

c、 The curriculum includes : Course no. 、 Course name 、 Teacher number 、 Teacher number .

d、 A trigger : Users are prohibited from inserting scores less than 0 Student information

e、 Two stored procedures :① Find the average score of the specified subject ② Reduce the scores of designated students

2、 Conceptual structural design

 

chart 1 E-R chart

3、 The structure of the design table

chart 2 The structure of the table

4、 Create database

① Build table

ifOBJECT_ID('Student')isnotnull

droptable Student

go

createtable Student

(

Sno char(9)primarykey,

Sname char(9)notnullunique,

);

go

ifOBJECT_ID('Course')isnotnull

droptable Course

go

createtable Course

(

Cno char(2)primarykey,

Cname char(20)notnull,

Tno char(9)notnull,

Tname char(9)notnull,

);

go

ifOBJECT_ID('SC')isnotnull

droptable SC

go

createtable SC

(

SC_Sno char(9),

SC_Cno char(2),

SC_Grade intnull,

primarykey(SC_Sno,SC_Cno),

foreignkey(SC_Sno)references Student(Sno),

foreignkey(SC_Cno)references Course(Cno)

);

Go

 

chart 3 design sketch

② Create triggers and stored procedures

-- trigger

ifOBJECT_ID('tri_SC')isnotnull

droptrigger tri_SC

go

createtrigger tri_SC on SC

afterinsert

as

    declare @temp int,@SNo char(9),@CNo char(2)

    set @temp =(selecttop 1 SC_Grade from inserted)

    set @CNo =(selecttop 1 SC_Cno from inserted)

    set @SNo =(selecttop 1 SC_Sno from inserted)

    if(@temp>=0)

    begin

        print' Insert the success '

    end

    else

    begin

        deletefrom SC where(SC_Sno like @SNo and SC_Cno like @CNo)

        print' The score is less than 0 Do not insert '

    end

go

-- stored procedure 1

ifOBJECT_id('reduce')isnotnull

dropprocedure avgGrade

go

createprocedure reduce(@num int,@sno char(9),@cno char(2))

asdeclare

    @finalGrade int;

begin

    set @finalGrade =(select SC_Grade from SC where (SC_Sno like @sno and SC_Cno like @cno))

    update SC

    set SC_Grade = @finalGrade-@num

    where (SC_Sno like @sno and SC_Cno like @cno)

end

-- stored procedure 2

ifOBJECT_id('avgGrade')isnotnull

dropprocedure avgGrade

go

createprocedure avgGrade(@cno char(2))

as

begin

selectavg(SC_Grade)

from SC

groupby(SC_Cno)

having SC_Cno like @cno

end

5、 Verification function

① insert data

insert

into Student(Sno,Sname)values

('20041013',' Agan '),

('20049014',' Infinite '),

('20049015',' Little black '),

('20041016',' Wind breath ');

go

insert Course(Cno,Cname,Tno,Tname)values

('1',' database ','1111',' Mr. Xing '),

('2',' mathematics ','1111',' Mr. Xing '),

('3',' Network counting ','1111',' Mr. Xing '),

('4',' Physics ','1111',' Mr. Xing '),

('5','C Language ','1112',' Infinite teacher ');

go

insert SC(SC_Sno,SC_Cno,SC_Grade)values

('20041013','1',100),

('20041013','2',100),

('20041013','3',100),

('20041013','4',100),

('20041016','1',90),

('20041016','2',100);

go

chart 4 Result chart

③ Trigger validation

Try inserting a score less than 0 Student information .

insert SC(SC_Sno,SC_Cno,SC_Grade)values

('20041016','3',-1)

 

chart 5 Trigger result graph

④ Storage process

reduce stored procedure : Reduce student number 20041013 Student subject 1 fraction 20

exec reduce@num=20,@sno='20041013',@cno='1';

 

chart 6 Stored procedure result graph

avgGrade stored procedure : Demand account 1 On average

exec avgGrade@cno='1';-- Demand account cno 1  On average

 

chart 7 Stored procedure result graph

原网站

版权声明
本文为[Not Agan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120705588589.html