分享好友 数据库首页 频道列表

MySQL和Sql Server的sql语句区别

数据库其他  2023-02-10 02:430
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://www.cnblogs.com/twoheads/p/9843224.html

查看更多关于【数据库其他】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
去重复的sql(Oracle) 去重复的英文
1.利用group by 去重复2.可以利用下面的sql去重复,如下  1) select id,name,sex from (select a.*,row_number() over(partition by a.id,a.set order by name) su from test a ) where su=1  2)select id,name,sex from (select a.*,row_number() over(p

0评论2023-02-10893

Oracle SQL七次提速技巧
以下SQL执行时间按序号递减。1,动态SQL,没有绑定变量,每次执行都做硬解析操作,占用较大的共享池空间,若共享池空间不足,会导致其他SQL语句的解析信息被挤出共享池。create or replace procedure proc1as beginfor i in 1..100000 loop    execute imme

0评论2023-02-10755

SQL ORACLE case when函数用法
case when 用法(1)简单case函数:格式:  case 列名   when 条件值1 then 选项1  when 条件值1 then 选项2......  else 默认值 end例如:  select   case job_level  when '1' then '1111'  when '2' then '2222'   when '3' then '3333

0评论2023-02-10564

mysql下如何执行sql脚本 执行SQL脚本
1.编写sql脚本,假设内容如下:  create database dearabao;  use dearabao;  create table niuzi (name varchar(20));  保存脚本文件,假设我把它保存在F盘的hello world目录下,于是该文件的路径为:F:\hello world\niuzi.sql2.执行sql脚本,可以有2种方法: 

0评论2023-02-10699

MySQL 5.7版本sql_mode=only_full_group_by问题
用到GROUP BY 语句查询时com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'col_user_6.a.START_TIME' which is not functionally dependent on colu

0评论2023-02-10973

Oracle迁移到MySQL性能下降的注意点 oracle数据库迁移需要注意的问题
背景:最近有较多的客户系统由原来由Oracle改造到MySQL后出现了性能问题CPU 100%,或是后台的CRM系统复杂SQL在业务高峰的时候出现堆积导致业务故障。在我的记忆里面淘宝最初从Oracle迁移到MySQL期间也遇到了很多SQL的性能问题,记忆最为深刻的子查询,当初的

0评论2023-02-10580

ORACLE中通过SQL语句(alter table)来增加、删除、修改字段
1.添加字段:alter table  表名  add (字段  字段类型)  [ default  '输入默认值']  [null/not null]  ;2.添加备注:comment on column  库名.表名.字段名 is  '输入的备注';  如: 我要在ers_data库中  test表 document_type字段添加备注  comm

0评论2023-02-10584

MySQL与Oracle 差异比较之六触发器
触发器编号类别ORACLEMYSQL注释1创建触发器语句不同create or replace trigger TG_ES_FAC_UNIT  before insert or update or delete on ES_FAC_UNIT  for each rowcreate trigger `hs_esbs`.`TG_INSERT_ES_FAC_UNIT` BEFORE INSERT on `hs_esbs`.`es_fac_u

0评论2023-02-10914

mysql where条件:某时间字段为今天的sql语句
1.查询:注册时间为今天的所有用户数:select count(*) from customer where TO_DAYS(createtime) = TO_DAYS(NOW())2.获取当前时间到凌晨24点还有多长时间:(Java中可用于判断某时间是否为今天)final Calendar cal = Calendar.getInstance();    ca

0评论2023-02-10717

mysql中的sql
变量用户变量: 在用户变量前加@系统变量: 在系统变量前加@@运算符算术运算符有: +(加), -(减), * (乘), / (除) 和% (求模) 五中运算位运算符有:(位于), | (位或), ^ (位异或), ~ (位取反),(位右移),(位左移)比较运算符有: = (等于),(大于),(小于), = (大

0评论2023-02-10936

Oracle的HINT可以强制指定SQL的执行计划,比如选择索引、表的连接顺序以及表的连接方式等等。(转)
在Oracle中查看所有的表: select * from tab/dba_tables/dba_objects/cat; 看用户建立的表 :  select table_name from user_tables;  //当前用户的表 select table_name from all_tables;  //所有用户的表 select table_name from dba_tables;  //包

0评论2023-02-10857

Oracle sql 子字符串长度判断
Oracle sql 子字符串长度判断 select t.* from d_table t WHEREsubstr(t.col,1,1)='8' and instr(t.col,'/')0 and length(substr(t.col,1,instr(t.col,'/')))5; 字符串的前两位都是数字:select * from d_table t WHERE regexp_like(substr(t.col,1,2), '^[

0评论2023-02-10759

Oracle、MySql、Sql Server比对
MySql:廉价(部分免费):当前,MySQL採用双重授权(DualLicensed),他们是GPL和MySQLAB制定的商业许可协议。假设你在一个遵循GPL的***(开源)项目中使用MySQL,那么你能够遵循GPL协议免费使用MySQL。否则,你须要购买MySQLAB制定的那个商业许可协议。Windows $

0评论2023-02-10441

Oracle 存储过程,临时表,动态SQL测试
--创建事务级别的结果临时表create global temporary table tmp_yshy( c1 varchar2(100), c2 varchar2(100))on commit delete rows;--创建事务级别的存储sql语句的临时表create global temporary table tmp_sql( c1 varchar2(4000))on commit delete rows;测

0评论2023-02-10508

更多推荐