Using REPLACE to Insert New Rows in MySQL

In MySQL the REPLACE statement is normally used to replace existing rows in a table. But it’s not limited to just replacing existing rows. There are times when REPLACE will also insert new rows. It all depends on the incoming data, as well as how the table is structured in relation to the incoming data.

Read more

How to Change the Comment on a Scheduled Event in MySQL

Suppose you have an existing MySQL event that has a comment. But the comment isn’t as helpful as you’d like it to be, and so you’d like to change it to something else.

This can be done easily with the ALTER EVENT statement along with the COMMENT clause. When we include the COMMENT clause in the ALTER EVENT statement, it replaces whatever comment was in the event with the new comment. If the event didn’t have a comment, then a new comment appears.

Read more

Add a Comment to a Scheduled Event in MySQL

MySQL provides us with the ability to create scheduled events – tasks that run according to a specified schedule. Scheduled events allow for the ability to add comments within the event. We can do this with the COMMENT clause.

We can include the comment when creating the event, or we can add one to an existing event.

Read more

Add a Composite Primary Key to an Existing Table in MySQL

In MySQL, adding a composite primary key to an existing table is similar to adding a singular primary key. We define the primary key with the ALTER TABLE statement.

However, when adding a composite primary key we need to use the ADD PRIMARY KEY clause. We can’t define it at the column level. With singular primary keys on the other hand, we have the choice of using the ADD PRIMARY KEY clause or applying it at the column level.

Read more

How Scheduled Events Deal with sql_mode in MySQL

When we create a scheduled event in MySQL, the current sql_mode is stored with the event. That causes the event to use the sql_mode that was in effect at the time the event was created. Same with altering an event.

Therefore, if we change the sql_mode after the event was created or altered, it won’t have any impact on the event. In other words, we can safely change our system’s sql_mode without worrying about whether it’s going to mess up any existing scheduled events.

Read more

3 Ways to Check the Event Scheduler Status in MySQL

In MySQL we can create events that run on a specified schedule. But events will only run if the Event Scheduler is on. If the Event Scheduler is off or disabled, then such events won’t run as scheduled.

Therefore if we create any events on the server, it’s a good idea to check whether the Event Scheduler is on or not.

Here are three options for doing this using SQL.

Read more

Disable Quotes from Table and Column Names When Using SHOW CREATE TABLE in MySQL

If you’re using MySQL’s SHOW CREATE TABLE statement, but you don’t want quotes to be included in the output, this is easily addressed.

By default, SHOW CREATE TABLE (and SHOW CREATE DATABASE) includes quotes around table and column names, etc. When I say “by default”, I mean assuming you haven’t yet changed anything to disable these quotes.

Fortunately there’s an easy way to disable these quotes. MySQL provides us with the sql_quote_show_create system variable. This variable can be set to 1 or 0, which enables and disables such quotes respectively.

Read more

How to Select a GIPK in MySQL

In MySQL, a GIPK is an invisible primary key that was automatically generated by MySQL due to the fact that a primary key wasn’t explicitly defined in the statement that created the table.

Given that it’s an invisible column, the GIPK column isn’t returned when we use a SELECT * or TABLE statement to return the table’s contents.

However, as with any invisible column, we can still select the GIPK by explicitly including it in our SELECT list.

Read more