基本的SQL语句练习

  • 格式:docx
  • 大小:27.38 KB
  • 文档页数:6

基本的SQL语句练习(学生课程表) 创建表 [c-sharp]view plaincopyprint? 1. DROP TABLE IF EXISTS student; 2. CREATE TABLE student( 3. sno int auto_increment primary key, 4. sname varchar(8), 5. ssex varchar(3), 6. sage int , 7. sclass varchar(6) 8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 9. INSERT INTO student(sname,ssex,sage,sclass) VALUES('李勇','男',20,'y01'); 10. INSERT INTO student(sname,ssex,sage,sclass) VALUES('刘晨','男',21,'y02'); 11. INSERT INTO student(sname,ssex,sage,sclass) VALUES('王敏','女',19,'y02'); 12. INSERT INTO student(sname,ssex,sage,sclass) VALUES('张力','男',25,'y05'); 13. DROP TABLE IF EXISTS course; 14. CREATE TABLE course( 15. cno int auto_increment primary key, 16. cname varchar(20), 17. ccredit int 18. ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 19. INSERT INTO course(cname,ccredit) VALUES('C语言',5); 20. INSERT INTO course(cname,ccredit) VALUES('数据库',5); 21. INSERT INTO course(cname,ccredit) VALUES('开发模式_VB',5); 22. DROP TABLE IF EXISTS sc; 23. CREATE TABLE sc( 24. sno int references student(sno) on delete cascade, 25. cno int references course(cno) on delete cascade, 26. grade int 27. ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 28. INSERT INTO sc VALUES(1,1,90); 29. INSERT INTO sc VALUES(1,2,95); 30. INSERT INTO sc VALUES(2,1,55); 31. INSERT INTO sc VALUES(4,3,null);

DROP TABLE IF EXISTS student;CREATE TABLE student(sno int auto_increment primary key,sname varchar(8),ssex varchar(3),sage int ,sclass varchar(6)) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO student(sname,ssex,sage,sclass) VALUES(INSERT INTO student(sname,ssex,sage,sclass) VALUES(INSERT INTO student(sname,ssex,sage,sclass) VALUES(INSERT INTO student(sname,ssex,sage,sclass) VALUES(DROP TABLE IF EXISTS course;CREATE TABLE course(cno int auto_increment primary key,cname varchar(20),ccredit int) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1、查出全体同学的学号、姓名 select sno,sname from student; 2、查询全体同学的姓名学号班级(按顺序输出) select sname,sno,sclass from student; 3、查询全体同学的记录(显示所有行) select * from student; 4、查询全体同学的姓名及出生年份 select sname,(2010-sage) as '出生年份' from student ; 5、查询全体同学姓名出生年份班级(班级要用小写字母lower函数) select sname,2010-sage as '出生年份',lower(sclass) from student; 6、查询全体同学的姓名/出生年份/所在班级列为YearOfBirth select sname,2010-sage YearOfBirth,sclass from student; 7、查询选中学员的所在班级并且去掉重复行用distinct select distinct sclass from student; 8、查询Y02班全体同学名单 select * from student where sclass='y02'; 9、查询所有年龄在20岁一下的同学姓名及年龄 select sname,sage from student where sage<20; 10、查询考试不及格的同学的姓名及成绩 select s.sname,sc.grade from student s,sc where s.sno = sc.sno and sc.grade<60; 11、查询年龄在19-20岁(包括19、20)之间的同学姓名、班级、年龄 select sname,sclass,sage from student where sage in (19,20);(19、20 比较特殊选项少可以使用 in) select sname,sclass,sage from student where sage between 19 and 20; select sname,sclass,sage from student where sage>=19 and sage<=20; 12、查询年龄不在19-20岁之间的同学姓名、班级、年龄 select sname,sclass,sage from student where sage not in (19,20); select sname,sclass,sage from student where sage not between 19 and 20; select sname,sclass,sage from student where sage<19 or sage>20; 13、查询y02班级和y05班的同学的姓名、性别 select sname,sclass from student where sclass in ('y02','y05'); select sname,sclass from student where sclass='y02' or sclass='y05'; 14、查询不是y02或者y05班的同学的姓名、性别 select sname,sage from student where sclass not in('y02','y05'); select sname,sage from student where sclass!='y02' and sclass!='y05'; select sname,sage from student where not sclass='y02' and not sclass='y05'; 15、查所有姓刘的同学的姓名、学号、性别( " % "表示一个或者多个," _ "表示只占一个字符) select sname,sno,ssex from student where sname like'刘%'; 16、查所有姓张且全名只有2个汉子的同学的所有信息 select * from student where sname like'张_'; 17、某些学生未考试查缺成绩的同学的学号和课程号 select sno,cno,grade from sc where grade is null; 18、查询所有成绩的同学的学号、课程号和成绩 select sno,cno,grade from sc where grade is not null; 19、查y02班年龄在20岁一下的姓名和年龄 select sname,sage from student where sclass='y02' and sage<20; 20、查选修1号课程的同学的学号和成绩,按成绩降序排序 select sno,grade from sc where cno=1 order by grade desc; 21、查全体同学信息查询结果按所在班级的班级名称按降序排列,同班同学按年龄升序排列 select * from student order by sclassdesc,sageasc; 22、查询学员的总人数 select count(sno) from student;