Database experiment of view operation of sqlserver

Create a view of students whose department number is 01;

create view department
as select *
from student
where clno='01';

Create a view with course number 001, and output the student number, name, course name and grades of the students who have taken this course;

create view class_001
as select sc.sno,sname,cname,score
from student,sc,course
where course.cno='001' and course.CNo=sc.CNo and
sc.SNo=student.SNo;

Use the view created in 2 to query the names of students who failed the course exam with course number 001;

select sname
from class_001
where score<60;

Create a view of the student surnamed Wang, output the student number and name, and require the later use of WITH CHECK OPTION [WITH CHECK OPTION means that when using the view to perform update (UPDATE), insert (INSERT) and delete (DELECT) operations, it is necessary to ensure that the updated element The group satisfies the predicate condition in the view definition, that is, the conditional expression in the image statement];

create view xing_wang
as select sno,sname
from Student
where sname like '王%'
with check option;

Create a view of the student surnamed Wang, output the student number and name, and request not to use WITH CHECK OPTION;

create view xing_wang2
as select sno,sname
from Student
where sname like '王%';

Insert the record (00051, Li Ming) into the view established in 4, and insert (00052, Li Ming) into the view established in 5, and then check the difference, and check the changes in the table Student and the two views at the same time; Then insert (00053, Wang Ming) into the view created in 4, and check the changes of the table Student and the changes of the two views;

Insert into the view created in 4 (00051, Li Ming)

insert 
into xing_wang 
values('00051','Li Ming');--failure
--The attempted insert or update has failed because the target view or a view spanned by the target view specified WITH CHECK OPTION, and the One or more result rows of the operation do not meet the CHECK OPTION constraints.

Insert into the view created in 5 (00052, Li Ming)

insert 
into xing_wang2 
values('00052','Li Ming');--Success

Insert into the view created in 4 (00053, Wang Ming)

insert 
into xing_wang2 
values('00053','Wang Ming');--success

Create a view of the course name, elective student ID and grades of the courses taught by Mr. Zhang San;

create view zhangsan_t
as select course.cname,sc.SNo,sc.Score
from course,sc,Teacher
where course.CTno=teacher.Tno and teacher.TName='张三' and course.CNo=sc.CNo;

According to the view built in 7, see if the following operations can be completed: (1) add 1 to one of the students' grades; (2) delete a row of data; (3) insert a row of data;

--(1) Add 1 to the grade of one of the students;

update zhangsan_t
set Score = Score+1
where sno='00001';

--(2) Delete a row of data;

delete 
from zhangsan_t
where sno='00002';
--The view or function 'zhangsan_t' cannot be updated, because the modification will affect multiple base tables.

--(3) Insert a row of data;

insert
into zhangsan_t
values('data structure','00007','77');
--The view or function 'zhangsan_t' cannot be updated, because the modification will affect multiple base tables.

Create a view of a student's student ID and grade point average;

create view stu_sno_score
as select sno,avg(score) average grade -- creating view or function failed because no column name was specified for column 2.
from sc group by sno;

Delete the view created by 1;

drop view department;

Create a view of student number, student name and total credits each student has taken, and encrypt the view when creating; (Hint: use WITH ENCRYPTION);

create view stu_message2 WITH ENCRYPTION
as select Student.sno,sname,总学分
    from student,(select sno,sum(course.ccredits) 总学分 
        from sc,course 
        where sc.CNo=course.CNo 
        group by sno) sc1
    where student.SNo=sc1.SNo;

Use the stored procedure sp_helptext to view the views created by 9 and 11;

EXEC sp_helptext'stu_sno_score';--9

EXEC sp_helptext'stu_message';--11

Use the window to modify directly in the view created in 9, and the required content is changed to the student's student number, name, course number, course name and grade.

Guess you like

Origin blog.csdn.net/m0_64206989/article/details/129956446