各数据库SQL语句差异

来源:互联网 发布:笑傲江湖武功排名知乎 编辑:程序博客网 时间:2024/06/05 21:12

因为数据库SQL语句在各大产品中带有“方言性”,即SQLSERVER SYBASE都是用了T-SQL,Mysql是用的标准SQL,Oracle有有自己的PL/SQL。由于这种“方言性”的差异导致很多SQL语句在移植的时候产生困难,在异构数据库的时候显得很尴尬。当然对于DBA来说,无非是查查语法而已的事。但是终究是不方便。下面看看各数据库之间的语句差异

一、分页查询

1、SQL Server

select top X * from table_name  --查询前X条记录,可以改成需要的数字。select top n * from (select top m * from table_name order by column_name ) a order by column_name desc  --查询第N到M条记录。常用的分页也是这种方式。例如常用的分页方式:declare @page intdeclare @row intset @page=2 --页数set @row=3  --每页展示行数select top (@row) * from (select top (@row*@page) * from table_name order by id  ) a order by id desc  --最基本的分页方式,改变@row和@page达到分页效果

2、MySQL

select * from table_name limit 0,10  --通常0是可以省略的,直接写成  limit 10。0代表从第0条记录后面开始,也就是从第一条开始select * from table_name limit 1,10  --则为从第一条后面的记录开始展示,也就是说从第二条开始。

3、Oracle

select * from table_name where rownum<X --X为前多少条记录select * from (select a.*,a.rownum rn from (select * from table_name) a where a.rownum<M) where rn>n --这句就是从n到m也的数据,分为三层结构


二、表结构复制

1、SQL Server

--当数据库中没有新表的情况,比如有了A,没有B表。 select * into B from A --复制表及数据 select * into B from A where 1>1 --只复制表结构 --当数据中已经有了B表的情况,上面就不适用了。 insert into B select * from A --复制表数据,为了避免。B表理论上应该没有数据,如果有,可能会造成违反主键。 insert into B select * from A where id>10 --加上where条件可以指定复制数据,上面没有表的情况也可以这样做。 这里要说的是,sybase和SQLSERVER是一样的。因为从某种角度来说,SYBASE就是SQL的原型。 

2、MySQL

--数据库中没有B表的情况 create table B select * from A create table B select * from A where 1<>1 --只复制表结构 --有B表的情况和sqlserver基本相同 insert into B select * from A 

 

3、Oracle

oracle 基本上合MYSQL是一样的,不过语法要求更严谨。当然Mysql也可以这样写。 --数据库中没有B表的情况 create table B as select * from A create table B as select * from A where 1<>1 --只复制表结构 create table B like A--复制表结构 --有B表的情况基本相同 insert into B select * from A 
0 0