简体   繁体   中英

mysql - Invalid date error when creating stored generated column

I am trying to add a generated column for an existing table.

Within the table, there is a varchar column containing data like 321njkfvds_10911342

If I add the generated column as VIRTUAL , it works well! .

ALTER TABLE my_table
  ADD COLUMN `PartitionKey` INT 
    GENERATED ALWAYS AS (IFNULL(TO_DAYS(SUBSTRING_INDEX(`Sequence`, '_', -1)), 0)) VIRTUAL
    AFTER `Sequence`;

But if I try to add it as STORED generated column, it fails.

ALTER TABLE my_table 
  ADD COLUMN `PartitionKey` INT 
    GENERATED ALWAYS AS (IFNULL(TO_DAYS(SUBSTRING_INDEX(`Sequence`, '_', -1)), 0)) STORED 
    AFTER `Sequence`;

Error Code: 1292 Incorrect datetime value: '10911342'

I know 10911342 is not a valid date, but at least its generated column is 0 when VIRTUAL specified.

But why can't I add the generated column as STORED while VIRTUAL works? is there some way to fix it?

@@version            
---------------------
10.3.27-MariaDB-log  

Generated (Virtual and Persistent/Stored) Columns:: Making Stored Values Consistent

...

When a generated column is PERSISTENT or indexed, the value of the expression needs to be consistent regardless of the SQL Mode flags in the current session. If it is not, then the table will be seen as corrupted when the value that should actually be returned by the computed expression and the value that was previously stored and/or indexed using a different sql_mode setting disagree.

...

See dbfiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM