首页 > 数据库 >SqlServer 与 MySQL 基本操作语句对比

SqlServer 与 MySQL 基本操作语句对比

时间:2023-01-27 15:06:36浏览次数:75  
标签:username name -- sp SqlServer tabName MySQL 基本操作 TABLE


继上篇 ​​MySQL 基本操作语句​​ 后,个人测试和补充了 SqlServer 与 MySQL 的 基本操作语句对比,主要是对比SQL命令的一些差异,不作更多说明。

(由于mysql 没有系统学习过,欢迎大侠们拍砖~~O(∩_∩)O~)


 SqlServer版本:SqlServer 2008

MySQL版本: MySQL 5.5


执行符号:

MySQL :分号';'

SqlServer :字符'GO'

 

 

自增列:MySQL必须要索引,不能设置增量;

默认值:MySQL默认值不用加括号;SqlServer可加或不加;

--MySQL

CREATE TABLE tabName(

ID INTPRIMARYKEY AUTO_INCREMENT,

Value INTDEFAULT

) AUTO_INCREMENT= 100ENGINE=MyISAM DEFAULTCHARSET=utf8;

 

--SqlServer

CREATE TABLE tabName(

ID INTIDENTITY(100,1)PRIMARYKEY,

Value INTDEFAULT(12)

)

 

查看表结构定义:

--SqlServer 

EXEC sp_help'tabName'

--MySQL

DESC tabName

 

更改表名:

--SqlServer 

EXEC sys.sp_rename@objname=N'tabName',@newname= 'newTabName'

--MySQL

ALTER TABLE tabName RENAME newTabName

 

更改字段类型:

--SqlServer

ALTER TABLE[dbo].[tabName]ALTERCOLUMN [ID] BIGINT

 

--MySQL

ALTER TABLE tabName MODIFY IDBIGINT

 

更改字段名:

--SqlServer

EXEC sys.sp_rename@objname=N'tabName.ID',@newname='IewID',@objtype='column'

 

--MySQL

ALTER TABLE tabName CHANGE IDIewIDBIGINT

 

添加字段:(一样)

--SqlServer

ALTER TABLE[dbo].[tabName]ADDNAME NVARCHAR(200)NULL

 

--MySQL

ALTER TABLE tabName ADD NAMENVARCHAR(200)NULL

 

删除字段:

--SqlServer

ALTER TABLE[dbo].[tabName]DROPCOLUMN NAME

 

--MySQL

ALTER TABLE tabName DROP NAME

 

添加主键/外键或约束:

--SqlServer

ALTER TABLE[dbo].[tabName]ADDCONSTRAINT pk_tabNamePRIMARYKEY (id);

 

--MySQL

ALTER TABLEtabNameADD CONSTRAINT pk_tabNamePRIMARYKEYtabName(id);

ALTER TABLEsubTabNameADD CONSTRAINT fk_subTabName_tabNameFOREIGNKEYsubTabName(fid)REFERENCEStabName(id);

 

删除主键/外键或约束:

--SqlServer(统一语法)

ALTER TABLE[dbo].[tabName]DROPCONSTRAINT pk_tabName

 

--MySQL

ALTER TABLEtabNameDROP CONSTRAINT constaintName

ALTER TABLEtabNameDROP PRIMARY KEY pk_tabName

ALTER TABLE subTabName DROP FOREIGNKEYfk_subTabName_tabName

 

删除表:

--SqlServer

DROP TABLEtabName,TabName2;

 

--MySQL

DROP TABLE IF EXISTS tabName,TabName2;

 

#######################################################################################

 

创建视图:

--MySQL

CREATE ORREPLACEVIEW VtabName

AS

SELECT *FROMtabName

WITH CHECK OPTION;

 

--SqlServer

CREATE VIEWVtabName

AS

SELECT *FROMtabName

WITH CHECK OPTION;

 

视图结构和定义:

--MySQL

DESC VtabName;

SHOW CREATEVIEWVtabName;

SHOW TABLE STATUS LIKE 'VtabName';

 

--SqlServer

sp_help VtabName

sp_helptext VtabName

 

删除视图:

--MySQL

DROP VIEWtabName;

DROP VIEW IF EXISTS tabName;

 

--SqlServer

DROP VIEWtabName;

 

#######################################################################################

 

创建索引:

--MySQL

CREATE TABLEtabName(id INT NOTNULL,KEYindName(id));

ALTER TABLEtabNameADD INDEX indName(Value);

CREATE UNIQUE INDEX indName ONtabName(Value);

 

--SqlServer

CREATE TABLE tabName(IDINTIDENTITY(100,1)CONSTRAINTPK_tabNamePRIMARYKEY)

CREATE UNIQUE INDEX indName ONtabName(Value);

 

重建索引:

--MySQL

REPAIR TABLE tabName QUICK;

 

--SqlServer

DBCC DBREINDEX('dbo.tabName','',100)

ALTER INDEXindNameON tabName REBUILD;

ALTER INDEX ALL ON tabNameREORGANIZE;

 

删除索引:

--MySQL

ALTER TABLEtabNameDROP INDEX indName;

ALTER TABLEtabNameDROP PRIMARY KEY pk_tabName;

ALTER TABLEsubTabNameDROP FOREIGN KEY fk_subTabName_tabName;

DROP INDEX indName ON tabName;

 

--SqlServer

DROP INDEX indName ON tabName;

ALTER TABLE tabName DROP CONSTRAINTPK_tabName

 

查看索引:

--MySQL

SHOW INDEX FROM tabName;

 

--SqlServer

SELECT *FROMsys.indexesWHEREobject_id= OBJECT_ID('tabName')

SELECT * FROM sys.sysindexesWHEREid = OBJECT_ID('tabName')

 

#######################################################################################

查看存储过程/函数定义:

--MySQL

SHOW CREATE{PROCEDURE | FUNCTION }name;

SHOW {PROCEDURE| FUNCTION } STATUS[ LIKE 'pattern'];

SELECT * FROM information_schema.RoutinesWHEREROUTINE_NAME='name';

 

--SqlServer

exec sp_helpf_getdate

exec sp_helptextf_getdate

SELECT * FROM information_schema.RoutinesWHEREROUTINE_NAME='name';

 

存储过程:

--MySQL

DELIMITER //

CREATE PROCEDUREsp_name(INparam1 INT,OUTparam2INT)

BEGIN

{sql_statement}

END//

DELIMITER ;

 

CALL sp_name();

DROP PROCEDUREIFEXISTS sp_name;

 

--SqlServer

CREATE PROCEDUREsp_name(@param1INT,@param2INTOUTPUT)

AS

BEGIN

{sql_statement}

END

GO

 

EXEC sp_name();

DROP PROCEDUREsp_name;

 

函数:

--MySQL

DELIMITER //

CREATE FUNCTIONfn_name()

RETURNS Decimal(10,2)

RETURN 3.14;

//

DELIMITER ;

 

SELECT fn_name();

DROP PROCEDUREIFEXISTS fn_name;

 

--SqlServer

CREATE FUNCTIONdbo.fn_name()

RETURNS Decimal(10,2)

AS

BEGIN

RETURN

END

GO

 

SELECT dbo.fn_name();

DROP FUNCTIONdbo.fn_name;

 

触发器:

--MySQL

DELIMITER //

CREATE TRIGGERtr_name

{ AFTER|BEFORE } { INSERT| UPDATE | DELETE}

ON tabName

FOR EACHROW

BEGIN

{sql_statement;}

END

DELIMITER ;

 

DROP TRIGGER IF EXISTS tr_name;

 

--SqlServer

CREATE TRIGGERdbo.tr_name

ON [dbo].[tabName]

{ FOR|AFTER | INSTEAD OF}{ INSERT | UPDATE| DELETE }

AS

BEGIN

{sql_statement;}

END

GO

 

DROP TRIGGER dbo.tr_name

 

#######################################################################################

 

循环语句:

--MySQL(1至100之和)

WHILE 循环:

DELIMITER //

CREATE PROCEDURETESTPRO()

BEGIN

DECLARE I,K INTDEFAULT 0 ;

WHILE I<= 100DO

SET K=I + K;

SET I=I + 1;

END WHILE;

SELECT K;

END;//

DELIMITER ;

 

REPEAT UNTIL 循环:

DELIMITER //

CREATE PROCEDURETESTPRO()

BEGIN

DECLARE I,K INTDEFAULT 0 ;

REPEAT

SET K=I + K;

SET I=I + 1;

UNTIL I>100

END REPEAT;

SELECT K;

END;//

DELIMITER ;

 

LOOP 循环:

DELIMITER //

CREATE PROCEDURETESTPRO()

BEGIN

DECLARE I,K INTDEFAULT 0 ;

LABEL: LOOP

SET K=I + K;

SET I=I + 1;

IF I>100THEN

LEAVE LABEL;

END IF;

END LOOP;

SELECT K;

END;//

DELIMITER ;

 

 

CALL TESTPRO();

DROP PROCEDUREIFEXISTS TESTPRO;

 

--SqlServer

DECLARE @I INT =

DECLARE @K INT =

WHILE @I<=

BEGIN

SET @K = @K + @I

SET @I = @I +

END

SELECT @K

 

游标:

--MySQL(参数名称不能与列明一样)

DELIMITER //

CREATE PROCEDURETESTPRO()

BEGIN

DECLARE FName varchar(50);

DECLARE LName varchar(50);

DECLARE IsDone BOOLEAN DEFAULTFALSE;

DECLARE cursor_name CURSOR FOR SELECT FirstName,LastNameFROMPerson;

DECLARE CONTINUE HANDLER FOR NOT FOUNDSETIsDone = TRUE;

OPEN cursor_name;

LABEL: LOOP

FETCH cursor_name INTO FName,LName;

IF IsDoneTHEN

LEAVE LABEL;

END IF;

SELECT CONCAT('Contact Name:',FName,LName)ASName;

END LOOP;

CLOSE cursor_name;

END;//

DELIMITER ;

 

--SqlServer

DECLARE @LastName varchar(50),@FirstNamevarchar(50);

DECLARE contact_cursor CURSOR FOR SELECT LastName,FirstNameFROM Person

OPEN contact_cursor;

FETCH NEXTFROMcontact_cursorINTO@LastName,@FirstName;

WHILE @@FETCH_STATUS =

BEGIN

SELECT 'Contact Name: ' + @FirstName + ' '+@LastName

FETCH NEXT FROM contact_cursorINTO@LastName,@FirstName;

END

CLOSE contact_cursor;

DEALLOCATE contact_cursor;

GO

 

#######################################################################################

查看账户信息:

--MySQL

select Host,User,Passwordfrommysql.user;

show grantsfor'username'@'localhost';

 

--SqlServer

select *fromsys.syslogins

select * from sys.sysuserswhereissqluser =

 

添加账户:

--MySQL(insertinto mysql.user 默认禁止,去掉my.init\sql-mode\STRICT_TRANS_TABLES)

CREATE USER 'username'@'localhost'IDENTIFIEDBY'password';

INSERT INTOmysql.user(Host,User,Password)

VALUES ('localhost','username',PASSWORD('password'));

 

--SqlServer

USE [master]

GO

CREATE LOGIN[username]WITH PASSWORD=N'password',CHECK_POLICY=OFF

GO

USE [YourDatabase]

GO

CREATE USER [username] FORLOGIN[username]

GO

 

更改密码:

--MySQL

mysqladmin -uusername -p123456 password

set password=password("kk");

set passwordfor'username'@'localhost'=password('123456');

update mysql.usersetPassword = PASSWORD('123456')WHEREuser='username';

flush privileges;

 

--SqlServer

ALTER LOGIN[username]WITH PASSWORD=N'123456'

sp_password @new = '123456',@loginame='username'--,@old = 'old_password'

 

授权和回收权限:​​(SqlServer服务器角色和数据库角色相关操作)​​

--MySQL

GRANT SELECTon*.* to 'username'@'localhost'identifiedby 'password';

REVOKE allprivileges,grant option FROM'username'@'localhost';

 

--SqlServer

USE [master]

GRANT CONNECTSQLTO [username]

REVOKE CONNECTSQLTO [username]

EXEC master..sp_addsrvrolemember@loginame=N'username',@rolename= N'sysadmin'

EXEC master..sp_dropsrvrolemember@loginame=N'username' ,@rolename=N'sysadmin'

GO

USE [YourDatabase]

GRANT INSERT,UPDATE,DELETE,SELECTON[dbo].[TestTab]TO[username]

REVOKE INSERT,UPDATE,DELETE,SELECTON[dbo].[TestTab]TO[username]

EXEC sp_addrolememberN'db_owner',N'username'

EXEC sp_droprolememberN'db_owner',N'username'

GO

 

删除账户:

--MySQL

DROP user 'username'@'localhost';

DELETE FROMmysql.userWHEREuser='username';

 

--SqlServer

USE [master]

GO

DROP LOGIN[TUser]

sp_droplogin @loginame = 'username'

GO

USE [YourDatabase]

GO

DROP USER [TUser]

sp_dropuser @name_in_db = 'username'

GO

 








标签:username,name,--,sp,SqlServer,tabName,MySQL,基本操作,TABLE
From: https://blog.51cto.com/hzc2012/6024081

相关文章

  • SQLServer 2012 包含数据库(Contained Databases)
    “包含数据库”是独立于其他数据库以及承载数据库的SQLServer实例的一种数据库。SQLServer2012以4种方法帮助用户使其数据库独立于实例。▶很多用于描述数据库的......
  • MySQL 自动备份还原工具 MySQL-AutoXtraBackup
    MySQL-AutoXtraBackup是Percona实验室中的开源工具,是基于 xtrabackup和autoxtrabackup备份的备份工具。如果不是很熟悉 AutoXtraBackup,个人觉得还不如使用 autoxtr......
  • Windows 2008 双机群集配置(for SQLServer)
    此处配置Windows2008群集,将用于SQLserver2008双机故障转移群集,此处SQLserver的群集基于Windows群集实现。Windows2008集群更容易实现了,只要规划好IP和磁盘分配,可一直......
  • MySQL EXPLAIN 实践汇总
    MySQLEXPLAIN可以显示估计查询语句执行计划,从中可以分析查询的执行情况是不是最优,这有助于我们对不使用索引的查询进行优化。EXPLAIN对于每个查询语句返回一行信息,它列出了......
  • SQLServer 2014 内存优化表
    内存优化表是SQLServer2014的新功能,它是可以将表放在内存中,这会明显提升DML性能。关于内存优化表,更多可参考两位大侠的文章:​​SQLServer2014新特性探秘(1)-内存数据库......
  • MySQL基础篇(运算符、排序分页、多表查询、函数)
    MySQL基础篇​​数据库概述​​​​数据库与数据库管理系统​​​​数据库与数据库管理系统的关系​​​​Mysql介绍​​​​RDBMS与非RDBMS​​​​关系型数据库(RDBMS)......
  • 统一观测丨如何使用 Prometheus 监控 MySQL
     MySQL作为最流行的关系型数据库管理系统之一,非常多系统的后端存储都有着MySQL的身影,可谓是广泛应用于各行各业。与此同时,数据库作为应用服务的核心组件,直接影响着应......
  • 统一观测丨如何使用 Prometheus 监控 MySQL
     MySQL作为最流行的关系型数据库管理系统之一,非常多系统的后端存储都有着MySQL的身影,可谓是广泛应用于各行各业。与此同时,数据库作为应用服务的核心组件,直接影响着应......
  • 统一观测丨如何使用 Prometheus 监控 MySQL
     MySQL作为最流行的关系型数据库管理系统之一,非常多系统的后端存储都有着MySQL的身影,可谓是广泛应用于各行各业。与此同时,数据库作为应用服务的核心组件,直接影响着应......
  • 统一观测丨如何使用 Prometheus 监控 MySQL
     MySQL作为最流行的关系型数据库管理系统之一,非常多系统的后端存储都有着MySQL的身影,可谓是广泛应用于各行各业。与此同时,数据库作为应用服务的核心组件,直接影响着应......