The MySQL REPLACE
statement allows us to replace data in a table with new data. The new data can be explicitly provided in the statement or it could come from the result of a query. Therefore, we can use the REPLACE
statement to effectively “refresh” a table with new/updated data from another table.
Tag: how to
Fix “Unknown event” Error in MySQL when Trying to Drop an Event
If you’re getting an error that reads something like “ERROR 1539 (HY000): Unknown event ‘Event1’” in MySQL when trying to drop an event, it’s probably because the event that you’re trying to drop doesn’t exist.
There are a couple of ways to address this issue. One way is to use the IF EXISTS
clause so that dropping a non-existent event doesn’t cause an error. Another way is to check the name of the event that you’re trying to drop – it could be that you’re using the wrong event name.
How to Drop a Primary Key in MySQL
In general, once we create a primary key on a table we leave it there. It’s there to help us maintain data integrity and removing it can put that data integrity at risk.
But sometimes we might have good reason to remove a primary key. Perhaps we need to drop it so that we can create a primary key on a different column or multiple columns. Or perhaps there’s another valid reason to drop the primary key.
Either way, below is an example of dropping the primary key from a column.
Continue readingFix “Variable ‘event_scheduler’ is a GLOBAL variable and should be set with SET GLOBAL” in MySQL
If you’re getting an error that reads “ERROR 1229 (HY000): Variable ‘event_scheduler’ is a GLOBAL variable and should be set with SET GLOBAL” in MySQL, it’s probably because you’re trying to set the event_scheduler
system variable, but you’re not specifying it as a global variable.
The event_scheduler
variable is a global variable and so we must specify it as a global variable.
To fix this issue, specify it as a global variable when setting it’s value.
Continue readingHow to Hide a GIPK from the SHOW CREATE TABLE Statement (and SHOW COLUMNS and SHOW INDEX) in MySQL
When we have a table with a generated invisible primary key (GIPK) in MySQL, we can usually see its definition when we use various SHOW
statements such as SHOW CREATE TABLE
, SHOW COLUMNS
, and SHOW INDEX
, as well as when we query information schema tables such as information_schema.columns
.
But there is a way of hiding the GIPK from such statements. It all comes down to the show_gipk_in_create_table_and_information_schema
variable. Yes, there’s actually a system variable that allows us to hide GIPKs from the output of various SHOW
statements and information schema tables.
Check Whether GIPKs are Enabled in MySQL
In MySQL, GIPK stands for generated invisible primary key. These are created whenever we create an InnoDB table without explicitly defining primary key.
However, MySQL will only create a GIPK if we have enabled GIPKs. More specifically, a GIPK will only be created when our sql_generate_invisible_primary_key
server system variable is set to ON
. By default this is set to OFF
.
Using REPLACE with the TABLE Statement in MySQL
When using the MySQL REPLACE
statement to update another table based on a source table, we have the option of using a SELECT
statement to select the whole table (or part of it) or the TABLE
statement to select the whole table.
Below is an example of refreshing a table from another one using the TABLE
statement with the REPLACE
statement.
5 Ways to Find a Table’s Primary Key in MySQL
Generally speaking, most tables we create in MySQL should have a primary key. A primary key is one or more columns that have been configured as the unique identifier for the table.
We usually create the primary key when creating the table, but we can also add a primary key to an existing table (assuming it doesn’t already have one).
Occasionally it might not be immediately apparent which column is the primary key for a given table. Or it might not be apparent whether or not the table has a composite primary key. Fortunately it’s easy enough to find out.
Below are five ways to get the primary key column/s from an existing table in MySQL.
Continue readingUsing the ‘key_column_usage’ Table to Get the Primary Key and Foreign Keys in MySQL
In MySQL we can use the information_schema.key_column_usage
table to get information about the keys in a table. This table returns one row for each column that is constrained as a key.
We can use this table to find out which column/s is the primary key of a given table and to return any foreign keys from the table.
Continue readingHow to Set and Check the ‘sql_quote_show_create’ System Variable in MySQL
In MySQL there’s a system variable called sql_quote_show_create
which is used to determine whether or not to use quoted identifiers when using the SHOW CREATE TABLE
and SHOW CREATE DATABASE
statements.
By default sql_quote_show_create
is set to 1
, which means that quotes/backticks will be generated whenever these statements are run, but we can also change this value to 0
in order to disable quotes.
Below are examples of checking this variable and setting it.
Continue reading