How to Migrate Triggers from SQL Server to MySQL

How to Migrate Triggers from SQL Server to MySQL

When migrating from SQL Server to MySQL, handling triggers properly is extremely important since it is essential part of the database logic. There is a little chance of automating this conversion because syntax of triggers creation is different in those two DBMS. The purpose of this article is to highlight the processes involved in migration of triggers from SQL Server to MySQL. It is supposed that audience has the basic knowledge of database programming.

First significant difference of trigger definitions in SQL Server and MySQL is the way of referencing old and new data. An important feature of MySQL that you must understand is that there are appropriate references for data types such as:

  • Old (existing) – OLD.column_name
  • New (inserted or updated) – NEW.column_name

Both values are stored in SQL server in two service tables namely ‘DELETED’ and ‘INSERTED’. To access the data, you have to do the appropriate selects. This rule can be illustrated by the following case. Assume, there is a MS SQL trigger:

CREATE TRIGGER [dbo].[update_archive] ON [dbo].[archive]

INSTEAD OF INSERT

UPDATE dbo.archive

SET archive.AssignedReadingPhysicianID = 

INSERTED.ReadingPhysicianID

FROM dbo.archive INNER JOIN INSERTED ON archive.StudyUID = 

INSERTED.StudyUID

WHERE archive.ReportCount = 0

It can be migrated to MySQL as follows:

CREATE TRIGGER update_archive

AFTER UPDATE ON archive FOR EACH ROW

BEGIN

IF(NEW.ReadingPhysicianID <> OLD.ReadingPhysicianID) THEN

UPDATE archive SET archive.AssignedReadingPhysicianID = NEW.ReadingPhysicianID;

...

END$$

DELIMITER ;

Also, single trigger in SQL Server can handle multiple actions which are insert, update, and delete. MySQL does not have such capability, so triggers must be duplicated for each action. By using a cursor, SQL Server can iterate both new and old ad ‘reading from table’. Similarly, the declaration ‘FOR EACH ROW’ is the channel through which MySQL uses an implicit loop.

All three differences can be illustrated. This will be shown by stating examples of the conversion of triggers from SQL Server to MySQL. For this purpose, we shall assume we have a trigger which inserts ‘defined’ in SQL Server after converting a string into lowercase. By practice, this is:

CREATE TRIGGER to_lowercase ON journal instead of INSERT AS

INSERT INTO journal(code) SELECT LOWER(code) FROM INSERTED;

In MySQL, this trigger takes another form such as:

DROP TRIGGER to_lowercase;

DELIMITER $$  

CREATE TRIGGER to_lowercase before INSERT ON journal FOR EACH ROW

BEGIN

SET NEW.code = LOWER(NEW.code);

END$$

DELIMITER ;

There are some entities called square brackets in the SQL Server code which are used to enclose database objects. But in MySQL, this syntax is not allowed. Instead, all square brackets have to be replaced by ` symbol or cut off: [object] -> `object`.

In addition, MS SQL has a system support for schemas. Schemas are virtual container grouping which, by similar semantics or ownership, categorize objects within a single database. Owing to this, the full name of database object is put in writing as database.schema.object. In MySQL, all schema names have to be removed from the source code because of its inability to support schemas.

The table below shows how to convert embedded from SQL Server to MySQL equivalents:

SQL ServerMySQL
CONVERT($type, $expression)CAST($expression AS $type)
LEN($expression)LENGTH($expression)
DATEADD(year, 1, $expression)$expression + interval 1 year
DATEADD(month, 2, $expression)$expression + interval 2 month
DATEADD(day, 3, $expression)$expression + interval 3 day
GETDATE()NOW()
‘string1’ + ‘string2’CONCAT(‘string1’, ‘string2’)
CONTAINS($expression, $template)$expression LIKE $template
CHARINDEX ($exp1, $exp2)LOCATE($exp1, $exp2)

 

About the author

James

James Miller

James is the Senior Writer & Rumors Analyst at PC-Tablet.com, bringing over 6 years of experience in tech journalism. With a postgraduate degree in Biotechnology, he merges his scientific knowledge with a strong passion for technology. James oversees the office staff writers, ensuring they are updated with the latest tech developments and trends. Though quiet by nature, he is an avid Lacrosse player and a dedicated analyst of tech rumors. His experience and expertise make him a vital asset to the team, contributing to the site’s cutting-edge content.

Add Comment

Click here to post a comment

Web Stories

5 Best Projectors in 2024: Top Long Throw and Laser Projectors for Every Budget 5 Best Laptop of 2024 5 Best Gaming Phones in Sept 2024: Motorola Edge Plus, iPhone 15 Pro Max & More! 6 Best Football Games of all time: from Pro Evolution Soccer to Football Manager 5 Best Lightweight Laptops for High School and College Students 5 Best Bluetooth Speaker in 2024 6 Best Android Phones Under $100 in 2024 6 Best Wireless Earbuds for 2024: Find Your Perfect Pair for Crystal-Clear Audio Best Macbook Air Deals on 13 & 15-inch Models Start from $149