用SQL语句和企业管理器建立如下的表结构并输入数据
创建数据库
create table student(
Sno int auto_increment primary key,
Sname varchar(45),
Ssex varchar(45),
Sage int,
Sdept varchar(45)
)engine = InnoDB default charset=utf8;
create table Course(
Cno int auto_increment primary key,
Cname varchar(45),
Cpno int,
Ccredit int
) engine = InnoDB default charset=utf8;
create table SC(
Sno int ,
Cno int ,
Grade int,
primary key(Sno,Cno),
constraint c_fk foreign key(Sno)references student(Sno),
constraint c_fk21 foreign key(Cno) references Course(Cno)
) engine = InnoDB default charset=utf8;
(1)Course表插入数据:
SELECT * FROM Student.Course;
insert into Course values(1,"数据库",5,4);
insert into Course (Cno,Cname,Ccredit) values(2,"数学",2);
insert into Course values(3,"信息系统",1,4);
insert into Course values(4,"操作系统",6,3);
insert into Course values(5,"数据结构",7,4);
insert into Course (Cno,Cname,Ccredit) values(6,"数据处理",2);
insert into Course values(7,"PASCAL语言",6,4);
(2)SC表插入数据:
SELECT * FROM Student.SC;
insert into SC values(95001,1,92);
insert into SC values(95001,2,85);
insert into SC values(95001,3,88);
insert into SC values(95002,2,90);
insert into SC values(95002,3,85);
insert into SC values(95003,3,59);
(3)student表插入数据
SELECT * FROM Student.student;
insert into student values(95001,"李勇","男",20,"CS");
insert into student values(95002,"刘晨","女",21,"lS");
insert into student values(95003,"王敏","女",18,"MA");
insert into student values(95004,"张力","男",19,"lS");
用SQL语句完成一下的要求:
1.select * from student where Sdept="lS"
2.select * from Course where Cname="数学";
3.select Sname from student,SC,Course where Course.Cpno="5" and SC.Cno=Course.Cno and SC.Sno=student.Sno;
4.select Sname,2018-Sage from student;
5.select Sname from student where Sname like '王%'
6.select Sname,Grade from student ,SC where Cno='3'and SC.Sno=student.Sno order by Grade desc;
7.select * from student order by Sdept asc ,Sage desc;
8.select avg(Grade) avg2 from SC where SC.Cno='2';
9.select max(Grade) from SC where SC.Cno='2';
10.select Cno,count(Sno) from SC group by Cno;
11.select Sno from SC group by Sno having count(Cno)>=3;
12.select Cname from Course where Cno=(select Cpno from Course where Cname='数据库');
13.select student.Sno,Sname from student,SC
where SC.Sno=student.Sno and Grade in (select max(Grade) from SC);
14.select student.Sno,Sname from SC,student where SC.Sno=student.Sno and
Grade in (select max(Grade) from SC,Course where SC.Cno=Course.Cno and Course.Cname="数学") ;
15.select Sno from SC where Grade in (select min(Grade) from SC) and Sno in (select max(Sno) from SC);
16.select * from SC where Grade > (select avg(Grade) from SC);
17.select Sno from SC where Sno in (select Sno from SC where Cno=1 ) and Cno=3;
18.select Sno from SC where Cno = 1 and 3 and not exists (select Sno from SC where Cno=2) ;
19. SELECT Sname from student where not exists (select * from SC WHERE Sno=student.Sno and Cno='1');
20.select Sname from student where Sno IN (select Sno from SC group by Sno having count(*) = (select count(*) from Course ));
21. select distinct Sno from SC where Sno in(select Sno from SC s1 where not exists(select * from SC s2 where s2.Sno='95002' and not exists(select * from SC s3 where s1.Sno=s3.Sno and s2.Cno=s3.Cno)));
22.select distinct SC.Sno,Sname from SC ,student where Grade>60 and SC.Sno =student.Sno;
23.select distinct SC.Cno, Cname from SC ,Course where Grade>60 and SC.Cno = Course.Cno;
24.create view is_student(a_Sno,a_Sname,a_Sage)as select Sno ,Sname ,Sage from student
where Sdept ='lS';
select * from is_student where a_Sage in (select max(a_Sage) from is_student);