常见的oracle面试题以及sql注意事项部分试题
部分常见ORACLE面试题以及SQL注意事项一、表的创建:一个通过单列外键联系起父表和子表的简单例子如下:CREATE TABLE parent(id INT NOT NULL,PRIMARY KEY (id)CREATE TABLE child(id INT, parent_id INT,INDEX par_ind (parent_id),FOREIGN KEY (parent_id) REFERENCES parent(id)ON DELETE CASCADE)InnoDB Tables 概述:InnoDB 给MySQL 提供了具有事务(commit) 、回滚(rollback) 和崩溃修复能力(crash recovery capabilities)的事务安全(transaction-safe (ACID compliant)型表。InnoDB 提供了行锁(locking on row level),提供与Oracle 类型一致的不加锁读取(non-locking read in SELECTs)。这些特性均提高了多用户并发操作的性能表现。在InnoDB 表中不需要扩大锁定(lock escalation),因为InnoDB 的列锁定(row level locks)适宜非常小的空间。InnoDB 是MySQL 上第一个提供外键约束(FOREIGN KEY constraints)的表引擎。InnoDB 的设计目标是处理大容量数据库系统,它的CPU 利用率是其它基于磁盘的关系数据库引擎所不能比的。从一个表中查询出数据插入到另一个表中的方法:select * into destTbl from srcTbl ;insert into destTbl(fld1, fld2) select fld1, 5 from srcTbl ;以上两句都是将srcTbl 的数据插入到destTbl,但两句又有区别的。第一句(select into from)要求目标表(destTbl)不存在,因为在插入时会自动创建。第二句(insert into select from)要求目标表(destTbl)存在,由于目标表已经存在,所以我们除了插入源表(srcTbl)的字段外,还可以插入常量,如例中的:5。如果只想要结构而不要数据。create table s_emp_42 as select * from s_emp where 1=2;/永假式课堂笔记注意事项:1.1.DDL: create table.drop table 表名;(数据定义语言:Data Definition Language)2.DML: insert.into.update .delete .(数据操纵语言): Data Manipulation Language3.DQL:select.from.(数据查询语言:Data Query Language)2.-使用commit确认提交事务:Transaction3.-员工分布在哪些部门?每个部门显示一次select distinct deptno, job from employee_ning;4.distinct(去重复的)5.-模糊查询: like, -%:任意多个字符-_:代表一个字符6.-空值的查询-错的,空值不能使用=比较select ename, bonus from employee_ningwhere bonus = null;-对的select ename, bonus from employee_ningwhere bonus is null;7.-排序-order by salary; 薪水由低到高:正序select ename, salary, bonus from employee_ningorder by salary;-order by salary desc; 薪水由高到低排列:倒序select ename, salary, bonus from employee_ningorder by salary desc;order by列名 从高到底 +desc()从低到高查看表结构:desc(describe)倒序排序:desc(descend)8.数字函数select ENAME,salary*1.2345678 from tarena111;-截取select ename,trunc(salary*1.2345678,2) from tarena111;-四舍五入select ename,round(salary*1.2345678,2) from tarena111;9.日期select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from tarena111; to_date字符->日期 <- to_char to_char to_number日期 -> 字符 -> 数字 <- <- to_date to_charyyyy:四位数字年,y=year;mm:两位数字月。m=month;dd:两位数字日期,d=day;hh24:24小时制的时间,h=hour;mi:分minutess:秒 secondmon:缩写的月month:全拼的月day:星期几am/pm:上午/下午select to_char(sysdate,'year month dd day am')from dual;例如:三天以前的日期select sysdate-3 from dual;七天以后的日期select sysdate+7 from dual;计算员工入职的天数,四舍五入到整数select ename, hiredate,round (sysdate-hiredate,0) from tarena111;=select ename, hiredate, round (sysdate-hiredate) from tarena111;计算员工入职的月数,四舍五入到整数-hire是别名select ename,round(months_between(sysdate,hiredate)hire from tarena111;单行函数可以嵌套多层,先从最里面执行,逐层向外运算select ename,round(months_between(to_date('2012-01-01','yyyy-mm-dd'),hiredate)hire from tarena111;10. (1)求和sumselect sum(salary) from tarena111;(2)求平均值select avg(salary) from tarena111;select avg(bonus) from tarena111;-bonus有空值,sum(bonus)/count(bonus)-忽略所有bonus是null的记录,不合逻辑select avg(bonus) from tarena111;-改进select sum(bonus)/count(bonus) from tarena111;select avg(nvl(bonus,0) from tarena111;11.-t1:驱动表, t2:匹配表t1 join t2 on t1.c1 = t2.c2t2 join t1 on t1.c1 = t2.c2-上述两种形式,结果集相同.顺序可能不同./*遍历驱动表,在匹配表中寻找满足等值连接条件的记录.1)在匹配表中找到一条,驱动表中的记录和匹配表中的记录组合,结果集中的一条.2)在匹配表中找到多条(例如,10条),驱动表中的记录和匹配表中的多条记录组合,结果集中的多条(10条).3)在匹配表中遍历的结果,一条都没找到,驱动表中此条记录放弃,不出现在最终结果集中.11.-自连接,是等值连接的一种特殊形式-查询员工名字和他的经理名字select worker.ename, manager.enamefrom employee_ning workerjoin employee_ning manageron worker.mgr = manager.empno;12.去空格trim("kingsu ") = "kingsu"13.分组查询group by 列名: 按列名分组,细粒度having 组函数的条件14.表间关系外键: foreign key: FKStudent ClassPK FK->PK id name age cid id name11 scott 18 2 1 1106班12 tiger 19 1 2 1105班13 tom 17 3 1104班-只有两条有班级的学生select s.name, c.namefrom student_wh s join class_wh con s.cid = c.id;15.复制表先创建一个新表,再select要复制的表的所有数据,用as来复制create table salgrade_wh1asselect * from salgrade_wh;-利用子查询向表中新增多条记录insert into emp_bak_wh(select * from employee_whwhere deptno = 10);-可以一步实现:create table emp_bak_wh(empno, ename, job, salary, depton)asselect empno, ename, job, salary, deptnofrom employee_whwhere deptno = 10;16.左连接与外连接-把没有部门的员工也列出来,-希望得到下述结果:1001 liucs developer bj1002 zwj developer bj1003 zsf account tj1009 wxb 17. 集合(结果集)操作-A并B,-union:去掉重复记录, 排序(消耗性能)-union all: 不去重,不排序/*如果结果集A/B没有重复记录,且不需要排序,则首选union all.*/select empno, ename, salary, deptnofrom employee_whwhere deptno = 20union allselect empno, ename, salary, deptno from employee_whwhere salary > 7000;-交集select empno,