数据库创建索引的缺点?
缺点:
第一,创建索引和维护索引要耗费时间,这种时间随着数据量的增加而增加。
第二,索引需要占物理空间,除了数据表占数据空间之外,每一个索引还要占一定的物理空间,如果要建立聚簇索引,那么需要的空间就会更大。
第三,当对表中的数据进行增加、删除和修改的时候,索引也要动态的维护,这样就降低了数据的维护速度。
有两张表;请用SQL查询,所有的客户订单日期最新的前五条订单记录。(分别注明MySQL. Oracle写法)
客户信息表(c CUSTOM)有以下字段:
id、name、mobile
客户订单表(C_ORDER)有以下字段:
id、custom_id、commodity、count、order _date
Mysql:
Select * from c_order order by order_date desc limit 0,5;
Oracle:
Select o.*,rownum n
from c_order order by order_date desc where n<6;
关于HQL与SQL,以下哪些说法正确?()
A. | HQL与SQL没什么差别 |
B. | HQL面向对象,而SQL操纵关系数据库 |
C. | 在 HQL 与 SQL 中,都包含 select,insert,update,delete 语句 |
D. | HQL仅用于査询数据,不支持insert,update和delete语句 |
答案:BC |
下面是学生表(student)的结构说明
字段名称 | 字段解释 | 字段类型 | 字段长度 | 约束 |
s_id | 学号 | 字符 | 10 | PK |
s_name | 学生姓名 | 字符 | 50 | Not null |
s_age | 学生年龄 | 数值 | 3 | Not null |
s-sex | 学生性别 | 字符(男:1女:0) | 1 | Not null |
下面是教师表(Teacher )的结构说明
字段名称 | 字段解释 | 字段类型 | 字段长度 | 约束 |
t_id | 教师编号 | 字符 | 10 | PK |
t_name | 教师名字 | 字符 | 50 | Not null |
下面是课程表(Course)的结构说明
字段名称 | 字段解释 | 字段类型 | 字段长度 | 约束 |
c_id | 课程编号 | 字符 | 10 | PK |
c_name | 课程名字 | 字符 | 50 | Not null |
t_id | 教师编号 | 字符 | 10 | Not null |
下面是成绩表(SC)的结构说明
字段名称 | 字段解释 | 字段类型 | 字段长度 | 约束 |
s_id | 学号 | 字符 | 10 | PK |
c_id | 课程编号 | 字符 | 10 | Not null |
score | 成绩 | 数值 | 3 | Not null |
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.s_id from (select s_id,score from SC where C_ID='001') a,(select s_id,score
from SC where C_ID='002') b
where a.score>b.score and a.s_id=b.s_id;
2、查询平均成绩大于60分的同学的学号和平均成绩;
select S_ID,avg(score)
from sc
group by S_ID having avg(score) >60;
3、查询所有同学的学号、姓名、选课数、总成绩;
select Student.S_ID,Student.Sname,count(SC.C_ID),sum(score)
from Student left Outer join SC on Student.S_ID=SC.S_ID
group by Student.S_ID,Sname
4、查询姓“李”的老师的个数;
select count(distinct(Tname))
from Teacher
where Tname like '李%';
5、查询没学过“叶平”老师课的同学的学号、姓名;
select Student.S_ID,Student.Sname
from Student
where S_ID not in (select distinct( SC.S_ID) from SC,Course,Teacher where SC.C_ID=Course.C_ID and Teacher.T#=Course.T# and Teacher.Tname='叶平');
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
elect Student.S_ID,Student.Sname from Student,SC where Student.S_ID=SC.S_ID and SC.C_ID='001'and exists( Select * from SC as SC_2 where SC_2.S_ID=SC.S_ID and SC_2.C_ID='002');
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select S_ID,Sname
from Student
where S_ID in (select S_ID from SC ,Course ,Teacher where SC.C_ID=Course.C_ID and Teacher.T#=Course.T# and Teacher.Tname='叶平' group by S_ID having count(SC.C_ID)=(select count(C_ID) from Course,Teacher where Teacher.T#=Course.T# and Tname='叶平'));
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
Select S_ID,Sname from (select Student.S_ID,Student.Sname,score ,(select score from SC SC_2 where SC_2.S_ID=Student.S_ID and SC_2.C_ID='002') score2
from Student,SC where Student.S_ID=SC.S_ID and C_ID='001') S_2 where score2 < score;
9、查询所有课程成绩小于60分的同学的学号、姓名;
select S_ID,Sname
from Student
where S_ID not in (select S.S_ID from Student AS S,SC where S.S_ID=SC.S_ID and score>60);
10、查询没有学全所有课的同学的学号、姓名;
select Student.S_ID,Student.Sname
from Student,SC
where Student.S_ID=SC.S_ID group by Student.S_ID,Student.Sname having count(C_ID) <(select count(C_ID) from Course);
11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
select distinct S_ID,Sname from Student,SC where Student.S_ID=SC.S_ID and SC.C_ID in (select C_ID from SC where S_ID='1001');
12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
select distinct SC.S_ID,Sname
from Student,SC
where Student.S_ID=SC.S_ID and C_ID in (select C_ID from SC where S_ID='001');