How can I create a foreign keys of text type in MariaDB or MySQL?

后端 未结 1 1747
梦如初夏
梦如初夏 2021-01-28 01:54

I have two tables:

CREATE TABLE first_table(
my_id TEXT(6) NOT NULL,
content VARCHAR(30) NOT NULL,
PRIMARY KEY(my_id(6))
) Engine=InnoDB charset utf8mb4 collate          


        
相关标签:
1条回答
  • 2021-01-28 02:33

    I am sorry to inform that it's not possible.

    According to MySql Documentation:

    Index prefixes on foreign key columns are not supported. One consequence of this is that BLOB and TEXT columns cannot be included in a foreign key because indexes on those columns must always include a prefix length.

    And MariaDB Documentation:

    The columns in the child table must be an index, or the leftmost part of an index. Index prefixes are not supported (thus, TEXT and BLOB columns cannot be used as foreign keys)

    If you really need alphanumeric keys, consider using something like char or varchar instead. The easiest, most generic, and most common approach is to use numeric keys, such as INT.

    0 讨论(0)
提交回复
热议问题