将 MySQL DDL 转换为 SQL Server DDL

发布于 2024-09-10 21:37:31 字数 1795 浏览 7 评论 0原文

我有一个为 MySQL 5 生成的模型,但现在我需要在 SQL Server 安装上创建这些表。

自从我使用 SQL Server 以来已经有多年了,我想确保我可以将此脚本转换为兼容的。

我真的不知道要寻找什么 TBQH,所以言归正传,这是我的 MySQL DDL

CREATE SCHEMA IF NOT EXISTS `bof_survey` DEFAULT CHARACTER SET utf8 COLLATE default collation ;
USE `bof_survey`;

-- -----------------------------------------------------
-- Table `bof_survey`.`question`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `bof_survey`.`question` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `text` VARCHAR(255) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bof_survey`.`category`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `bof_survey`.`category` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NOT NULL ,
  `adverb` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bof_survey`.`answer`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `bof_survey`.`answer` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `question_id` INT UNSIGNED NULL ,
  `category_id` INT UNSIGNED NULL ,
  `text` VARCHAR(60) NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_answer_question` (`question_id` ASC) ,
  INDEX `fk_answer_category1` (`category_id` ASC) ,
  CONSTRAINT `fk_answer_question`
    FOREIGN KEY (`question_id` )
    REFERENCES `bof_survey`.`question` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_answer_category1`
    FOREIGN KEY (`category_id` )
    REFERENCES `bof_survey`.`category` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

I have a model that was generated for MySQL 5 but now I need to create these tables on a SQL Server installation.

It's been years since I mucked with SQL server and I want to make sure I can convert this script to be compatible.

I don't really know what to look for TBQH, so without further ado, here's my MySQL DDL

CREATE SCHEMA IF NOT EXISTS `bof_survey` DEFAULT CHARACTER SET utf8 COLLATE default collation ;
USE `bof_survey`;

-- -----------------------------------------------------
-- Table `bof_survey`.`question`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `bof_survey`.`question` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `text` VARCHAR(255) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bof_survey`.`category`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `bof_survey`.`category` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NOT NULL ,
  `adverb` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `bof_survey`.`answer`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `bof_survey`.`answer` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `question_id` INT UNSIGNED NULL ,
  `category_id` INT UNSIGNED NULL ,
  `text` VARCHAR(60) NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_answer_question` (`question_id` ASC) ,
  INDEX `fk_answer_category1` (`category_id` ASC) ,
  CONSTRAINT `fk_answer_question`
    FOREIGN KEY (`question_id` )
    REFERENCES `bof_survey`.`question` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_answer_category1`
    FOREIGN KEY (`category_id` )
    REFERENCES `bof_survey`.`category` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

分开我的手 2024-09-17 21:37:31

一种开始方法是将 DDL 加载到 MySQL 数据库中,然后使用 mysqldump --兼容=mssql 重新转储它。这应该可以帮助您开始——从那里开始,您可能会浏览 T-SQL 文档,并根据具体情况在此处询问。

另外,微软还有一些资源,比如这篇文章(针对SQL Server 2000,但它可以帮助正确映射数据类型)。

One way of getting started is to load your DDL into a MySQL database and then use mysqldump --compatible=mssql to re-dump it. That should get you started -- and from there on it may be going through the T-SQL docs and asking here on a case-by-case basis.

In addition, Microsoft has some resources, such as this article (for SQL Server 2000, but it could help mapping the data types correctly).

婴鹅 2024-09-17 21:37:31

Peter,

您需要考虑 MySQL 和 MSSQL 之间的一些差异,尤其是模式。我不太清楚它们如何与 MySQL 一起工作,但它似乎几乎就像 MSSQL 本身所说的数据库一样。

MSSQL 中的模式更多的是一个安全抽象层,用于对数据库内的对象进行分组。 AFAIK 并没有大量使用它,但 MS 希望推广它。我在这里省略了它,然后在默认模式(通常是 dbo)中创建对象。

不用说,其余部分非常简单:

-- ----------------------------------------------------- 
-- Table question
-- ----------------------------------------------------- 
IF NOT EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   name = 'question' ) 
    BEGIN
        CREATE  TABLE question
            (id int IDENTITY(1, 1)
                    NOT NULL,
             text varchar(255) NOT NULL,
             PRIMARY KEY (id)) ; 
    END 

-- ----------------------------------------------------- 
-- Table category 
-- ----------------------------------------------------- 
IF NOT EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   name = 'category' ) 
    CREATE  TABLE category
        (id int IDENTITY(1, 1)
                NOT NULL,
         name varchar(45) NOT NULL,
         adverb varchar(45) NOT NULL,
         PRIMARY KEY (Id)) ; 

-- ----------------------------------------------------- 
-- Table answer 
-- ----------------------------------------------------- 
IF NOT EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   name = 'answer' ) 
    CREATE  TABLE answer
        (id int IDENTITY(1, 1)
                NOT NULL,
         question_id int NULL,
         category_id int NULL,
         text varchar(60) NULL PRIMARY KEY (Id),
         CONSTRAINT fk_answer_question FOREIGN KEY (question_id) REFERENCES question (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
         CONSTRAINT fk_answer_category1 FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE NO ACTION ON UPDATE NO ACTION);

CREATE INDEX fk_answer_question ON answer(question_id ASC) 
CREATE INDEX fk_answer_category1 ON answer(category_id ASC) 

请注意以下更改:

  • AUTO_INCRMENT 已交换
    为了身份。您指定起始值和增量
  • MSSQL 没有 SIGNED 或 UNSIGNED ints 的概念
  • 默认情况下,主键将被创建为聚集索引 除非指定
  • ,否则索引将被创建为非唯一且非聚集索引

列名“text”为保留关键字,也应该更改,以阻止任何解析问题。

希望有帮助。

Peter,

there are some differences between MySQL and MSSQL that you need to consider here, especially schemas. I am not too sure how they work with MySQL, but it seems to be almost like what MSSQL calls a database in its own right.

A schema in MSSQL is more a security abstraction layer and used to group objects inside a database. It is something that is not greatly used AFAIK, but something that MS would like to promote. I have left it out here, and the objects are then created in the default schema (normally dbo).

Needless to say, the rest is quite straight forward:

-- ----------------------------------------------------- 
-- Table question
-- ----------------------------------------------------- 
IF NOT EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   name = 'question' ) 
    BEGIN
        CREATE  TABLE question
            (id int IDENTITY(1, 1)
                    NOT NULL,
             text varchar(255) NOT NULL,
             PRIMARY KEY (id)) ; 
    END 

-- ----------------------------------------------------- 
-- Table category 
-- ----------------------------------------------------- 
IF NOT EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   name = 'category' ) 
    CREATE  TABLE category
        (id int IDENTITY(1, 1)
                NOT NULL,
         name varchar(45) NOT NULL,
         adverb varchar(45) NOT NULL,
         PRIMARY KEY (Id)) ; 

-- ----------------------------------------------------- 
-- Table answer 
-- ----------------------------------------------------- 
IF NOT EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   name = 'answer' ) 
    CREATE  TABLE answer
        (id int IDENTITY(1, 1)
                NOT NULL,
         question_id int NULL,
         category_id int NULL,
         text varchar(60) NULL PRIMARY KEY (Id),
         CONSTRAINT fk_answer_question FOREIGN KEY (question_id) REFERENCES question (id) ON DELETE NO ACTION ON UPDATE NO ACTION,
         CONSTRAINT fk_answer_category1 FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE NO ACTION ON UPDATE NO ACTION);

CREATE INDEX fk_answer_question ON answer(question_id ASC) 
CREATE INDEX fk_answer_category1 ON answer(category_id ASC) 

Please note the following changes:

  • AUTO_INCREMENT is swapped
    for IDENTITY. You specify the start value and the increment
  • MSSQL doesnt have the notion of SIGNED or UNSIGNED ints
  • The Primary key will be created as a clustered index by default
  • The indexes will be created as non-unique and non clustered unless specified

The columnname 'text' is a reserved keyword and should be changed too, to stop any parsing problems.

Hope that helps.

难如初 2024-09-17 21:37:31

另一种可能性(如果您有权访问 MySQL 数据库本身,而不是 DDL)是使用 Microsoft 发布的迁移向导:

对于迁移到 SQL Server 2008: com/downloads/details.aspx?FamilyID=0e6168b0-2d0c-4076-96c2-60bd25294a8e&displaylang=en" rel="nofollow noreferrer">http://www.microsoft.com/downloads/details.aspx?FamilyID=0e6168b0 -2d0c-4076-96c2-60bd25294a8e&displaylang=en

对于迁移到 SQL Server 2005:http://www.microsoft.com/downloads/details.aspx?FamilyID=c6f14640-da22-4604-aaaa-a45de4a0cd4a&displaylang=en

我已经使用了 SQL Server 2008 的向导...它运行良好。

-布莱恩

Another possibility (if you have access to the MySQL database itself, as opposed to the DDL) is to use the migration wizard that Microsoft has released:

For migrating to SQL Server 2008: http://www.microsoft.com/downloads/details.aspx?FamilyID=0e6168b0-2d0c-4076-96c2-60bd25294a8e&displaylang=en

For migrating to SQL Server 2005: http://www.microsoft.com/downloads/details.aspx?FamilyID=c6f14640-da22-4604-aaaa-a45de4a0cd4a&displaylang=en

I've used the wizard for SQL Server 2008...it works well.

-Brian

友欢 2024-09-17 21:37:31

我正在将 DDL 从 MySQL Server 版本 5.7.12 导出到 SQL Server 版本 12.00.6024,并发现以下内容:

  1. 当我从 MySQL 导出 DDL 时,列名称周围有反引号,例如 `my_column_name` 但当我从 SQL Server 导出时,列名会被括号 [my_column_name] 括起来。

我通过首先对所有反勾号进行文本搜索并将其替换为左括号 ([),然后对左括号进行搜索,后跟一个空格 ([),并将它们替换为右括号,后跟一个空格 (],从而解决了这个问题),然后搜索左括号,后跟逗号 ([,),并将其替换为右括号,后跟逗号 (],),依此类推。

  1. MySQL 使用关键字KEY,它的意思是INDEX。由于还识别了实际的PRIMARY KEYFOREIGN KEY,因此我必须手动检查脚本并替换。

  2. MySQL 导出类型为 bigint(20) 的列。我将它们更改为 bigint

  3. 我将 CHARACTER SET latin1 更改为 COLLATE latin1

  4. 我将 double 更改为 float(53)

  5. 我删除了UNIQUE KEY [NAME] ([NAME]),并在创建表后创建了一条alter语句ALTER TABLE [my_table] ADD CONSTRAINT UNQ_NAME UNIQUE([NAME])< /code>。

I am exporting DDL from MySQL Server version 5.7.12 into SQL Server version 12.00.6024 and have discovered the following:

  1. When I export DDL from MySQL, the column names are surrounded with back ticks, e.g. `my_column_name` but when I export from SQL Server, the column names are surrounded by brackets [my_column_name].

I got around this by first doing a text search on all back ticks and replacing them with left brackets ([), then doing a search on left brackets followed by a space ([ ) and replacing them with right brackets followed by a space (] ), then doing a search on left brackets followed by a comma ([,) and replacing them with right brackets followed by a comma (],) and so forth.

  1. MySQL uses the keyword KEY where it means INDEX. Since there are actual PRIMARY KEYand FOREIGN KEY also identified, I had to manually go through the script and replace.

  2. MySQL exported columns with type bigint(20). I changed these to bigint

  3. I changed CHARACTER SET latin1 to COLLATE latin1

  4. I changed double to float(53).

  5. I removed UNIQUE KEY [NAME] ([NAME]), and created an alter statement after the table creation ALTER TABLE [my_table] ADD CONSTRAINT UNQ_NAME UNIQUE([NAME]).

自找没趣 2024-09-17 21:37:31

在导出所有表定义两次后,我放弃了,只是用 Python 编写了代码。

count = 0
unique_keys = []
with open(my_sql_file_path, 'r') as input_file:
    data = input_file.readlines()

for line in data:
    count = count + 1
    if '`;' in line:
        line = line.replace('`;', '];')
    if '`,' in line:
        line = line.replace('`,', '],')
    if '` ' in line:
        line = line.replace('` ', '] ')
    if '`)' in line:
        line = line.replace('`)', '])')
    if '`\n' in line:
        line = line.replace('`\n', ']\n')
    if '`' in line:
        line = line.replace('`', '[')
    if 'ENGINE' in line.upper():
        line = line.replace('ENGINE', '-- ENGINE')
    if 'DOUBLE' in line.upper():
        line = line.replace('double', 'float(53)')
        line = line.replace('DOUBLE', 'FLOAT(53)')
    if 'ENGINE' in line.upper():
        line = line.replace('ENGINE', '-- ENGINE')
        line = line.replace('engine', '-- ENGINE')
    if 'bigint(20)' in line:
        line = line.replace('bigint(20)', 'bigint')
    if 'bigint(15)' in line:
        line = line.replace('bigint(15)', 'bigint')
    if 'CHARACTER SET' in line:
        line = line.replace('CHARACTER SET', 'COLLATE')
    if 'KEY' in line:
        if 'PRIMARY KEY' not in line \
                and 'UNIQUE KEY' not in line\
                and '_KEY_' not in line:
            line = line.replace('KEY', 'INDEX')
        if 'UNIQUE KEY' in line:
            row_num = (str(count) + " ")
            unique_keys.append(row_num)
        print(line)
print("Manually change these UNIQUE KEY lines to ALTER TABLE statements", unique_keys)

After exporting all the table definitions twice, I gave up and just coded the thing in Python.

count = 0
unique_keys = []
with open(my_sql_file_path, 'r') as input_file:
    data = input_file.readlines()

for line in data:
    count = count + 1
    if '`;' in line:
        line = line.replace('`;', '];')
    if '`,' in line:
        line = line.replace('`,', '],')
    if '` ' in line:
        line = line.replace('` ', '] ')
    if '`)' in line:
        line = line.replace('`)', '])')
    if '`\n' in line:
        line = line.replace('`\n', ']\n')
    if '`' in line:
        line = line.replace('`', '[')
    if 'ENGINE' in line.upper():
        line = line.replace('ENGINE', '-- ENGINE')
    if 'DOUBLE' in line.upper():
        line = line.replace('double', 'float(53)')
        line = line.replace('DOUBLE', 'FLOAT(53)')
    if 'ENGINE' in line.upper():
        line = line.replace('ENGINE', '-- ENGINE')
        line = line.replace('engine', '-- ENGINE')
    if 'bigint(20)' in line:
        line = line.replace('bigint(20)', 'bigint')
    if 'bigint(15)' in line:
        line = line.replace('bigint(15)', 'bigint')
    if 'CHARACTER SET' in line:
        line = line.replace('CHARACTER SET', 'COLLATE')
    if 'KEY' in line:
        if 'PRIMARY KEY' not in line \
                and 'UNIQUE KEY' not in line\
                and '_KEY_' not in line:
            line = line.replace('KEY', 'INDEX')
        if 'UNIQUE KEY' in line:
            row_num = (str(count) + " ")
            unique_keys.append(row_num)
        print(line)
print("Manually change these UNIQUE KEY lines to ALTER TABLE statements", unique_keys)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文