1.运算符的差异
不同数据库系统中字符串的拼接方式不同,主流数据库对字符串拼接的支持如下:
1)MYSQL
concat:支持一个或多个参数,如concat('1','2','3')
concat_ws:可以在待拼接的字符串之间加入指定的分隔符,如concat_ws('1','x','2')
2)MSSQLServer
+:如'1'+'2'
3)Oracle
||:如'1'+'2'
concat():只支持2个参数
4)DB2
||:如'1'+'2'
2.常用SQL的区别
1)限制结果集行数
A.MYSQL:用limit关键字用来限制返回的结果集
select * from x order by y desc limit 3,5;
B.MSSQLServer:用top关键字返回结果集中的前N条记录
select top 10 * from x order by y desc;
C.MSSQLServer2005可以用窗口函数row_number()实现限制结果集行数
select row_number() over(order by y),x,y,z from a;
D.Oracle:rownum、row_number()
E.DB2:row_number()、fetch
select * from x order by y desc fetch first 8 rows only;
2)删除索引
A.MYSQL
drop index idx1 on employ;
B.MSSQLServer
drop index employ.idx1;
C.Oracle、DB2
drop index idx1;
3.取元数据信息的差异
元数据:如数据库的名称、当前用户名、数据库中的表、指定表的字段定义
1)取数据库信息
A.MYSQL
database():返回当前数据库名
version():以一个字符串形式返回
user()、system_user、session_user:返回当前MySQL的用户名
B.MSSQLServer
app_name():返回当前会话的应用程序名称
current_user:返回当前登录用户名
host_name():返回工作站命名
注:在MSSQLServer中若查询当前数据库名,则必须到系统表sysprocesses中查询,如:
select
dbname =
case when dbid = 0 then null
when dbid <> 0 then db_name(dbid)
end
from master..sysprocesses
where spid = @@SSID
系统表“master..sysprocesses”中存储了当前数据库系统中的进程信息,而“@@SSID”则表示当前进程号。
C.Oracle
user:当前登录用户名,如select user from dual;
userenv():当前登录用户相关的环境信息,只有一个参数,参数的可选值如下:ISDBA、LANGUAGE、TEMINAL、SESSIONID、ENTRYID、LANG、INSTANCE
D.DB2
current_user:当前登录用户名
current_server:取得当前服务名,如select current_user,current_server from sysibm.sysdummy1
sysibm.sysversions:当前数据库的版本的SQL
2)取得所有表
A.MYSQL
show tables
B.MSSQLServer
sysobjects:其中stype='U',如select name from sysobjects where xtype='U';
C.Oracle
all_objects:其中Object_Type='TABLE',如select Object_Name from all_objects where Object_Type='TABLE';
D.DB2
all_syscat.tables:定义了表和视图,其中TYPE='T',如SELECT TABNAME FROM syscat.tables where TYPE = 'T';
3)取得指定Schema下的表
A.MYSQL:(假设Schema名为demoschema)SHOW TABLES FROM demoschema
B.MSSQLServer
sysobjects:其中xtype='U'。(假设Schema名为demoschema)如:select name from demoschema.sysobjects where xtype = 'U';
C.Oracle
all_objects:其中Object_Type='TABLE',OWNER='Schema'。(假设Schema名为demoschema)如:select Object_Name from all_objects where Object_Type='TABLE' and OWNER='demoschema'
D.DB2
all_syscat.tables:其中TYPE='T',TABSCHEMA='Schema'。(假设Schema名为demoschema)如:select TABNAME from syscat.tables where TYPE='T' and TABSCHEMA='demoschema'
4)取得指定表的字段定义
A.MYSQL:(假设表名为mytable):DESCRIBE mytable
B.MSSQLServer
sysobjects:(假设表名为mytable):
SELECT syscols.name as COLUMN_NAME,
st.name as DATA_TYPE,
syscomm.text as DATA_DEFAULT,
syscols.isnullable as NULLABLE
FROM syscolumns syscols
LEFT JOIN systypes st
ON syscols.xusertype = st.xusertype
LEFT JOIN syscomments syscomm
ON syscols.cdefault = syscomm.id
WHERE syscols.id = OBJECT_ID(N'mytable')
ORDER BY syscols.id,syscols.colorder;
C.Oracle
all_tab_columns:其中TABLE_NAME为表名,(假设表名为mytable)如:
SELECT COLNAME as COLUMN_NAME,TYPENAME as DATA_TYPE,DEFAULT as DATA_DEFAULT,NULLS as NULLABLE FROM syscat.columns where TABNAME = 'MYTABLE';
D.DB2
syscat.columns:其中TABNAME为表名,(假设表名为mytable)如:
SELECT COLNAME as COLUMN_NAME,TYPENAME as DATA_TYPE,DEFAULT as DATA_DEFAULT,NULLS as NULLABLE FROM syscat.columns WHERE TABNAME ='mytable'