将外键添加到现有表

Add Foreign Key to existing table(将外键添加到现有表)
本文介绍了将外键添加到现有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向名为katalog"的表添加外键.

ALTER TABLE 目录添加约束`fk_katalog_sprache`外键(`Sprache`)参考`Sprache`(`ID`)删除集 NULLON UPDATE SET NULL;

当我尝试这样做时,我收到此错误消息:

<块引用>

错误代码:1005.无法创建表 'mytable.#sql-7fb1_7d3a' (errno: 150)

INNODB 状态错误:

<块引用>

120405 14:02:57 表的外键约束错误mytable.#sql-7fb1_7d3a:

外键(`Sprache`)参考`Sprache`(`ID`)删除集 NULLON UPDATE SET NULL:无法解析表名接近:(`身份证`)删除集 NULLON UPDATE SET NULL

当我使用此查询时,它可以工作,但删除时"操作错误:

ALTER TABLE `katalog`添加外键(`Sprache`)引用`sprache`(`ID`)

两个表都是 InnoDB 并且两个字段都是INT(11) not null".我正在使用 MySQL 5.1.61.尝试在 MacBook Pro 上使用 MySQL Workbench(最新)触发此 ALTER 查询.

表创建语句:

创建表`katalog`(`ID` int(11) 无符号 NOT NULL AUTO_INCREMENT,`Name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,`AnzahlSeiten` int(4) 无符号非空,`Sprache` int(11) 非空,主键(`ID`),唯一键 `katalogname_uq`(`Name`)) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC$$创建表`sprache`(`ID` int(11) NOT NULL AUTO_INCREMENT,`Bezeichnung` varchar(45) 不为空,主键(`ID`),唯一键`Bezeichnung_UNIQUE`(`Bezeichnung`),KEY`ix_sprache_id`(`ID`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

解决方案

要将外键 (grade_id) 添加到现有表 (users),请按照以下步骤操作:

ALTER TABLE users ADD grade_id SMALLINT UNSIGNED NOT NULL DEFAULT 0;ALTER TABLE 用户 ADD CONSTRAINT fk_grade_id FOREIGN KEY (grade_id) REFERENCES grades(id);

I want to add a Foreign Key to a table called "katalog".

ALTER TABLE katalog 
ADD CONSTRAINT `fk_katalog_sprache` 
FOREIGN KEY (`Sprache`)
REFERENCES `Sprache` (`ID`)
ON DELETE SET NULL
ON UPDATE SET NULL;

When I try to do this, I get this error message:

Error Code: 1005. Can't create table 'mytable.#sql-7fb1_7d3a' (errno: 150)

Error in INNODB Status:

120405 14:02:57 Error in foreign key constraint of table mytable.#sql-7fb1_7d3a:

FOREIGN KEY (`Sprache`)
REFERENCES `Sprache` (`ID`)
ON DELETE SET NULL
ON UPDATE SET NULL:
Cannot resolve table name close to:
(`ID`)
ON DELETE SET NULL
ON UPDATE SET NULL

When i use this query it works, but with wrong "on delete" action:

ALTER TABLE `katalog` 
ADD FOREIGN KEY (`Sprache` ) REFERENCES `sprache` (`ID` )

Both tables are InnoDB and both fields are "INT(11) not null". I'm using MySQL 5.1.61. Trying to fire this ALTER Query with MySQL Workbench (newest) on a MacBook Pro.

Table Create Statements:

CREATE TABLE `katalog` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`AnzahlSeiten` int(4) unsigned NOT NULL,
`Sprache` int(11) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `katalogname_uq` (`Name`)
 ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC$$

CREATE TABLE `sprache` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
 `Bezeichnung` varchar(45) NOT NULL,
 PRIMARY KEY (`ID`),
 UNIQUE KEY `Bezeichnung_UNIQUE` (`Bezeichnung`),
KEY `ix_sprache_id` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

解决方案

To add a foreign key (grade_id) to an existing table (users), follow the following steps:

ALTER TABLE users ADD grade_id SMALLINT UNSIGNED NOT NULL DEFAULT 0;
ALTER TABLE users ADD CONSTRAINT fk_grade_id FOREIGN KEY (grade_id) REFERENCES grades(id);

这篇关于将外键添加到现有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)