Database Principle Experiment 4: Views and Indexes (all codes for 17 questions)

Database Principle: Experiment 4: Views and Indexes (all codes for 17 questions)

-------Engineering large database principle experiment class
-------Source: Department of Computer Science 0211181 Lan Duoduo, 0211181 Chen Ge
, the latest version on December 5, 2020.
Note that your s, sc, c tables are included Is the data correct? It’s okay to debug one more code blue!
Please like before reading (copy assignment).

–1. Use Transact-SQL statements to create the view Stu_CS1 of the "Computer Department" students.

create view Stu_CS1 (sno,sname,sex,age)as select sno,sname,sex,age from s where sdept='计算机'

-2. Use the graphical tool of SQL Server Management Studio to create the student view Stu_CS2 of the "Computer Department" who took the "c2" course on the basis of the view Stu_CS1.

create view Stu_CS2(sno,sname,sex,age,cno)as select sno,sname,sex,age,cno from Stu_CS1,c where cno='c2' 

–3. On the basis of the view Stu_CS2, create the student view Stu_CS3 of the "Computer Department" who has taken the "c2" course and has a score of "90" or higher.

create view Stu_CS3(sno,sname,sex,age,grade)as select Stu_CS2.sno,sname,sex,age, grade from Stu_CS2,sc where grade>90

-4. On the basic table SC, establish a student learning status view S_GRADE, which includes: student ID, number of elective courses, and average grade.

create view S_GRADE(sno,num,avggrade)as select sno,count(cno),avg(grade)from sc group by sno

-5. Query each student's student ID, number of elective courses, and average grade through the student learning status view S_GRADE.

select *from S_GRADE

-6. Query the student number, number of elective courses, and average grade of elective courses greater than "S4 elective courses" through the student learning status view S_GRADE.

select *from S_GRADE where num >(select num from S_GRADE where sno='S4')

-7. Define the student view STUDENT_COMPUTER of the "Computer" department, including student ID, name, gender, and age.

create view STUDENT_COMPUTER as select sno,sname,sex,age from s where sdept='计算机'

-8. Based on the student view STUDENT_COMPUTER of the "Computer" department, insert a student's information, the student ID is S99, the name is Wang Min, the gender is male, and the age is 22. Observe the changes in the data in the student table s.

insert into STUDENT_COMPUTER values('s99','王敏','男',22)

-9. Based on the student view STUDENT_COMPUTER of the "Computer" department, delete the student record named "Wang Min".

delete from STUDENT_COMPUTER where sname='王敏'

–10. Create view view1, request to query the elective courses and grades of students whose student ID is's1'

create view view1(sno,cno,grade)as select sno,cno,grade from sc where sno='s1'

–11. Change the view view1, request to query the elective courses and grades of students whose student ID is's1' and's3'

alter view view1(sno,cno,grade)as select sno,cno,grade from sc where sno='s1'or sno='s3'

–12. Update the score of the student'c1' with the student ID of's1' through view1 to 98 points

update view1 set grade=98 where cno='c1'and sno='s1'

–13. On the basic table SC, establish a student's academic performance level view SC_level, in which there are student ID, name, class number of elective courses, class name, and grade level.

create view SC_等级(sno,sname,cno,cname,gradelevel)as select sc.sno,sname,sc.cno,cname,(case when grade>90 then 'A' when grade>80 and grade<90 then'B' when grade>70 and grade<80 then'C' when grade>60 and grade<70 then'D'
when grade<60 then'E' end)from sc,c,s where s.sno=sc.sno and c.cno=sc.cno

–14. Use Transact-SQL statements to create a clustered unique index in the sc table, the index name is IDX_SNO_CNO, the index is sorted in ascending order of SNO, and when SNO is the same, it is sorted in descending order of CNO

create unique clustered index IDX_SNO_CNO on sc(sno asc,cno desc)

–15. Use the graphical tool of SQL Server Management Studio to create a common index IDX_S on the SDEPT (the series) column of the S table

create index IDX_S on s (sdept)

–16. Create a composite index IDX_SDEPT_AGE in the S table, the index is arranged in ascending order of the system, and when the system is the same in descending order of age

create index IDX_SDEPT_AGE on s(sdept asc,age desc)

–17. Delete the index IDX_SDEPT_AGE

drop index s.IDX_SDEPT_AGE

Guess you like

Origin blog.csdn.net/qq_43554335/article/details/110739681