Oracle百问百答之进阶提高(六)
51.delete、drop和truncate的区别
Delete:删除数据,表结构还在,可以恢复(savepoint),缺点是删除速度慢
Drop:删除数据和表结构,可以闪回(flashback)[cascade:级联删除]
Truncate:删除数据,表结构还在,无法找回,优点是删除速度快
52.主键和外键指的是什么?
能够唯一标识一条记录的属性列(组)就是该表的主键。主键是唯一的,且必须非空。而外键是另外一张表中与主表的某个字段的类型、字段名相同的字段(关联字段),外键的作用是关联两张或者两个以上表,保持数据一致性、完整性。只要主表上的关联字段是唯一或者主键,均可以被外键引用,但须注意的是外键不能是复合键。
53.查看哪个表被锁?
Select sess.sid,sess.serial#, lo.oracle_username,lo.os_user_name,ao.object_name,lo.locked_mode
from v$locked_object lo,dba_objects ao,v$session sess
where ao.object_id = lo.object_id
and lo.session_id = sess.sid;
54.left join、right join、inner join full join,union,union all的区别
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行
full join 返回两张表中的行 left join+right join
UNION在进行表链接后会筛选掉重复的记录
UNION ALL操作只是简单的将两个结果合并后就返回
55.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断
select * from t where id in(select id from t group by id having count(*)>1 );
分析:先用分组函数group by 和聚合函数count()统计出有重复的id,再根据id查找重复的记录。
56.删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,重复的只保留一行
delete from t where id in(select id from t group by id having count(*)>1) and rowid not in
(select min(rowid) from t group by id having count(*)>1);
分析:先假设删除全部重复的数据,再从要被删除的数据中剔除(也就是保留)rowid最小的
57.查找表中多余的重复记录(多个字段)
select * from t where (id,name) in(select id,name from t group by id,name having count(*)>1 );
58.删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from t where (id,name) in(select id,name from t group by id,name having count(*)>1 ) and rowid not in
(select min(rowid) from t group by group by id,name having count(*)>1 );
59.怎样生成执行计划?
有3种方式a、explain plan for sql语句; select * from table(dbms_xplan.display);b、sqlplus中set autotrace on;c、toad中Ctrl+E,pl/sqldeveloper中F5
60.怎样创建表空间?
create tablespace tsp_jh
datafile 'd:\oracle\product\10.2.0\oradata\apjh.dbf'
size 20m
reuse
autoextend on
next 5m
maxsize unlimited
logging
permanent
extent management local autoallocate
blocksize 8k
segment space management manual
flashback on;