create table users (id int not null auto_increment
, username varchar(255) NOT NULL
, password varchar(255) NOT NULL
, active int NOT NULL
, PRIMARY KEY (id))
ENGINE=InnoDB COLLATE=utf8_unicode_ci;
create table athing (id int not null auto_increment
, name varchar(255) not null
, status varchar(255) not null
, created_by_user_id int
, PRIMARY KEY (id)
, CONSTRAINT athing_fk1 FOREIGN KEY (created_by_user_id) REFERENCES users (id)
) ENGINE=InnoDB COLLATE=utf8_unicode_ci;
我使用 HeidiSQL 修改了“ATHING”表中的 FK 以包含 ON DELETE SET NULL。
我的 USERS 表不见了!我能够把事情清理干净。我删除了所有指向 USERS 的 FK 并删除了该表。
当我尝试重新创建 USERS 表时,我收到此错误:
ERROR 1005 (HY000): Can't create table `sprintdb`.`system_users` (errno: 150 "Foreign key constraint is incorrectly formed")
某些表上仍然有支持这些键的索引。在查询INNODB_SYS_TABLES和INNODB_SYS_INDEXES表时发现那些我认为被删除的索引仍然存在于这些系统表中。
在使用第三方工具创建表然后约束现有表时,我多次收到此消息。这是两件事之一:
- The
int
列有不同的尺寸
- The
int
列有不同的标志(无 AUTO_INCRMENT)
例如,我使用一个工具创建了一个表,该工具以某种方式创建了一个列INT(10)
而不是预期的INT(11)
。虽然我只是选择了INT
当创建两者时,它被搞乱了 - 从来没有找到原因。
长话短说,通常最好明确说明INT
创建表时的大小。
对于您的情况,以下内容应该有效:
create table users (id int(11) not null auto_increment
, username varchar(255) NOT NULL
, password varchar(255) NOT NULL
, active int NOT NULL
, PRIMARY KEY (id))
ENGINE=InnoDB COLLATE=utf8_unicode_ci;
create table athing (id int(11) not null auto_increment
, name varchar(255) not null
, status varchar(255) not null
, created_by_user_id int(11) not null
, PRIMARY KEY (id)
, CONSTRAINT athing_fk1 FOREIGN KEY (created_by_user_id) REFERENCES users (id)
) ENGINE=InnoDB COLLATE=utf8_unicode_ci;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)