v_s1 using the command create view. Then, use this
view to query students' grades, and you can also
statistically summarize the query results.
Create view program as follows:
create view v_s1
as
select t1.stuid,sname,t2.cid,cname,grade
from stuinfo t1,course t2,score t3
where t1.stuid=t3.stuid and t2.cid=t3.cid
Then, using this view v_ s1 can query the student
ID, name, course ID, course name, and grades of each
student. The program code is as follows:
use student
go
select stuid,sname,cid,cname,grade
from v_s1
Figure 3. View query result 1.
For example, to query the average score, highest
score, and lowest score of each course that a student
has studied, you can directly use this view to query.
The program code is as follows:
use student
go
select cid,cname,AVG(grade) as average,MAX(grade)
as Maximum,
MIN(grade) as Minimum
from v_s1
group by cid,cname
Figure 4. View query result 2.
From the program code designed in the above
example, whether it is the number of lines to write the
program or the running efficiency of the program,
compared to the multi table query designed using the
join query method before, the view query is more
concise and easy to understand, and the program code
is also relatively easy to write.
As a special database object, a view is a virtual
table. Once defined, a view can be queried, modified,
deleted, and updated like a basic table. Moreover,
querying through a view involves relatively simple
program code, which can simplify operations. This is
highly recognized by database learners or users,
especially those unfamiliar with multi table queries.
Due to the fact that the view only stores the definition
of data and does not store the data itself, it is
beneficial for data security. However, it is also
necessary to have a clear place for the storage of the
data itself, which should be used in conjunction with
the basic table in a reasonable manner.
3.3 Stored Procedure Query
In addition to connection queries and view queries,
multi table queries can also be completed through
stored procedures. A stored procedure is a database
object composed of a set of T-SQL statements and
stored in the form of a storage unit on the server
(Mingyuan Zhao, 2023). Taking querying students'
course grades as an example, first create a stored
procedure, and then use the EXEC command to call
the stored procedure to complete the query of
students' course grades.
The program for creating stored procedure proc1
is as follows:
use student
go
create procedure proc1
as
select t1.stuid,sname,cname,grade
from stuinfo t1,course t2,score t3
where t1.stuid=t3.stuid and t2.cid=t3.cid
Then, if you want to query the score information
of each course that all students have studied, use the
program command EXEC to call the above stored
procedure proc1, that is, directly enter the command
EXEC proc1 in SSMS to achieve data query. The
query results are as follows:
Figure 5. Stored procedure query result 1.
If you want to query the score information of a
single student or course, you need to create a stored
procedure with parameters to implement it. The
program for creating a stored procedure with
parameters, proc2, is as follows:
use student
go
create procedure proc2(@sid varchar(10))
as
select t1.stuid,sname,cname,grade
from stuinfo t1,course t2,score t3
where t1.stuid=t3.stuid and t2.cid=t3.cid and
t1.stuid=@sid
Next, use the program command EXEC to call the
above stored procedure proc2, that is, directly enter
the command EXEC proc2 '200301' in SSMS to
query the data. The query results are as follows:
ANIT 2023 - The International Seminar on Artificial Intelligence, Networking and Information Technology