当前位置:操作系统 > Unix/Linux >>

最近使用数据库的一点总结

最近使用数据库的一点总结
 
最近由于一个项目比较特殊,需要适应不同的数据,即oracle,mysql,mssql。对这三个数据库做了些简单的对比,如下:
Oracle:
查看表是否存在:select * from all_tables 或 select * from user_tables;
获取 当前表的自增序列: select 序列.nextval from dual;
异常处理: exception
  when others then
日期加一天: sysdate+1
日期加一小时:sysdate+1/24
日期加一分钟: sysdate+1/1440
日期加一秒:  sysdate+1/86400
 
=======================================================================================
MSSQL:
查看是否存在:select * from sysobjects
获取 当前表的自增序列: select SCOPE_IDENTITY()  
异常处理:
if @@error<>0 
begin
end
事务(在mssql中,要手动开始事务): begin transaction   commit transaction   rollback transaction   ;是
日期加一天: dateadd(day,1,getdate())
日期加一小时:dateadd(hour,1,getdate())
日期加一分钟: dateadd(minute,1,getdate())
日期加一秒:  dateadd(second,1,getdate())
=======================================================================================
mysql:
查看是否存在:select  * from information_schema.`TABLES`;
获取 当前表的自增序列:select LAST_INSERT_ID();
不要使用这种异常处理: if @@error_count<>0 then
    end if;
正确的使用异常处理:decalre continue handler for sqlexception set flag = null;
事务(在mysql中,事务默认开启的): start transaction   commit transaction   rollback transaction ;
过程中,不能有retun关键字。
临时表只能查询一次;
视图中不能有子查询;
update、delete 中 不能 出现当前操作的表,比如:delete from t_user where  userid = (select userid from t_user where username='aaa'),这种写法是不允许的。
日期加一天: date_add(sysdate(),interval '1' day)
日期加一小时:date_add(sysdate(),interval '1' day_hour)
日期加一分钟: date_add(sysdate(),interval '1' day_minute)
日期加一秒:  date_add(sysdate(),interval '1' day_second)
 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,