Prepared Statements in MySQL: A Complete Guide with Examples

In MySQL, prepared statements are a feature that allows us to execute the same SQL query multiple times with different parameters. They can improve performance, make our code cleaner, and help defend against SQL injection attacks.

In this article, we’ll explore prepared statements in MySQL, with examples and practical tips on how to use them effectively. We’ll also cover the importance of deallocating statements to free up resources.

Continue reading

How to Use the HANDLER Statement in MySQL for Faster Data Access

The HANDLER statement in MySQL provides a low-level mechanism to directly access storage engine-level functionality, bypassing some of the overhead associated with regular SQL queries. It can be especially useful for traversing a database in high-performance scenarios.

HANDLER is available for MyISAM and InnoDB tables, and can be used as a faster alternative to the SELECT statement.

Continue reading

Quick Intro to the event_scheduler System Variable in MySQL

In MySQL, the event_scheduler system variable is used to start and stop the Event Scheduler, as well as enable or disable it.

At runtime we can set the value to ON or OFF, and we can check the current value of the variable. We can also disable the Event Scheduler at server startup, but we can’t do this at runtime. However, we can still check the event_scheduler variable to see whether it’s enabled or disabled.

Continue reading

How to Create a MySQL Event Only if it Doesn’t Already Exist

In MySQL we can use the CREATE EVENT statement to create scheduled events. As with many CREATE ... statements, we have the option of using the IF NOT EXISTS clause to specify that the object should only be created if it doesn’t already exist.

Of course, we wouldn’t normally be trying to create an event if we know that it already exists. But there may be times where we’re not sure, and we want our code to handle this scenario without throwing an error if an event with the same name already exists. This is common when creating scripts that are designed to be run across multiple environments. That’s where the IF NOT EXISTS clause can come in handy.

Continue reading

How to “Unhide” a GIPK in MySQL

In MySQL, GIPKs are invisible by definition. GIPK stands for generated invisible primary key, and it’s basically an invisible column automatically created by MySQL with a primary key constraint.

However, just because GIPKs are automatically created invisible, it doesn’t mean that we can’t “unhide” them – or make them visible.

We can make a GIPK visible just as we would make any other invisible column visible – use the ALTER TABLE statement to set it to VISIBLE.

Continue reading