Validated on 1 Mar 2021 • Last edited on 9 Jul 2024
sql mode
MySQL is an open source, object-relational database built with speed and reliability in mind. Its large and active developer community has created many third-party applications, tools, and libraries that expand MySQL’s functionality.
MySQL can operate in different SQL modes. Global SQL modes affect the SQL syntax MySQL supports and the data validation checks it performs. You can add and remove modes as needed, or remove all modes from your cluster if desired.
Set Global SQL Mode Using the CLI
How to Set Global SQL Mode Using the DigitalOcean CLI
doctl databases sql-mode set <database-cluster-id> <sql-mode-1> ... <sql-mode-n> [flags]
The following example sets the SQL mode ALLOW_INVALID_DATES for an existing database cluster with the ID ca9f591d-f38h-5555-a0ef-1c02d1d1e35. The cluster already has the modes NO_ZERO_DATE, NO_ZERO_IN_DATE, STRICT_ALL_TABLES set, but they must be included in the command to avoid being overwritten by the additional mode:
doctl databases sql-mode set ca9f591d-f38h-5555-a0ef-1c02d1d1e35 NO_ZERO_DATE NO_ZERO_IN_DATE STRICT_ALL_TABLES ALLOW_INVALID_DATES
Set Global SQL Mode Using the API
How to Set Global SQL Mode Using the DigitalOcean API
import os
from pydo import Client
client = Client(token=os.environ.get("DIGITALOCEAN_TOKEN"))
req = {
"sql_mode": "ANSI,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE"
}
update_resp = client.databases.update_sql_mode(database_cluster_uuid="a7a8bas", body=req)
Set Global SQL Mode using the Control Panel
To set the global SQL mode for a MySQL cluster, find the cluster on the Databases page and then click the cluster’s name. From the database’s Overview page, click the Settings tab to view the settings for the cluster.
In the Global SQL mode section, you can see the current SQL modes in use. To add or remove modes, click Edit.
Select an available SQL mode from the drop-down menu or start typing the name of a mode to select from the results that match. When finished, click the Save button to apply the changes or the Cancel button to cancel editing. You can click the Reset to the default MySQL 8 SQL modes links to revert to the default settings.
To remove modes, mouse over the mode you want to remove and then click the “x” icon to remove it from your cluster.
The default SQL modes for MySQL on managed databases are as follows:
ANSI (shorthand)
ERROR_FOR_DIVISION_BY_ZERO
NO_ENGINE_SUBSTITUTION
NO_ZERO_DATE
NO_ZERO_IN_DATE
STRICT_ALL_TABLES
ALLOW_INVALID_DATES
In certain cases, you may also want to adjust the sql_require_primary_key, which determines whether primary keys are a requirement. When set to True (default), primary keys are required. When set to False, primary keys are optional. However, we do not currently support adjusting this mode in the control panel. Instead, you can set this mode by making a configuration request via our API.
Warning
For clusters with more than one node, we strongly recommend keeping sql_require_primary_key as True. Setting it to False may severely impact replication and cause issues.