mysql - InnoDB:更改 VARCHAR 主键的长度

标签 mysql foreign-keys innodb varchar

我在 InnoDB 中有一个表,它使用基于 VARCHAR 的外键:

CREATE TABLE `portal_equity` (
   `isin` varchar(12) NOT NULL,
    ....,
    PRIMARY KEY (`isin`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

另一个表对该表有外键约束:

CONSTRAINT `equity_id_refs_isin_2581bb1d` FOREIGN KEY (`equity_id`) REFERENCES `portal_equity` (`isin`)

我需要更改主键 VARCHAR 列的长度,所以我做到了。现在,每次我想引用“portal_equity”表中 isin 超过原始长度 12 的行时,我都会不断收到以下完整性错误。

Foreign key constraint fails for table investtor.portal_equitypastvalue: CONSTRAINT equity_id_refs_isin_7eed44e7 FOREIGN KEY (equity_id) REFERENCES portal_equity (isin) Trying to add in child table, in index portal_equitypastvalue_equity_id_6e7526e1 tuple: DATA TUPLE: 3 fields; 0: len 12; hex 304c55303332333133343030; asc 0LU032313400;; 1: len 3; hex 8fb578; asc x;; 2: len 4; hex 8001398a; asc 9 ;;

But in parent table investtor.portal_equity, in index PRIMARY, the closest match we can find is record: PHYSICAL RECORD: n_fields 9; compact format; info bits 0 0: len 13; hex 304c5530333233313334303036; asc 0LU0323134006;; 1: len 6; hex 000000005139; asc Q9;; 2: len 7; hex 800000002d0110; asc - ;; 3: len 4; hex 61736466; asc asdf;; 4: len 4; hex 80000050; asc P;; 5: len 3; hex 736466; asc sdf;; 6: len 4; hex 80000001; asc ;; 7: len 2; hex 4144; asc AD;; 8: len 0; hex ; asc ;;

我需要重新生成所有外键约束吗?这似乎有点过分了。此行为/错误是否记录在某处?

最佳答案

Do I need to regenerate all foreignkey constraints?

是的。

That seems rather excessive.

如果您更改主键字段的数据类型,在我看来,外键字段(及其各自的索引和约束)的数据类型也需要更改。

尽管 documentation声明“字符串类型的长度不必相同”,逻辑上讲外键字段的长度至少应与您要在其中存储的任何数据一样长。

祝你好运!

关于mysql - InnoDB:更改 VARCHAR 主键的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4271147/

相关文章:

java - ORMlite + MySQL 外键绑定(bind)

MySQL 从 google LineChart 的 TIMESTAMP 列中获取日期的百分比

php - 加入来自不同数据库的两个不同表

mysql - MVCC 行锁定与教科书事务行为

用于紧固选择和插入查询的 MySQL 数据库配置

php - 计算关联数组中的唯一值

sql - PostgreSQL 中的外键匹配

MongoDB 使用外键将字段复制到另一个集合

database - 外键命名方案

mysql - MySQL 5.5:以下哪一项是innodb中text/varchar字段的更好存储?