学习之 数据库

来源:互联网 发布:软件测试方法app 编辑:程序博客网 时间:2024/06/14 01:22

不废话,因为准备笔试,所以看了两天书,有点不适应,先热身吧

 

今天主要看了数据库的SQL语句,重新复习

 

create table s

(

s# char(8) primary key,

sname char(8) not NULL default 'Unknown',

SEX  char(1),

AGE  tinyint,

primary key s# 

reference c.s#,

GRADE int,

foreign key sname reference c.sname,

check (( GRADE is null) or(GRADE between 0 and 100))

check (SEX ='M' or SEX= 'F')

)

 

 

alter table add location char(30),

 

select * from A cross/left/right join B on A.b=B.b.

 

 

union的作用是去除重复元素例如

select s# from s where c#=c1 union(select s# from s where c#=c2)

既选择了s1课程,又选择了s2课程的学生的代号,避免了表的连接,效率更高

 

where avg(GRADE)>=max(grade), 选择平均成绩最高的学生

select count(G) from s group by c# 

 

 

select    S#max(GRADE)
min(GRADE), avg(GRADE)
   from     SC
  group by  S#
选出每个学生的学号,最高成绩,最低成绩,平均成绩

select    S#avg(GRADE)
   from     SC
  group by  S#
  having  min(GRADE) >= 60

选出平均成绩大于60的学生的学号,


select    AGEcount(S#)
from     S
where   SEX = M
group by  AGE
having  count(*) > 50

某个年龄段,超过50个人的年龄和人数


select    S#
    from     SC
    group by S#
    having    avg(GRADE) >=  all
(select avg(GRADE)
from  SC
group  by S#)

平均成绩最高的学生

【实战笔试题】
有学生系统数据库,存在关系,s(sno,sname),c(cno,cname),sc(sno,cno,grade)
1 选择课程 db的学生的no
2 成绩最高的学生的学号
3 每科大于90的学生的学号
1
select sno
from sc
where cno in 
(select cno  
from c
where cname='db')
2
select sno
from sc
where  max(grade)
3
select count(sno)
from sc
where sno in
(select sno
from sc
group by sno
having grade>=90
)
所谓内连接,外连接,交叉连接
内连接:只列出符合条件的行, select * from a inner join b on a.username = b.username.
外连接:列出符合条件的行,同时也列出剩余的行  select * from a left outer join b on a.username = b.username. 列出左边所有        的行,不仅仅是符合条件的行
交叉连接,笛卡尔积的形式

 

 

java对数据库的操作,最基本的操作也是非常简单的

选择driver,对driver的出错处理;connection建立好连接;statement要写好sql语句,执行结果返回给result,

所谓statement和preparedstatement,与存储过程是类似的,preparedstatement就是提前编译,执行时可以不必再编译,同时,只需要设置不同的参数,就可以实现代码的reuse,

 

 

原创粉丝点击