1. 程式人生 > >主流資料庫常用內容對比

主流資料庫常用內容對比

##一、主流資料庫常用內容對比

###1、表名和列名使用時候區分大小寫

  • Sqlserver:不區分,和語句保持一致
  • Oracle:設計全部顯示為大寫,查詢結果以大寫展示
  • MySql: 設計中將以小寫顯示,查詢以小寫展示【lower_case_table_names = 1,】
  • PostgreSql:設計建議小寫,查詢時候全部按小寫 (如果需要區分大小寫,需要使用’,如: ‘tableName’)
  • SqlLite:不區分,和語句保持一致

###2、檢視

  • Sqlserver:支援
  • Oracle:支援
  • MySql:支援
  • PostgreSql:支援
  • SqlLite:支援

###3、儲存過程

  • Sqlserver:支援
  • Oracle:支援
  • MySql:支援
  • PostgreSql:支援
  • SqlLite:不支援

###4、函式

  • Sqlserver:支援
  • Oracle:支援
  • MySql:支援
  • PostgreSql:支援
  • SqlLite:支援

###5、分頁

  • Sqlserver:
通用:ROW_NUMBER() OVER()方式
select * from ( 
    select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId from ArtistModels 
  ) as b
      where RowId between 10 and 20 
備註:---where RowId BETWEEN 當前頁數-1*條數 and 頁數*條數---     
SQL2012以上的版本才支援:offset fetch next方式(:推薦使用 )
select * from ( 
    select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId from ArtistModels 
  ) as b
      where RowId between 10 and 20 
備註:--order by ArtistId offset 頁數 rows fetch next 條數 rows only ----
適應於資料庫2012以下的版本:top not in方式
select top 3 * from ArtistModels 
where ArtistId not in (select top 15 ArtistId from ArtistModels)
備註:-where Id not in (select top 條數*頁數  ArtistId  from ArtistModels)  
  • Oracle:
方式一(效率高):
SELECT * FROM  
(  
SELECT A.*, ROWNUM RN  
FROM (SELECT * FROM TABLE_NAME) A  
WHERE ROWNUM <= 40  
)  
WHERE RN >= 21  
方式二:
SELECT * FROM  
(  
SELECT A.*, ROWNUM RN  
FROM (SELECT * FROM TABLE_NAME) A  
)  
WHERE RN BETWEEN 21 AND 40  
  • MySql:
語法:SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
案例:select * from orders_history where type=8 order by id limit 10000,10;
  • PostgreSql:
語法:select * from persons limit  A  offset  B;
案例:select * from persons limit 5 offset 0 ;
解釋:A就是你需要多少行;B就是查詢的起點位置
  • SqlLite:
案例:SELECT * FROM Table ORDER BY ID DESC Limit 10,9
解釋:limit語義:跳過10行,取9行

###6、按日期範圍查詢

  • Sqlserver:
select * from table1 where t1>='2017-6-1' and t1<='2017-6-5'
  • Oracle:
SELECT * FROM TB_NAME t WHERE to_date(,'yyyy-MM-dd HH24:MI:SS')
between to_date('2011-07-01 12:00:00','yyyy-MM-dd HH24:MI:SS') and to_date('20110702 12:00:00','yyyymmdd HH24:MI:SS') ;
  • MySql:
select fullName,addedTime FROM t_user where addedTime >='2017-1-1 00:00:00'  and addedTime < '2018-1-1 00:00:00';
  • PostgreSql:
select * from user_info where create_date >= '2015-07-01' and create_date < '2015-08-15';
  • SqlLite:
select * from table1 where t1>='2017-6-1' and t1<='2017-6-5'

###7、模糊查詢

  • Sqlserver:
萬用字元 含義
1、% 包含零個或更多字元的任意字串。
2、_(下劃線) 任何單個字元。3、
4、[ ] 指定範圍(例如 [a-f])或集合(例如 [abcdef])內的任何單個字元。
5、[^] 不在指定範圍(例如 [^a - f])或集合(例如 [^abcdef])內的任何單個字元。
6、Like特殊情況:搜尋萬用字元字元   WHERE ColumnA LIKE '%5/%%' ESCAPE '/'
  在 LIKE 子句中,前導和結尾百分號 (%) 解釋為萬用字元,而斜槓 (/) 之後的百分號解釋為字元%。
  • Oracle:
1、%:
SELECT * FROM [user] WHERE uname LIKE ‘%三%’ ;
2、_:單一任何字元(下劃線)常用來限制表示式的字元長度語句
SELECT * FROM [user] WHERE uname LIKE ‘三__’;
3、[]:在某一範圍內的字元,表示括號內所列字元中的一個(類似正則表示式)
SELECT * FROM [user] WHERE u_name LIKE ‘[張李王]三’ ;
4、[^]: 不在某範圍內的字元,用法與[ ]相反
5、在Oracle中提供了instr(strSource,strTarget)函式,比使用’%關鍵字%’的模式效率高很多。
instr函式也有三種情況:
instr(欄位,’關鍵字’)>0相當於 欄位like ‘%關鍵字%’  ;
instr(欄位,’關鍵字’)=1相當於 欄位like ‘關鍵字%’  ;
instr(欄位,’關鍵字’)=0相當於 欄位not like ‘%關鍵字%’  。
特殊用法: 
select id, namefrom user where instr(‘101914, 104703’, id) > 0; 
它等價於 
select id, namefrom user where id = 101914 or id = 104703;
  • MySql:
1、同Sqlserver和Oracle前4點
2、LOCATE('substr',str,pos)方法
SELECT LOCATE('bar',`foobarbar`);  ###返回4
3、POSITION('substr' IN `field`)方法
SELECT `column` FROM `table` WHERE POSITION('keyword' IN `filed`)
4、INSTR(`str`,'substr')方法
SELECT `column` FROM `table` WHERE INSTR(`field`, 'keyword' )>0 
5、FIND_IN_SET(str1,str2):
SELECT * FROM `person` WHERE FIND_IN_SET('apply',`name`);
  • PostgreSql:
1、字首+模糊查詢。(可以使用b-tree索引)
select * from tbl where col like 'ab%';    
或   
select * from tbl where col ~ '^ab';  
2、字尾+模糊查詢。(可以使用reverse(col)表示式b-tree索引
select * from tbl where col like '%ab';    
或  
select * from tbl where col ~ 'ab$';    
寫法   
select * from tbl where reverse(col) like 'ba%';    
或    
select * from tbl where reverse(col) ~ '^ba'; 
3、前後模糊查詢。(可以使用pg_trgm和gin索引)
select * from tbl where col like '%ab%';    
或    
select * from tbl where col ~ 'ab';  
4、全文檢索。(可以使用全文檢索型別以及gin或rum索引)
select * from tbl where tsvector_col @@ 'postgres & china | digoal:A' order by ts_rank(tsvector_col, 'postgres & china | digoal:A') limit xx; 
5、正則查詢。(可以使用pg_trgm和gin索引)
select * from tbl where col ~ '^a[0-9]{1,5}\ +digoal$';  
6、相似查詢。(可以使用pg_trgm和gin索引)
select * from tbl order by similarity(col, 'postgre') desc limit 10;  
7、ADHOC查詢,任意欄位組合查詢
select * from tbl where a=? and b=? or c=? and d=? or e between ? and ? and f in (?);  
8、關鍵字
SELECT * FROM 表名 WHERE 欄位 ILIKE regexp_replace(concat('%','關鍵字','%'),'\\','\\\','g')
  • SqlLite:
1、同Sqlserver和Oracle前4點
"select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address

###8、字串連線符

  • Sqlserver:
1、使用 +
select 'Post'+'gresql'+' good!';
2、使用 CONCAT
SELECT CONCAT('學號:',XNumber,'的綜合成績:',FSalary/(FAge-21)) 
FROM user
  • Oracle:
1、使用 ||
select 'Post'||'gresql'||' good!';
2、使用 CONCAT
SELECT CONCAT('學號:',XNumber,'的綜合成績:',FSalary/(FAge-21)) 
FROM user
  • MySql:
1、使用 ||,如果是純數字拼接,不能使用,得出結果為 1||2=3
select 'Post'||'gresql'||' good!';
2、使用 CONCAT
SELECT CONCAT('學號:',XNumber,'的綜合成績:',FSalary/(FAge-21)) 
FROM user
  • PostgreSql:
1、使用 ||
select 'Post'||'gresql'||' good!';
2、使用 CONCAT
SELECT CONCAT('學號:',XNumber,'的綜合成績:',FSalary/(FAge-21)) 
FROM user
  • SqlLite:
1、使用 ||
SELECT 'I''M '||'Chinese.' 

###9、主鍵自增 和返回值

  • Sqlserver:
1、建立:id int identity(1,1),
2、返回值
@@IDENTITY 返回為當前會話的所有作用域中的任何表最後生成的標識值。
SCOPE_IDENTITY 返回為當前會話和當前作用域中的任何表最後生成的標識值
SCOPE_IDENTITY 和 @@IDENTITY 返回在當前會話中的任何表內所生成的最後一個標識值。但是,SCOPE_IDENTITY 只返回插入到當前作用域中的值;@@IDENTITY 不受限於特定的作用域。
  • Oracle:
1、建立
建立表
Create table t_user(Id number(6),userid varchar2(20),loginpassword varchar2(20),isdisable number(6) );
建立序列
create sequence user_seq increment by 1 start with 1 nomaxvalue nominvalue nocache
建立觸發器
create or replace trigger tr_user
before insert on t_user
for each row
begin
select user_seq.nextval into :new.id from dual;
end;
測試
insert into t_user(userid,loginpassword, isdisable) values('ffll','liudddyujj', 0);
insert into t_user(userid,loginpassword, isdisable) values('dddd','zhang', 0) ;
select * from t_user;
2、返回值 select user_seq.CURRVAL from dual
  • MySql:
1、建立:id int primary key auto_increment
2、返回值:SELECT LAST_INSERT_ID()
  • PostgreSql:
1、建立:新建欄位  並且將欄位屬性設定為serial
2、新增返回:insert into point(pointtype,pointx,pointy,pointval)values(1,2,3,4) RETURNING id;
  • SqlLite:
1、建立:id INTEGER PRIMARY KEY AUTOINCREMENT
2、返回值:select last_insert_rowid() from person
3、自增自增歸零:DELETE FROM sqlite_sequence WHERE name='TableName';
4、特定欄位自增歸零:UPDATE sqlite_sequence SET seq = 0 WHERE name='TableName';

###10、變數定義符號

  • Sqlserver:
1、@
  • Oracle:
1、:
  • MySql:
1、@
  • PostgreSql:
1、:
  • SqlLite:
1、不支援

###11、預設時間函式

  • Sqlserver:
1、建立預設時間: datetime default getdate()
  • Oracle:
1、建立預設時間:date default sysdate
  • MySql:
1、建立預設時間: datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '建立時間' ;
  • PostgreSql:
1、建立預設時間:datetime varchar(40) DEFAULT (now())
  • SqlLite:
1、建立預設時間: datetime default (datetime(current_timestamp,'localtime'))