1、自增長列的插入:
SQLServer中可以不為自動增長列插入值,
MySQL中需要為自動增長列插入值。
2、獲取當前時間函數:
SQLServer寫法:getdate()
MySQL寫法:now()
3、從數據庫定位到表。
Sqlserver寫法:庫名.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 DATABASEif 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
(關於On duplicate key update的兩篇文章,推薦給大家!
可遇不可求的Question之SQLServer的INSERT ON DUPLICATE KEY UPDATE語法篇
MySql避免重復插入記錄方法(ignore,Replace,ON DUPLICATE KEY UPDATE)
)
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';
(注:若想知道[type]='U'代表什么意思,請點擊http://blog.csdn.net/winddai/article/details/5815138)
MySQL寫法:
SHOW TABLES
10、某些關鍵詞的使用
10.1截取字符串
SQLServer只能使用SUBSTRING關鍵詞來截取字符串。
MySQL可以使用SUBSTRING和SUBSTR截取字符串
10.2取得字符串的長度
SQLServer只能使用Len關鍵詞取得字符串的長度。
MySQL可以使用Length取得字符串的長度。
11、相同點
delete,select,insert,drop(刪除數據庫:drop database 庫名),update,create(創建數據庫:create
database 庫名)語句一樣。
---------------------
https://blog.csdn.net/shiqijiamengjie/article/details/50396793