If you need to do some inserts and updates in MySQL, one option is to run separate INSERT
and UPDATE
statements. Another option is to use the INSERT ... ON DUPLICATE KEY UPDATE
statement. Depending on the scenario, this can be a handy option.
Category: MySQL
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 readingHow 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.
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.
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.
2 Ways to View an Event’s Comment in MySQL
When we create a scheduled event in MySQL, we have the option of adding a comment to the event. This comment can be a string of up to 64 characters. We have a couple of options when it comes to viewing the comment of an existing event.
Continue readingHow to Rename a Scheduled Event in MySQL
In MySQL we can use the ALTER EVENT
statement to make changes to existing scheduled events, including changing the name of the event.
2 Ways to Drop an Event in MySQL
In MySQL, scheduled events are tasks that run according to a specified schedule. When we no longer require an event we can drop it so that it doesn’t take up unnecessary room in the system. We have a couple of ways of going about this.
Continue readingHow to Enable/Disable a Scheduled Event in MySQL
When we create a scheduled event in MySQL, it’s enabled by default. But we can change this.
We can create an event in disabled status, then enable it later. Or we can do the opposite – create it as an enabled event and then disable it later.
Continue readingHow 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
.