SQL Mode¶
sql_mode is a system parameter in MatrixOne that specifies the mode in which MatrixOne performs queries and operations. sql_mode can affect the syntax and semantic rules of MatrixOne, changing the behavior of MatrixOne queries for SQL. In this article, you will be introduced to the mode of sql_mode, what it does, and how to set SQL mode.
By default, sql_mode includes ERROR_FOR_DIVISION_BY_ZERO, NO_ENGINE_SUBSTITUTION, NO_ZERO_DATE, NO_ZERO_IN_DATE, ONLY_FULL_GROUP_BY, and STRICT_TRANS_TABLES. MatrixOne accepts multiple MySQL-compatible SQL mode names, but only a tested subset changes parser or execution behavior. Do not assume that every accepted mode implements all MySQL semantics.
ANSI_QUOTES is accepted but does not change quoting behavior; double-quoted tokens remain string literals. PIPES_AS_CONCAT is accepted but does not control ||; MatrixOne treats || as concatenation regardless of this mode. Strict modes, NO_ZERO_DATE, and ERROR_FOR_DIVISION_BY_ZERO enable corresponding checks in supported write and conversion paths. ONLY_FULL_GROUP_BY controls grouping behavior; other accepted mode names are not guaranteed to implement complete MySQL semantics.
View sql_mode¶
View sql_mode in MatrixOne using the following command:
SELECT @@global.sql_mode;
-- global mode
SELECT @@session.sql_mode;
-- session mode
Set sql_mode¶
Set sql_mode in MatrixOne using the following command:
set global sql_mode = 'xxx' -- global mode, reconnecting to database takes effect
set session sql_mode = 'xxx' -- session mode
Examples¶
CREATE TABLE student(
id int,
name char(20),
age int,
nation char(20)
);
INSERT INTO student values(1,'tom',18,'上海'),(2,'jan',19,'上海'),(3,'jen',20,'北京'),(4,'bob',20,'北京'),(5,'tim',20,'广州');
mysql> select * from student group by nation;
-- This operation is not supported in `ONLY_FULL_GROUP_BY` mode
ERROR 1149 (HY000): SQL syntax error: column "student.id" must appear in the GROUP BY clause or be used in an aggregate function
mysql> SET session sql_mode='ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,STRICT_TRANS_TABLES';
-- Turns off ONLY_FULL_GROUP_BY mode for the current session
Query OK, 0 rows affected (0.02 sec)
mysql> select * from student group by nation;
-- Turns off `ONLY_FULL_GROUP_BY` mode immediately in the current session
+------+------+------+--------+
| id | name | age | nation |
+------+------+------+--------+
| 1 | tom | 18 | 上海 |
| 3 | jen | 20 | 北京 |
| 5 | tim | 20 | 广州 |
+------+------+------+--------+
3 rows in set (0.00 sec)
mysql> SET global sql_mode='ONLY_FULL_GROUP_BY';
-- Set the global ONLY_FULL_GROUP_BY mode on.
Query OK, 0 rows affected (0.02 sec)
mysql> select * from student group by nation;
-- ONLY_FULL_GROUP_BY mode does not take effect, because you need to reconnect to the database for global mode to take effect.
+------+------+------+--------+
| id | name | age | nation |
+------+------+------+--------+
| 1 | tom | 18 | 上海 |
| 3 | jen | 20 | 北京 |
| 5 | tim | 20 | 广州 |
+------+------+------+--------+
3 rows in set (0.00 sec)
-- Exit the current session
mysql> exit;
mysql> select * from student group by nation;
-- After reconnecting the database and executing the query, ONLY_FULL_GROUP_BY mode is successfully enabled.
ERROR 1149 (HY000): SQL syntax error: column "student.id" must appear in the GROUP BY clause or be used in an aggregate function