最近在做oracle兼容,原来项目是用postgresql数据库,发现有许多区别,故就想把工作中遇到过的几种数据库整理一下。
在工作中,用到过的几种数据库,ms sqlserver,postgresql,oracle,现记录下来其中区别,以备以后查阅。
记录不全,只是记录遇到过的,以后遇到其他再慢慢补全。
一、sql语句的差别:其中sql语句基本差不多,只是有少许差别
1.查询前10条数据
.sqlserver
select top 10 * from T where ..condition order by 某一列名;
. postgresql
select * from T where ..condition order by 某一列名 limit 10;
. oracle
在oracle中,要实现查询前N条数据:不能用select top N,
要用order by 跟rownum组合来实现。
(1). select * from (select * from T order by 某一列名) t where rownum<=10; 先排序后取值
(2). select * from (select t1.* ,rownum rn from tabName t1 order by t1.colname) t2 where t2.rn<=10;
--不能对rownum使用">"
select * from emp where rownum > 10;注定要失败!
.mysql
select * from table where [condition] order by [condition] limit 10;
-------------------------------------------------------------------------------------------------------------
2. 表别名前能不能加as
oracle中,表别名前不能加as,用空格代替,列名前可以加;
错:select * from T as t ;
错:select * from (select rownum as rn,t1.* from T as t1) as t2 where t2.rn between 10 and 20;
其他数据库都可以加。
----------------------------------------------------------------------------------
3. 某列值是日期时间(datetime或stamp)类型
Oracle:
oracle中插入日期、日期时间格式必须to_date转换,比如'08-21-2003', '2012-12-10 11:05:21'
insert into student(subTime) values(to_date('08-21-2003','MM-DD-YYYY'));
insert into T(subTime) values (to_date('2012-12-10 11:05:21','yyyy-mm-dd hh24:mi:ss'));
oracle查询时间格式也必须要to_date转换
select count(1) FROM kakou_wzxx where subTime between to_date('2011/1/19 10:02:27','yyyy-mm-dd hh24:mi:ss') and to_date('2012/12/19 10:02:28','yyyy-mm-dd hh24:mi:ss')
select count(1) FROM kakou_wzxx where subTime>=to_date('2011/1/19 10:02:27','yyyy-mm-dd hh24:mi:ss') and subTime<=to_date('2012/12/19 10:02:28','yyyy-mm-dd hh24:mi:ss')
---------------------------------------------------------------------------------------------------------------
4. 分页语句
从第M条记录开始取N条记录
------------------------------
. sqlserver: (top)四种写法
第一种写法:
select top N *
from userinfo
where userId in
(select top (M+N-1) userId from userinfo where condition.. order by userId desc)
order by userId desc;
第二种写法:
select top N *
from userinfo
where userId not in
(select top M userId from userInfo order by userId asc)
order by userId
第三种写法:
select top N*
from userInfo
where userId>
(select max(userId) from (select top M userId from userInfo order by userId) a)
order by userId
第四种写法: (sqlserver 2005及以上版本)
select * from
(select Row_Number() over (order by userId) as RN, * from UserInfo) U
where U.RN between M and M+N ;
-----------------------------------------------
.postgresql : (limit)
select * from UserInfo
where name like '%a%'
order by id
limit N offset M;
---------------------------------------------------
.
oracle: (rownum)
oracle分页时必须用rownum,如果还有排序,则只能用子查询来实现先排序.
(1)
select *
from (select rownum rn, u1.* from userinfo u1 where condition.. order by condition..) u2
where u2.rn between 10 and 20 ;
(2)
select *
from (select rownum rn, u1.* from userinfo u1 where rownum<=20 order by name) u2
where u2.rn >10 ;
--------------------------------------------------------------------------------------------------------
.mysql: (limit)
select * from table where [condition] order by [condition] limit start,pageNum;
说明:
start:取数据的定位点 [开始是从0开始] pageNum:每次取多少
比如:start=10,pageNum=10 那么就是说,取11-20 条数据
------------------------------------------------------------------------------
二、在程序中引用的区别
1. 命名空间不同
.sqlserver
using System.Data.SqlClient;
对象:SqlConnection、SqlCommand、SqlParameter 、SqlDataAdapter
.postgresql
using System.Data.Odbc;
OdbcConnection、OdbcCommand、OdbcParameter、OdbcDataAdapter
.oracle
using System.Data.OracleClient;
OracleConnection、OracleCommand 、OracleParameter 、OracleDataAdapter
-----------------------------------------------------------------------------
2.参数化查询变量表示不同
.sqlserver
标识符用@,比如:
StringBuilder strSql = new StringBuilder();
strSql.Append("update T_User(name,phone,email) set name=@name,phone=@phone,email=@eamil;");
SqlParameter[] parameters = {
new SqlParameter("@name",SqlType.VarChar,50),
new SqlParameter("@phone",SqlType.VarChar,50),
new SqlParameter("@eamil",SqlType.VarChar,50)
}
parameters[0].Value= ..;
parameters[1].Value=..;
parameters[2].Value=..;
.postgresql
标识符用?
StringBuilder strSql = new StringBuilder();
strSql.Append("update T_User(name,phone,email) set name=?,phone=?,email=?;");
OdbcParameter[] parameters = {
new OdbcParameter("@name",OdbcType.VarChar,50),
new OdbcParameter("@phone",OdbcType.VarChar,50),
new OdbcParameter("@eamil",OdbcType.VarChar,50)
}
parameters[0].Value= ..;
parameters[1].Value=..;
parameters[2].Value=..;
.oracle
标识符用:
StringBuilder strSql = new StringBuilder();
strSql.Append("update T_User(name,phone,email) set name=:name,phone=:phone,email=:email;");
OracleParameter[] parameters = {
new OracleParameter(":name",OracleType.VarChar,50),
new OracleParameter(":phone",OracleType.VarChar,50),
new OracleParameter(":eamil",OracleType.VarChar,50)
}
parameters[0].Value= ..;
parameters[1].Value=..;
parameters[2].Value=..;
----------------------------------------------
3.在程序中获取DataTable时,表的列名有区别
oracle中,程序中查出来的DataTable里面的列名全部是大写,不管建表时是小写还是大写。
而在postgresql中,根据建表时列名是大写和小写有关。 是小写查询出来的列名就是小写。
鉴于这两者数据库有这种区别,所以在程序要兼容这两种数据库的时候,程序前台绑定时要注意:列名大小写的区别
$('#gvList').datagrid({
iconCls: 'icon-save',
autoRowHeight: true,
nowrap: true,
striped: true,
fit: true,
fitColumns: true,
pageSize: 30,
collapsible: false,
url: 'List.aspx?paramType=Query',
queryParams: params,
sortName: 'xxbh',
sortOrder: 'desc',
remoteSort: false,
singleSelect: true,
idField: 'xxbh',
columns: [[
{ field: 'hpzl', title: '号牌种类' , width: 150, align: 'center' },
{ field: 'hphm', title: '号牌号码' , width: 150, align: 'center' },
{ field: 'start_time', title: '有效期开始' , width: 150, align: 'center', formatter: timeFormatter },
{ field: 'end_time', title: '有效期结束' , width: 150, align: 'center', formatter: timeFormatter },
{ field: 'examineflow_type', title: '审核状态' , width: 80, align: 'center' },
{ field: 'temp_flow', title: '操作选项' , width: 150, align: 'center', formatter: oprFormatter }
]],
pagination: true,
rownumbers: true,
});
---------------------------------------------
$('#gvList').datagrid({
iconCls: 'icon-save',
autoRowHeight: true,
nowrap: true,
striped: true,
fit: true,
fitColumns: true,
pageSize: 30,
collapsible: false,
url: 'List.aspx?paramType=Query',
queryParams: params,
sortName: 'XXBH',
sortOrder: 'desc',
remoteSort: false,
singleSelect: true,
idField: 'XXBH',
columns: [[
{ field: 'HPZL', title: '号牌种类' , width: 150, align: 'center' },
{ field: 'HPHM', title: '号牌号码' , width: 150, align: 'center' },
{ field: 'START_TIME', title: '有效期开始' , width: 150, align: 'center', formatter: timeFormatter },
{ field: 'END_TIME', title: '有效期结束' , width: 150, align: 'center', formatter: timeFormatter },
{ field: 'APPROVE_FLOW', title: '审核状态' , width: 150, align: 'center' },
{ field: 'BKXXBH', title: '操作选项' , width: 150, align: 'center', formatter: oprFormatter }
]],
pagination: true,
rownumbers: true
});
当然为了兼容,并且列名大小写不一致的情况下,我的做法是:在程序中,对获取到的DataTable再做一次处理,即将所获取到的表列名全部转换为一致,要么全小写,要么全大写。方法很简单:
/// <summary>
/// 将Datatable中的列名转换为小写
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static DataTable ConvertColumnName2Lower( DataTable dt)
{
if (dt == null )
{
return null ;
}
for (int i = 0; i < dt.Columns.Count; i++)
{
dt.Columns[i].ColumnName = dt.Columns[i].ColumnName.ToLower();
}
return dt;
}
-----------------------------------------------------------------------------------------------------------------------------------------
以上就是工作中遇到的一些区别,肯定还有许多,以后会在工作中慢慢补充。