- 用SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”。
create table `student`(Sno int PRIMARY KEY, Sname varchar(20) unique, Ssex varchar(5) check(Ssex in ('男','女')) , Sage int, Sdept varchar(20) DEFAULT('计算机') );
- 修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint。
alter table `student` MODIFY sage smallint;
- 为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX
create table SC(Sno int, Cno int, Score int) ;
create index SC_INDEX on `sc`(sno,cno asc)
- 创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
create VIEW stu_info as select a.sname,a.ssex,b.cname,c.score from student as a , course as b , sc as c where a.sno=c.sno and c.cno=b.cno;
select * from stu_info;
- 显示所有职工的基本信息。 select * FROM worker;
2、查询所有职工所属部门的部门号,不显示重复的部门号。
select DISTINCT(部门号)FROM worker;
3、求出所有职工的人数。 select count(*) from worker;
4、列出最高工和最低工资。 select max(工资),min(工资) from worker;
5、列出职工的平均工资和总工资。 select avg(工资),sum(工资) from worker;
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。create table `工作日期表`(id int PRIMARY key,name varchar(20),)
7、显示所有女职工的年龄。无性别字段
8、列出所有姓刘的职工的职工号、姓名和出生日期。
select 职工号,姓名,出生日期 from worker WHERE 姓名 like '张%'
9、列出1960年以前出生的职工的姓名、参加工作日期。
10、列出工资在1000-2000之间的所有职工姓名。select 职工号,姓名,工作时间 from worker WHERE 工资>=1000 and 工资<=9000;
11、列出所有陈姓和李姓的职工姓名。
select 姓名 from worker WHERE 姓名 like '李%' or 姓名 like '张%';
12、列出所有部门号为2和3的职工号、姓名、党员否。
13、将职工表worker中的职工按出生的先后顺序排序。SELECT * from worker order by `出生日期` ;
14、显示工资最高的前3名职工的职工号和姓名。 SELECT * from worker order by 工资 LIMIT 3;
15、求出各部门党员的人数。 SELECT 部门号,count(*) from worker where 政治面貌 ='党员' GROUP BY 部门号;
16、统计各部门的工资和平均工资SELECT 部门号,sum(工资),avg(工资) from worker GROUP BY 部门号;
17、列出总人数大于4的部门号和总人数。SELECT 部门号,count(*) from worker GROUP BY 部门号 HAVING count(*)>4;
3.查询student表的所有记录 select * from student;
4.查询student表的第2条到4条记录 select * from student LIMIT 1 ,3;
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息 select id,name,department from student ;
6.从student表中查询计算机系和英语系的学生的信息
select * from student where department in ('计算机系','英语系') ;
select * from student where department ='计算机系' or department ='英语系'
- 从student表中查询年龄18~22岁的学生信息
select * from student where (year(now())-birth) >=18 or (year(now())-birth)<=22;
- 从student表中查询每个院系有多少人
select department ,count(*) from student group by department;
- 从score表中查询每个科目的最高分
select max(grade), c_name from score group by c_name;
- 查询李四的考试科目(c_name)和考试成绩(grade)
select a.`name`,b.grade , b.c_name from student as a , score as b where a.id=b.stu_id and name='李四';
- 用连接的方式查询所有学生的信息和考试信息
select * from student as a , score as b where a.id=b.stu_id
- 计算每个学生的总成绩
select a.name,a.id ,sum(grade) from student a,score b where a.id=b.stu_id group by a.id;
- 计算每个考试科目的平均成绩
select id ,c_name,avg(grade) from score group by id;
- 查询计算机成绩低于95的学生信息
select a.*, b.grade,b.c_name from student a , score b where c_name='计算机' and grade<95;
- 查询同时参加计算机和英语考试的学生的信息
- 将计算机考试成绩按从高到低进行排序
SELECT * from score where c_name='计算机' ORDER BY grade desc;
17.从student表和score表中查询出学生的学号,然后合并查询结果
SELECT * from student a,score b where a.id=b.stu_id;
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
SELECT name,department,c_name,grade from student a,score b where a.`name` like "张%" or a.`name` like "王%";
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
SELECT name, year(now())-birth '年龄',department,c_name,grade from student a,score b where a.name like "张%" or a.name like "王%";