Technology
MSSQL to MySQL Migration
Being a highly popular relational DBMS, SQL Server comes with high total cost of ownership (TCO) for large projects and restrictive licensing policy. This is the reason of migrating databases from MS SQL to an open-source database management system for some companies and organizations.
When looking for advanced open-source system that can handle large and sophisticated databases, database professionals consider either MySQL or PostgreSQL. MySQL offers the most important features expected from advanced DBMS such as high performance, security, scalability and large collection of administration tools. At the same time, it is easier to configure and deploy than PostgreSQL. This makes MySQL a reasonable choice for small and medium size companies requiring efficiency, reliability and high level of the data integrity.
The procedure of MSSQL to MySQL migration combines the following steps:
- MS SQL table definitions are exported from the database in form of DDL statements
- these statements are converted into MySQL format with respect to appropriate type mapping and imported into the target database
- SQL Server data is exported into comma separated values (CSV) files
- necessary transformations are made over the data, so it is complied with MySQL format
- the resulting data is imported into MySQL database
Table Structures
Table definitions can be extracted from MS SQL database in form of DDL statements as follows.
- Open MS SQL Management Studio
- Right-click on database in the left pane
- In popup menu select ‘Tasks’ and then ‘Generate Scripts’ item
- On the ‘Set scripting options’ tab of the appeared window click on ‘Advanced’
The output script must be pre-processed before importing into MySQL database. All statements missing in MySQL such “SET ANSI_NULLS ON”, “SET QUOTED_IDENTIFIER ON”, “SET ANSI_PADDING ON” must be removed. Square brackets around tables, column or index names must be replaced by back ticks. Square brackets enclosing data types must be removed.
As a part of MSSQL to MySQL migration, all the source data types missing in MySQL must be mapped as it is specified in the table below:
SQL Server | MySQL |
---|---|
Bit | tinyint(1) |
char(length > 255) | text |
datetime2 | datetime |
datetimeoffset | timestamp |
geography | longtext |
image | longblob |
money, smallmoney | decimal(18,2) |
nchar(length > 255) | text |
Ntext | longtext |
nvarchar(length > 255) | text |
Smalldatetime | datetime |
Text | longtext |
time2 | time |
timestamp | longblob |
uniqueidentifier | varchar(36) |
varbinary(max) | longblob |
varchar(length > 255) | text |
varchar(max) | text |
Xml | longtext |
Do not forget to replace IDENTITY attribute of INT or BIGINT column by AUTO_INCREMENT. Final processing of SQL Server output script includes the following steps:
- All keywords that are not supported by MySQL like ‘WITH NOCHECK’, ‘CLUSTERED’, ‘ON PRIMARY’ must be removed
- SQL Server query terminator ‘GO’ must be replaced by MySQL equivalent ‘;’
Data Migration
Once table structures are migrated, it is time for MSSQL to MySQL migration of the data. MS SQL Management Studio allows to export the data as follows:
- Open the program and right-click on database in the left pane
- Select items ‘Tasks’ > ‘Export Data’ in the popup menu
- Go through the wizard dialog, select data source ‘Microsoft OLE DB Provider for SQL Server’ and target ‘Flat File Destination’.
- Select ‘data only’ for ‘Types of data to script’
At the completion of export procedure, data will be stored into the specified CSV file.
Binary data requires some pre-processing before loading into MySQL. On ‘Specify Table Copy or Query’ wizard page select ‘Write a query to specify the data to transfer’ option. Then compose SELECT-query on the next wizard page as follows:
select {non-binary column #1}, {non-binary column #2}, cast(master.sys.fn_varbintohexstr(
cast({binary column name} as varbinary(max))) as varchar(max)) as {binary column name} from {table name};
Note, the workaround does not work for binary data larger that 1MB, the query goes into infinite hang. The data stored in CSV files can be imported into MySQL database using the standard mysqlimport tool.
Indexes
Migrating indexes from MSSQL to MySQL can be a bit tricky because the two databases use different internal structures and syntax for creating and managing indexes. However, the migration process can be broken down into manageable steps. Below is a guide on MSSQL to MySQL migration of indexes, with attention to potential challenges and how to overcome them.
Before you start the migration process, you need to understand the existing indexes in your MSSQL database. This includes the types of indexes, their structure, and how they are used.
- Identify the Tables and Indexes: Use the following query to retrieve a list of tables and their corresponding indexes in your MS SQL Server database:
SELECT
t.name AS TableName,
i.name AS IndexName,
i.type_desc AS IndexType
FROM
sys.tables AS t
JOIN sys.indexes AS i ON t.object_id = i.object_id
WHERE
i.type_desc <> ‘HEAP’ — Exclude heap tables without indexes
ORDER BY
t.name, i.name;
- Map MS SQL Index Types to MySQL. Not all index types in MS SQL have direct equivalents in MySQL. Below is a general mapping of common MS SQL index types to MySQL:
- Clustered Index (MS SQL) → PRIMARY KEY (MySQL) or INDEX: In MS SQL, the clustered index determines the physical order of the data. MySQL doesn’t have a direct equivalent for clustered indexes, but the PRIMARY KEY in MySQL implicitly creates a clustered index. Other non-clustered indexes in MS SQL should be mapped to INDEX or UNIQUE indexes in MySQL.
- Non-Clustered Index (MS SQL) → INDEX (MySQL): This is the most common index type and can directly be mapped to MySQL’s INDEX. MySQL supports multiple non-clustered indexes, and you can create them with the CREATE INDEX statement.
- Unique Index (MS SQL) → UNIQUE (MySQL): Both MS SQL and MySQL support unique indexes, so this type of index can be easily migrated.
- Full-Text Index (MS SQL) → FULLTEXT (MySQL): Full-text indexes in MS SQL Server need to be migrated to MySQL’s FULLTEXT indexes. Keep in mind that MySQL’s full-text search has different functionality, so some adjustments to your queries may be required.
Modify index creation statements for MySQL Syntax. While MS SQL and MySQL use similar syntax for creating indexes, there are some differences you need to be aware of:
Index Naming: MS SQL allows index names to contain special characters like spaces, while MySQL does not. Make sure to modify any index names that could cause conflicts when migrating.
Include Tablespaces (Optional): MySQL supports tablespaces in some editions (e.g., InnoDB), so you may need to include TABLESPACE clauses if you’re working with large databases.
Index Type Differences: Ensure that the index types are mapped correctly. For example, in MS SQL, you might have an XML or spatial index, which requires special handling in MySQL (such as using SPATIAL or FULLTEXT indexes in MySQL).
Once the index creation scripts are modified, you can proceed with executing them on your MySQL database.
Conclusion
MSSQL to MySQL migration can be a complex but highly rewarding process. While both databases serve similar purposes, the underlying architecture differs, making it essential to understand the key distinctions and carefully map data types during migration. By assessing your existing MS SQL database, choosing the right tools, and adjusting scripts for compatibility with MySQL, you can successfully complete the migration.
The success of the migration depends on careful planning, testing, and a strategic approach to the procedure. MSSQL to MySQL migration can significantly improve the performance, scalability, and cost-effectiveness of your database systems. By following best practices and continuously optimization, you can ensure that your MySQL database operates efficiently and meets your organization’s needs for years to come.
Shubham Kansal is a tech-freak self-motivated professional who thrives on innovation and overcoming challenges. He is working on Tech Zimo for the last 4 years and went through writing for a lot of big media houses. Writing is her all-time favorite job. Know more about him on facebook,