ava*_*ske 20 mysql foreign-keys mysql-workbench mysql-error-1050
我有一个包含列的表CustomizationSet:
customization_set_guid (which is a non-nullable guid and also the primary key)
creator_account_guid
and a few others
Run Code Online (Sandbox Code Playgroud)
和包含现有数据的表注册列:
registration_id (an int and the primary key)
customization_set_guid (also a guid (so a char(36)) which is nullable, and all entries are currently null)
and a few other columns
Run Code Online (Sandbox Code Playgroud)
当我试着跑
ALTER TABLE Registration ADD FOREIGN KEY
(
customization_set_guid
) REFERENCES CustomizationSet (
customization_set_guid
);
Run Code Online (Sandbox Code Playgroud)
在MySQL Workbench中,它给出了错误1050Table'.\ dbname\registration'已经存在.
如果我尝试使用UI使用Alter Table对话框的Foreign Keys选项卡添加外键,并选择CustomizationSet作为引用表,则不允许我在列列表中选择customization_set_guid.
我真的不确定为什么它不会让我添加这个外键.我刚刚在我刚刚添加的表之间成功创建了外键.注册表已经存在了一段时间......
小智 9
我得到了同样的错误,这是因为外键已经存在.你想要的只是添加约束:
ALTER TABLE Registration
ADD CONSTRAINT idx_Registration_CustomizationSet
FOREIGN KEY (customization_set_guid)
REFERENCES CustomizationSet(customization_set_guid);
Run Code Online (Sandbox Code Playgroud)
所以一个团队成员想出了这个办法。其中一个表设置为 utf8_general 类型,另一个表设置为默认类型。我不认为这是一个问题,因为默认值是 utf8_general,但显然 mysql 只查看类型名称而不是底层类型。