SQL statement query (two)

I believe that every bit of accumulation will eventually accumulate.

(1) Inquire about students who have taken course 1 or course 2 as an elective. (Write 2 forms of query statements)
select Sno from SC where Cno='1' UNION select Sno from SC where Cno='2';
select distinct Sno from SC where Cno in ('1','2')
( 2) Inquire about the names of students who have not taken the No. 2 course.
select Sname from Student where not exists(select from SC where Sno=Student.Sno AND Cno='2');
(3) Query the intersection of the student set of elective course 1 and the student set of elective course 2.
select Sno from SC where Cno='1' intersect select Sno from SC where Cno='2';
(4) Query the difference between students in the information department and students who are not more than 19 years old.
select
from Student where Sdept='IS' except select* from Student where Sage<=19;
(5) Retrieve the names of students who have taken three courses or more.
select Sname from Student where Sno in(select sc.Sno from SC group by sc.sno having COUNT(*) >= 3)
(6) Delete the course selection records of all students in the Information Department (IS).
delete from SC where Sno in(select Sno from Student where Sdept='IS');
(7) The course name for modifying the record of Cno to "1" is "Database Principles and Applications".
update Course set Cname='Database Principles and Applications'where Cno='1';
(8) Change the scores of all students in the Information Department to 80.
update SC set Grade=80 where Sno in(select Sno from Student where Sdept='IS')
(9) Delete the record of the student whose name is Zhang Li.
delete from Student where Sname in(select Sname from Student where Sname='Zhang Li')
(10) Use SQL to add a record to the course table, the content of the added record is as follows:
('9','data mining','1 ',5)
insert INTO Course(Cno,Cname,Cpno,Ccredit) values ​​('9','data mining','1','5')
(11) Add a student number to the student table as "s9" , The student named "Zhao Dan"
insert into Student(Sno,Sname) values('s9','赵丹')
(12) Delete the added student in (7).
delete from Student Where Sno='s9'
(13) Query the course numbers of the courses jointly taken by the students with the student number "201215121" and the students with the student number "201215122".
select Cno from SC where Sno='201215121' intersect select Cno from SC where Sno='201215122'
Use SQL statements to create a view and perform corresponding operations on the view:
(1) Establish a view CS_View for computer students, and require the view to include students All attributes in the table must be modified and inserted when required to ensure that the view is only for students from the computer science department.
create view CS_Student as select * from Student where Sdept='CS'
with check option
(2) Establish a student view CS_SC_View, which is a student where Sdept='CS' with check option is selected in the Department of Computer Science and whose score is below 90 points, and queries this view.
create view CS_S3(Sno,Sname,Grade) as select Student.Sno,Sname,Grade from Student,SC
where Sdept='CS' AND Student.Sno=SC.Sno and SC.Cno='3'
create view CS_s4 as select Sno, Sname, Grade from CS_S3 where Grade<90;
(3) Define the student number and grade point average as a view.
create view S_G(Sno,Gavg) as select Sno,AVG(Grade) From SC Group BY Sno;
(4) Use the established view to query the students of the computer department who have taken the No. 2 course.
select CS_Student.Sno,Sname from CS_Student,SC where CS_Student.Sno=SC.Sno and SC.Cno ='2'
(5) Change the name of the student whose middle school number is 201215122 in the view CS_View to "Liu Liu", and check the Student table Is there any change in the name of this student? If there is any change, please explain why.
update CS_Student set Sname='Liu Liu'where Sno='201215122'
(6) Insert a new student record into the view CS_View, where the student number is 201215126, the name is Li San, the age is 20, the department is IS, If the execution fails, analyze the execution results.
insert into CS_Student values('201215126','李三',20)
failed to execute successfully Reason: Because the specified CS_Student view can only have computer systems, this modification is not inserted into the computer system, so it cannot be operated.
(7) Change the view of the department of the student whose CS_View middle school number is 201215121 to IS, and check whether the department of this student in the Student table has changed.
update CS_Student set Sdept='IS' where Sno='201215121';


Guess you like

Origin blog.csdn.net/qq_43254543/article/details/89360204