SqlServer 与 MySQL 基本操作语句对比

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

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

 SqlServer版本:SqlServer 2008

MySQL版本: MySQL 5.5

执行符号:

MySQL :分号‘;‘

SqlServer :字符‘GO‘

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

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

--MySQL

CREATE TABLE
tabName(

ID INT
PRIMARY KEY
AUTO_INCREMENT,

Value INT
DEFAULT 12

) AUTO_INCREMENT
= 100 ENGINE=MyISAM
DEFAULT CHARSET=utf8;

--SqlServer

CREATE TABLE
tabName(

ID INT
IDENTITY(100,1)
PRIMARY KEY,

Value INT
DEFAULT(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]
ALTER COLUMN
[ID] BIGINT

--MySQL

ALTER TABLE
tabName MODIFY ID
BIGINT

更改字段名:

--SqlServer

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

--MySQL

ALTER TABLE
tabName CHANGE ID
IewID BIGINT

添加字段:(一样)

--SqlServer

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

--MySQL

ALTER TABLE
tabName ADD NAME
NVARCHAR(200)
NULL

删除字段:

--SqlServer

ALTER TABLE
[dbo].[tabName]
DROP COLUMN
NAME

--MySQL

ALTER TABLE
tabName DROP NAME

添加主键/外键或约束:

--SqlServer

ALTER TABLE
[dbo].[tabName]
ADD CONSTRAINT
pk_tabNamePRIMARY
KEY (id);

--MySQL

ALTER TABLE
tabName ADD
CONSTRAINT pk_tabNamePRIMARY
KEY tabName(id);

ALTER TABLE
subTabName ADD
CONSTRAINT fk_subTabName_tabNameFOREIGN
KEY subTabName(fid)
REFERENCES tabName(id);

删除主键/外键或约束:

--SqlServer(统一语法)

ALTER TABLE
[dbo].[tabName]
DROP CONSTRAINT
pk_tabName

--MySQL

ALTER TABLE
tabName DROP
CONSTRAINT constaintName

ALTER TABLE
tabName DROP
PRIMARY KEY pk_tabName

ALTER TABLE
subTabName DROP FOREIGN
KEY fk_subTabName_tabName

删除表:

--SqlServer

DROP TABLE
tabName,TabName2;

--MySQL

DROP TABLE
IF EXISTS tabName,TabName2;

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

创建视图:

--MySQL

CREATE OR
REPLACE VIEW
VtabName

AS

SELECT *
FROM tabName

WITH CHECK
OPTION;

--SqlServer

CREATE VIEW
VtabName

AS

SELECT *
FROM tabName

WITH CHECK
OPTION;

视图结构和定义:

--MySQL

DESC VtabName;

SHOW CREATE
VIEW VtabName;

SHOW TABLE
STATUS LIKE ‘VtabName‘;

--SqlServer

sp_help
VtabName

sp_helptext VtabName

删除视图:

--MySQL

DROP VIEW
tabName;

DROP VIEW
IF EXISTS tabName;

--SqlServer

DROP VIEW
tabName;

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

创建索引:

--MySQL

CREATE TABLE
tabName(
id INT NOT
NULL,KEY
indName(id))
;

ALTER TABLE
tabName ADD
INDEX indName(Value);

CREATE UNIQUE
INDEX indName ON
tabName(Value);

--SqlServer

CREATE TABLE
tabName(ID
INT IDENTITY(100,1)
CONSTRAINT PK_tabNamePRIMARY
KEY)

CREATE UNIQUE
INDEX indName ON
tabName(Value);

重建索引:

--MySQL

REPAIR TABLE
tabName QUICK;

--SqlServer

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

ALTER INDEX
indName ON
tabName REBUILD;

ALTER INDEX
ALL ON tabName
REORGANIZE;

删除索引:

--MySQL

ALTER TABLE
tabName DROP
INDEX indName;

ALTER TABLE
tabName DROP
PRIMARY KEY pk_tabName;

ALTER TABLE
subTabName DROP
FOREIGN KEY fk_subTabName_tabName;

DROP INDEX
indName ON tabName;

--SqlServer

DROP INDEX
indName ON tabName;

ALTER TABLE
tabName DROP CONSTRAINT
PK_tabName

查看索引:

--MySQL

SHOW INDEX
FROM tabName;

--SqlServer

SELECT *
FROM sys.indexes
WHERE object_id
= OBJECT_ID(‘tabName‘)

SELECT *
FROM sys.sysindexes
WHERE id
= OBJECT_ID(‘tabName‘)

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

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

--MySQL

SHOW CREATE
{ PROCEDURE
| FUNCTION }
name ;

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

SELECT *
FROM information_schema.Routines
WHERE ROUTINE_NAME=‘name‘;

--SqlServer

exec sp_helpf_getdate

exec sp_helptextf_getdate

SELECT *
FROM information_schema.Routines
WHERE ROUTINE_NAME=‘name‘;

存储过程:

--MySQL

DELIMITER
//

CREATE PROCEDURE
sp_name(IN
param1 INT,OUT
param2 INT)

BEGIN

{sql_statement}

END//

DELIMITER
;

CALL sp_name();

DROP PROCEDURE
IF EXISTS
sp_name;

--SqlServer

CREATE PROCEDURE
sp_name(@param1
INT,@param2
INT OUTPUT)

AS

BEGIN

{sql_statement}

END

GO

EXEC sp_name();

DROP PROCEDURE
sp_name;

函数:

--MySQL

DELIMITER
//

CREATE FUNCTION
fn_name()

RETURNS
Decimal(10,2)

RETURN 3.14;

//

DELIMITER
;

SELECT fn_name();

DROP PROCEDURE
IF EXISTS
fn_name;

--SqlServer

CREATE FUNCTION
dbo.fn_name()

RETURNS
Decimal(10,2)

AS

BEGIN

RETURN 3.14

END

GO

SELECT dbo.fn_name();

DROP FUNCTION
dbo.fn_name;

触发器:

--MySQL

DELIMITER
//

CREATE TRIGGER
tr_name

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

ON tabName

FOR EACH
ROW

BEGIN

{sql_statement;}

END

DELIMITER
;

DROP TRIGGER
IF EXISTS tr_name;

--SqlServer

CREATE TRIGGER
dbo.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 PROCEDURE
TESTPRO()

BEGIN

DECLARE
I,K
INTDEFAULT 0 ;

WHILE I
<= 100 DO

SET K
= I
+ K;

SET I
= I
+ 1;

END WHILE;

SELECT K;

END;//

DELIMITER
;

REPEAT UNTIL 循环:

DELIMITER
//

CREATE PROCEDURE
TESTPRO()

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 PROCEDURE
TESTPRO()

BEGIN

DECLARE
I,K
INTDEFAULT 0 ;

LABEL: LOOP

SET K
= I
+ K;

SET I
= I
+ 1;

IF I>100
THEN

LEAVE LABEL;

END IF;

END LOOP;

SELECT K;

END;//

DELIMITER
;

CALL TESTPRO();

DROP PROCEDURE
IF EXISTS
TESTPRO;

--SqlServer

DECLARE
@I INT = 1

DECLARE
@K INT = 0

WHILE @I
<= 100

BEGIN

SET
@K = @K
+ @I

SET
@I = @I
+ 1

END

SELECT @K

游标:

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

DELIMITER
//

CREATE PROCEDURE
TESTPRO()

BEGIN

DECLARE
FName varchar(50);

DECLARE
LName varchar(50);

DECLARE
IsDone BOOLEAN DEFAULT
FALSE;

DECLARE
cursor_name CURSOR
FOR SELECT FirstName,LastName
FROM Person;

DECLARE
CONTINUE HANDLER
FOR NOT FOUND
SET IsDone
= TRUE;

OPEN cursor_name;

LABEL: LOOP

FETCH
cursor_name INTO
FName,LName;

IF IsDone
THEN

LEAVE
LABEL;

END
IF;

SELECT
CONCAT(‘Contact Name:‘,FName,LName)
AS Name;

END LOOP;

CLOSE cursor_name;

END;//

DELIMITER ;

--SqlServer

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

DECLARE
contact_cursor CURSOR
FOR SELECT LastName,
FirstName FROM Person

OPEN contact_cursor;

FETCH NEXT
FROM contact_cursor
INTO @LastName,
@FirstName;

WHILE
@@FETCH_STATUS = 0

BEGIN

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

FETCH
NEXT FROM contact_cursor
INTO @LastName,
@FirstName;

END

CLOSE contact_cursor;

DEALLOCATE
contact_cursor;

GO

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

查看账户信息:

--MySQL

select Host,User,Password
from mysql.user;

show grants
for ‘username‘@‘localhost‘;

--SqlServer

select *
from sys.syslogins

select *
from sys.sysusers
where issqluser
= 1

添加账户:

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

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

INSERT INTO
mysql.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] FOR
LOGIN [username]

GO

更改密码:

--MySQL

mysqladmin
-uusername -p123456 
password 654321

set password=password("kk");

set password
for ‘username‘@‘localhost‘
= password(‘123456‘);

update mysql.user
set Password
= PASSWORD(‘123456‘)
WHERE user=‘username‘;

flush privileges;

--SqlServer

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

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

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

--MySQL

GRANT SELECT
on *.*
to ‘username‘@‘localhost‘
identified by
‘password‘;

REVOKE all
privileges,
grant option FROM
‘username‘@‘localhost‘;

--SqlServer

USE [master]

GRANT CONNECT
SQL TO
[username]

REVOKE CONNECT
SQL TO
[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,SELECT
ON [dbo].[TestTab]
TO [username]

REVOKE INSERT,UPDATE,DELETE,SELECT
ON [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 FROM
mysql.user
WHERE user=‘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

时间: 2024-10-25 05:38:43

SqlServer 与 MySQL 基本操作语句对比的相关文章

MYSQL基本操作语句

0.修改密码:mysqladmin -u root -p password 123456 导出数据库:mysqldump -u root -p yunpay>yunpay.sql 导入数据库:mysql –u root -p yunpay < yunpay.sql 1.为mysql增加一个名为admin,主机名任意的网络用户,其通过密码'123'访问数据库,这个用户拥有对数据库的所有操作权限(ALL PRIVILEGES) CREATE USER 'admin'@'%' IDENTIFIED

MySql 基本操作语句整理

数据库 DATABASE: 创建 CREATTE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name . 删除: DROP {DATABASE | SCHEMA} [IF EXISTS] db_name; 显示: SHOW{ DATABASES| SCHEMAS} [LIKE 'pattern'|WHERE expr]; 选择: USE database_name : 数据表 T

Mysql的基本操作语句(适合初学者借鉴)

因本人之前对mysql数据库不是特别了解,所以那段时间为了应对工作做出了各种努力,现将本人总结整理的mysql数据库基本操作摘录出来分享给大家,大多已加批注,方便记忆学习!  Mysql的基本操作语句 1.新建用户 1.1 登录MYSQL: @>mysql -u root -p @>密码 无密码设置如果已设mysql系统环境变量可直接mysql进入,如没有设置mysql系统环境变量请使用绝对路径 1.2 创建用户: mysql> insert into mysql.user(Host,U

MongoDB与MySQL的操作对比表及区别介绍

MongoDB与MySQL的操作对比表及区别介绍 MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库.它们各有各的优点,关键是看用在什么地方.所以我们所熟知的那些SQL(全称Structured Query Language)语句就不适用于MongoDB了,因为SQL语句是关系型数据库的标准语言. 以我们公司项目为例,在早期的项目中,都在使用关系型数据库,用过SQLServer,Orac

你搞懂 ORACLE、 SQLSERVER、MYSQL与DB2的区别了吗

ORACLE. SQLSERVER.MYSQL与DB2的区别--平台性:    Oracle.MYSQL与DB2可在所有主流平台上运行:    SQL Server只能在Windows下运行: --安全性:    Oracle的安全认证获得最高认证级别的ISO标准认证,而SQL Server并没有获得什么安全认证:    这方面证明了Oracle的安全性是高于SQL Server的: --数据类型.函数.sql语句:    oracle中有复合数据类型,sql server中没有: 总之:   

Linux下的Mysql基本语句

Linux下的Mysql基本语句 实验目标 数据库常见概念 SQL语句概述-创建数据库 SQL语句之表的基本操作 实验环境 LAMB环境 服务端:xuegod63.cn     IP:192.168.1.63 客户端:xuegod64.cn                   IP:192.168.1.64   实验概述 数据库相关概念: DBD(数据库开发)  DBA(数据库管理员) SQL :结构化查询语言(Structured QueryLanguage)简称SQL 作用:结构化查询语言是

mysql sql语句大全

1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始 备份 BACKUP DATABASE pubs TO testBack 4.说明:创建新表 cr

SQLserver 转换Mysql 一些语法使用的区别

1.存储过程的语法各式eg:DELIMITER $$ CREATE PROCEDURE test_p_add9( p_name VARCHAR(100), p_test VARCHAR(100), p_age INT, OUT o_newid INT) BEGIN INSERT INTO table_test (f_name,f_test,f_age,f_date) VALUES (p_name,p_test,p_age,NOW()); SET o_newid=LAST_INSERT_ID();

最全mysql sql语句大全

mysql sql语句大全 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始 备份 BACKUP DATABASE pubs TO testBac