应届生/初级数据库开发笔面试汇总

基于oracle 10g数据库对笔面试涉及到的最基本应用问题做一个汇总,供需。

一、数据库安装

建议

  1. 遵循一致命名规则、合理的目录以便简化数据库管理
  2. 应用分布在多个磁盘防止数据库错误影响
  3. I/O操作分布多个磁盘提高性能
服务器安装步骤

  1. 运行oracle安装程序-停止所有oracle服务,双击setup.exe
  2. 选择安装方法-高级安装(升级已有数据库时需要选择)或基本安装
  3. 输入全局数据库名称-本地数据库名+网络位置orcl.xxx.com
  4. 设置数据库口令
客户端安装步骤

  1. 运行oracle安装程序
  2. 指定文件位置-源于目标路径
  3. 选择安装类型-管理员
  4. 进行安装
  5. 配置网络服务名-SID=orcl
  6. 配置连接协议-ip地址、端口号
  7. 配置网络服务名

二、启动与关闭

startup                //启动
shutdown x             //关闭,x=normal正常关闭,x=immediate未提交事务会回退,立即关闭,x=transactionnal保证事务提交关闭,x=abort终止关闭

三、建表

CREATE TABLE test( id int not null, name char(20), score int, primary key(id), check(score between 0 and 100) )engine = Innodb;

  1. 设置默认值——DEFAULT '默认值'
  2. 设置非空——NOT NULL
  3. 设置唯一——UNIQUE(字段名)
  4. 设置主键——PRIMARY KEY(字段名) 
  5. 设置外键——FOREIGN KEY(字段名) REFERENCES 表名(字段名)
  6. 设置检查约束——CHECK(字段名 BETWEEN xx AND xx) 或 CHECK(字段名 IN (x,x) 或CHECK(字段名>xx AND ……)

四、虚拟表应用

  1. 计算当前时间:select sysdate from dual
  2. 当计算器使用:select 1+100 from dual
  3. 取出字符串,数字:select 'aaa',1 from dual

五、增删改查例子

student(s#,sname,sage,ssex) 学生表 
course(c#,cname,T# cdate)课程表 
sc(s#,c#,score)成绩表 
Teacher(T#,tname)教师表
  1. 查询a课程比b课程成绩高的所有学生的学号:select sc1.s# from sc sc1 join sc sc2 on sc1.s# = sc2.s# where sc1.cname = 'a' and sc2.cname = 'b' and sc1.score > sc2.score
  2. 查询平均成绩大于60分的同学的学号和平均成绩:select s#, avg(score) from sc group by s# having avg(score)>60
  3. 查询所有同学的学号,姓名,选课数,总成绩:select student.s#,student.name ,count(sc.c#) ,sum(sc.score) from student left join sc on student.s# = sc.s#
  4. 查询姓李的老师个数:select count(T#) from Teacher where tname like "李%" 
  5. 查询没有学过叶萍老师课的同学学号,姓名: select studet.s# student.sname from student where s# not in (select distinct(sc.s#) from sc, course, teacher where sc.c# = course.c# and teacher.t# = course.t# and teacher.tname = "叶萍")
  6. 查询学过001和002课程的同学的姓名学号: select student.s3 student .sname from student join course on student.s# = course.s# where(course.c# = '001' union course.c# = '002')
  7. 查询学过叶萍老师课的同学的学号,姓名:select student.s# student.sname from student where s# in(select distinct(sc.s#) from sc course teacher where sc.c# = course.c# and teacher.t# = course.t# and teacher.tnmae = "叶萍") 
  8. 查询002成绩比001低的同学的学号,姓名:select student.s# student .name from student where student.s# in(select sc1.s# from sc sc1 join sc sc2 on sc1.s# = sc2.s# where s1.c# = 001 and s2.c# = 002 and sc1.score> sc2.score)
  9. 查询所有课程成绩小于60分的同学的学号,姓名:select student.s# student.sname from student join sc on student.s# = sc.s# where sc.score > 60;
  10. 查询没有学全 所有课的同学的学号和姓名:select student.s# student.sname from student,sc where student.s# = sc.s# group by student.s#,student.sname having count(c#) < (select count(c#)from course)
  11. 查询至少有一门课程与学号1001的同学所学相同的同学的学号和姓名:select student.s# ,student.sname from student join sc where student.s# = sc.s# and c# in select c# from sc where s# = '1001'
  12. 查询至少学过学号为001同学所有一门课的其他同学的学号和姓名:
    select distinct student.s#,student,sname from SC join student on SC.s# = student.s# where c# in(select c# from sc where s#='001')




你可能感兴趣的:(数据库)