Mysql數據庫安裝使用教程03:MySQL和Sql Server的sql語句區別

  1. 自增長列的插入:
    SQLServer中可以不爲自動增長列插入值,
    MySQL中需要爲自動增長列插入值。

  2. 獲取當前時間函數:
    SQLServer寫法:getdate()
    MySQL寫法:now()

  3. 從數據庫定位到表:
    Sqlserver寫法:庫名.dbo.表名(注:中間使用一個點) ;或者:庫名..表名(注:中間使用兩個點)

    庫名.dbo.表名,寫法:
    select password from Info.dbo.users where userName='boss'
    
    庫名..表名
    select password from Info..users where userName='boss'
    

    mysql寫法:庫名.表名

    select password from Info.users where userName='boss'
    
  4. 判斷是否存在某個數據庫,若存在,則刪除:
    Sqlserver寫法:

    IF DB_ID('users') IS NOT NULL DROP DATABASE users
    

    Mysql寫法:

    Drop DATABASE if exists users
    

    拓展:若sqlserver數據庫正在使用中,刪除之前,先要把數據庫變成“單一用戶”,再刪除
    ALTER DATABASE users SET SINGLE_USER with ROLLBACK IMMEDIATE IF DB_ID(‘users’) IS NOT NULL DROP DATABASE users

    另附:判斷某數據庫中是否存在某張表,若存在,則刪除
    Sqlserver寫法:
    if exists(select * from sysobjects where name =‘Users_test’)
    drop table Users_test
    Mysql寫法:
    DROP TABLE IF EXISTS Users_test

  5. 主鍵存在,則更新,不存在,則插入:
    Mysql寫法:
    INSERT into users (userID,userName,password) VALUES (1,’jmj’,’123’) ON DUPLICATE KEY UPDATE userName
    =‘jmj’, password =123

    Sqlserver沒有mysql這樣的關鍵字,只能組合sql語句來實現操作:
    if not exists (select userID from users where userID= 1)insert into users (userID,userName,password) values(1,’jmj’,’123’) else update users set userName
    = ’jmj’, password=’123’ where userID = 1

  6. 符號的使用:
    mysql對參數可以使用單引號,也可以使用雙引號,對字段名和表明可以使用反引號。
    sqlserver只能使用單引號,且不能使用反引號。
    Mysql寫法:
    Select password from Users where userName=‘boss’ or username=”jmj”
    Sqlserver寫法:
    Select password from Users where userName=‘boss’ or username=’jmj’

  7. 取出查詢結果中的第一條數據或者前幾條記錄(取前幾條記錄只需要修改對應的數字即可),分頁也是使用這個關鍵字:
    SQLServer寫法:
    select top 1 password from users where userName=‘boss’
    MySQL寫法:
    select password from users where userName='111’limit 0,1
    它可以規定範圍 limit a,b——範圍a-b

  8. 查詢所有庫:
    SQLServer寫法:
    select * from [master]…[SysDatabases];
    MySQL寫法:
    SHOW DATABASES;

  9. 查詢指定庫中的所有表:
    SQLServer寫法:
    select *from 庫名.dbo.[SysObjects] where[type]=‘U’;
    MySQL寫法(需要先進入這個數據庫才能使用這個命令查看):
    SHOW TABLES

  10. 截取字符串:
    SQLServer只能使用SUBSTRING關鍵詞來截取字符串。
    MySQL可以使用SUBSTRING和SUBSTR截取字符串

  11. 取得字符串的長度:
    SQLServer只能使用Len關鍵詞取得字符串的長度。
    MySQL可以使用Length取得字符串的長度。

  12. 相同點:
    delete,select,insert,drop(刪除數據庫:drop database 庫名),update,create(創建數據庫:create database 庫名)語句一樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章