The information schema is a standard set of read-only views that provide metadata about objects in a database. These views are typically accessed by qualifying the view name with INFORMATION_SCHEMA
(for example INFORMATION_SCHEMA.TABLES
). But before we do this, we need to know which views are available in the first place.
Category: MySQL
Introduction to the sys.ps_thread_stack() Function in MySQL
In MySQL, the sys.ps_thread_stack()
function returns a JSON formatted stack of all statements, stages, and events within the Performance Schema for a given thread ID.
2 Ways to Add a DEFAULT Clause to an Existing Column in MySQL
If you have an existing column in MySQL that you want to add a DEFAULT
clause to, the following example may help.
To explicitly add a DEFAULT
clause to an existing column in MySQL, we can use the ALTER TABLE
statement along with the MODIFY
clause.
We can also add a DEFAULT
clause implicitly by defining a column as nullable. When we do this, MySQL implicitly adds its own DEFAULT
clause.
Understanding the sys.extract_schema_from_file_name() Function in MySQL
In MySQL, the sys.extract_schema_from_file_name()
function is a stored function in the sys
schema.
It accepts a path name, and returns the path component that represents the schema name.
This enables us to get the schema from a given file path.
Continue readingHow to Enforce a CHECK Constraint in MySQL
If we have a CHECK
constraint that’s currently not enforced, we can easily change this so that it’s enforced.
We can use the following code to enforce a CHECK
constraint in MySQL.
Fix Error 1138 “Invalid use of NULL value” in MySQL
If you’re getting error 1138 that reads “invalid use of NULL value“, it could be because you’re trying to change the definition of a column to require non-NULL
values, but the column contains NULL
values.
This can happen when we try to change a column from NOT NULL
to NULL
. It can also happen when we try to set a column as a primary key, but that column contains NULL
values.
To fix this issue, be sure to ensure that the column you’re modifying doesn’t contain any NULL
values. Alternatively, if the column should be able to accept NULL
values, then you should change your column definition to allow NULL
values.
How to Add a CHECK Constraint to an Existing Table in MySQL
As of MySQL 8.0.16, we can create CHECK
constraints in MySQL. We can create them at the time of creating the table, or we can add the constraint later.
To add a CHECK
constraint to an existing table, we can use the ALTER TABLE
statement.
SERIAL vs AUTO_INCREMENT in MySQL
If you’ve ever encountered a SERIAL
column in a MySQL database, you might’ve wondered how it differs from an AUTO_INCREMENT
column. After all, they both generate values that automatically increment?
Read on to find out what the difference is between SERIAL
and AUTO_INCREMENT
in MySQL.
3 JSON Utility Functions in MySQL
MySQL provides us with a few utility functions that act on JSON values, or strings that can be parsed as JSON values.
Below are three JSON utility functions that we can use for such tasks.
Continue readingHow to Add Headings for WITH ROLLUP in MySQL
MySQL provides us with the WITH ROLLUP
modifier for the GROUP BY
function for generating super aggregate data.
But by default, this modifier doesn’t label its data. By this I mean, it doesn’t provide us with meaningful labels for the rows that it generates. It simply uses NULL
in those rows.
Maybe you’ve encountered this and are now looking for a solution.
Below is a handy little technique we can use to replace NULL
with our own headings for these rows.