SqlServer与MySql语法比较

时间:2022-01-14 14:56:18

1、复制表(包括表结构、表数据)

SqlServer:

  Select * into user_copy from user

MySql:

  CREATE TABLE user_copy LIKE user;

  INSERT INTO user_copy SELECT * FROM user;

 

2、多表连接做update

SqlServer:

  UPDATE s1 SET s1.salary= s2.salary
  FROM (SELECT * FROM s WHERE age>10) AS s1 JOIN s2 ON s1.uname= s2.uname
  WHERE s1.uname = 'xx'

MySql:(无法直接对目标表的查询数据直接做更新,只能针对目标表操作)

  UPDATE s1 JOIN s2 ON s1.uname= s2.uname
  SET s1.salary= s2.salary
  WHERE s1.uname = 'xx'

 

3、修改表字段结构

SqlServer:  

  ALTER TABLE user ALTER COLUMN uid INT NOT NULL
  GO
  ALTER TABLE user ALTER COLUMN name nvarchar(100) NOT NULL
  GO

MySql:(无法直接对目标表的查询数据直接做更新,只能针对目标表操作)

  ALTER TABLE user MODIFY uid INT NOT NULL COMMENT 'xx';
  ALTER TABLE user MODIFY name varchar(100) NOT NULL COMMENT 'xxy';

 

4、添加、修改主键

SqlServer:

  ALTER TABLE user ADD CONSTRAINT PK_uid_name PRIMARY KEY(uid, name)

MySql:(无法直接对目标表的查询数据直接做更新,只能针对目标表操作)

  ALTER TABLE user DROP PRIMARY KEY;
  ALTER TABLE user ADD PRIMARY KEY (uid, name);

 

5、查询数据拼接Sql语句

SqlServer:

  SELECT 'insert into dbo.TableName([ID],[Name]) values ('+ CAST([ID] AS VARCHAR) + ',''' + CAST([Name] AS VARCHAR)  + ''')'
  FROM dbo.TableName WHERE ID > 433

 

6、查询字段首位为汉字

SqlServer:

SELECT * FROM 表名 WHERE ascii(字段)>127

7、查询字段包含汉字(任意位)

SqlServer:

SELECT * FROM 表名 WHERE 字段 LIKE '%[吖-座]%'
--[吖-座]是中文字符集第一个到最后一个的范围