一、问题:你在实际项目中是如何进行数据库建模的?
1、需求分析,找出所涉及的各种信息
2、进行概念数据模型设计
2.1、找实体
2.2、找实体的属性
2.3、找实体之间的关系 (与客户讨论,修改CDM)
3、根据CDM产生PDM,对PDM进行修改。
4、后面需求还有变化,但只在有必要的情况下再修改数据库。
二、SQL性能分析?
规则:SELECT子句中避免使用 ‘ * ‘
规则:使用exists语句代替in语句;
使用not exists代替not in
规则:使用truncate代替delete from 表语句
规则:减少访问数据库的次数
规则:使用表的别名(Alias)
规则:尽快使用COMMIT
规则:数据库冗余字段的设计
其它:Oracle与性能有关的几个特征(三个缓冲区)
三、如何快速导入导出大数据量(1000万条)的表(如t_employee)
步骤:
1、从数据库1中导出数据
2、使用delete删除数据2中相同表的所有数据。
3、导入。
思考:以上步骤对吗?
分析:错误有2。
1、不应该使用delete删除大量数据。
2、在数据导入时,数据库系统将会同时维护索引。所以效率会很低。
正确的做法是:
1、从数据库1中导出
2、删除数据库2中对应表中的所有索引对象
3、使用truncate命令清除该表的所有数据
4、导入(此时数据库不会再维护索引,效率最高)
5、重建相关索引(使用脚本)
四、需求:某网站每天都有大量访问,所以需要统计网站流量。表有两个字段,分别是访问时间字段:log_Time,访问IP字段:IP)
问题1:请统计每个小时(0-1,1-2....-24)的访问次数。
问题2:哪个时段访问是最高峰。
select t.*, t.rowid from t_access t
--方法:得到要统计的原始信息(假设今天是2008-03-02)
select to_char(t.log_time,'hh24') as hour,ip
from t_access t
where to_char(t.log_time,'yyyy-mm-dd')='2008-03-02'
--(必须记住)日期格式:yyyy-mm-dd hh24:mi:ss
--统计
select to_char(t.log_time,'hh24') as hour,count(ip) as access_count
from t_access t
where to_char(t.log_time,'yyyy-mm-dd')='2008-03-02'
group by to_char(t.log_time,'hh24')
--问题2:哪个时段访问是最高峰。
--说明:这个问题与求人数最多的班级一样,要分析出来得分两步才能求出来。
--第一步:最高峰是?次
select max(count(ip)) as access_count
from t_access t
where to_char(t.log_time,'yyyy-mm-dd')='2008-03-02'
group by to_char(t.log_time,'hh24')
--第二步:哪个时段=?次
select to_char(t.log_time,'hh24') as hour,count(ip) as access_count
from t_access t
where to_char(t.log_time,'yyyy-mm-dd')='2008-03-02'
group by to_char(t.log_time,'hh24')
having(count(ip)=(
select max(count(ip)) as access_count
from t_access t
where to_char(t.log_time,'yyyy-mm-dd')='2008-03-02'
group by to_char(t.log_time,'hh24')
)
)
--其它:按客户要求的格式显示:
select to_char(t.log_time,'hh24')||'-'||(to_char(t.log_time,'hh24')+1) as hour,
count(ip) as access_count
from t_access t
where to_char(t.log_time,'yyyy-mm-dd')='2008-03-02'
group by to_char(t.log_time,'hh24')
-----------参考脚本------------
create table T_ACCESS
(
LOG_TIME DATE,
IP CHAR(17)
);
insert into T_ACCESS (LOG_TIME, IP)
values (to_date('01-03-2008 00:00:12', 'dd-mm-yyyy hh24:mi:ss'), '192.169.1.190 ');
insert into T_ACCESS (LOG_TIME, IP)
values (to_date('02-03-2008 00:00:30', 'dd-mm-yyyy hh24:mi:ss'), '130.169.12.78 ');
insert into T_ACCESS (LOG_TIME, IP)
values (to_date('02-03-2008 01:00:30', 'dd-mm-yyyy hh24:mi:ss'), '192.169.13.192 ');
insert into T_ACCESS (LOG_TIME, IP)
values (to_date('02-03-2008 02:02:00', 'dd-mm-yyyy hh24:mi:ss'), '192.169.1.80 ');
insert into T_ACCESS (LOG_TIME, IP)
values (to_date('02-03-2008 02:02:30', 'dd-mm-yyyy hh24:mi:ss'), '202.169.1.80 ');
commit;
五、如何限制一个字段中录入的数据?
说明:在数据库中,这项技术就是约束?
参考答案:
1.主键(Primary key) 2.非空(Not Null) 3.默认(default)
4.唯一(unique key) 5.检查(Check) 6.外键
7.自定义完整性(在补贴表中添加触发器。当补贴表中添加记录时,表上触发器自动执行,到员工表中检查该员工的年龄是大于50,
如果不是,就执行rollback,从而避免添加补贴不)。