0

Given the following example table, I would like the default value of str_field_hash to contain the SHA1 hash of the value of str_field.

CREATE TABLE test (
    str_field VARCHAR(1024) NOT NULL,
    str_field_hash CHAR(40) NOT NULL,

    CONSTRAINT str_field_hash_unique UNIQUE (str_field_hash)
);

str_field is too large for a unique key constraint.

This question contains answers indicating the newer versions of MySQL/MariaDB support using functions in default column values, though I've been unable to find information on whether I can derive a default value for a column from the value provided for another column.

I know I can use a trigger for this, but I'd love to keep it all confined to the table schema itself if possible.

Developing using MySQL v8.0.11.

jdp
  • 3,446
  • 2
  • 30
  • 54
  • 2
    Specify DBMS and its version in interest precisely. – Akina May 18 '20 at 16:35
  • Updated the question. Version is `MySQL v8.0.11`. – jdp May 18 '20 at 16:38
  • If so read [MySQL 8.0 Reference Manual / Data Types / Data Type Default Values](https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html). No functions except current_timestamp. – Akina May 18 '20 at 16:40
  • That is clearly incorrect, given that the samples themselves demonstrate the use of `UUID_TO_BIN(UUID())`. – jdp May 18 '20 at 16:44
  • 1
    Please try to find the text part relative to YOUR server version... – Akina May 18 '20 at 17:34

0 Answers0