MySQL建表错误

发布于 2024-12-23 12:01:38 字数 987 浏览 0 评论 0原文

我有两个名为

MEMBER 的表 - 列 id(主键)、姓名、电子邮件

主题 - 列id、topic_type、created_by

我想创建一个新表 MEMBER_TO_TOPICS 将成员映射到主题,其中包含列 memberid(成员表 id 的外键)、topicid( 主题表 ID 的外键),created_time
这是我尝试执行的查询。

CREATE TABLE `gsraisin`.`member_to_topics` (
`member_id` VARCHAR(50) NOT NULL,
`topic_id` VARCHAR(50) NOT NULL,
`created_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`member_id`, `topic_id`),
CONSTRAINT `FK_member_to_topics_memberid` FOREIGN KEY `FK_member_to_topics_memberid`   
(`member_id`)
REFERENCES `member` (`id`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `FK_member_to_topics_topicid` FOREIGN KEY `FK_member_to_topics_topicid` 
(`topic_id`)
REFERENCES `topics` (`id`)
ON DELETE CASCADE
ON UPDATE NO ACTION
)
ENGINE = InnoDB;

但执行时出现以下错误 - MYSQL 错误号 1005 can't Create table member_to_topics (errno:121)

I have two tables named

MEMBER - columns id(primary key), name, email &

TOPICS - columns id, topic_type, created_by.

I want to create a new table MEMBER_TO_TOPICS which maps member to topics, which has columns memberid(foreign key of member table id), topicid(foreign key of topic table id), created_time.
Here is the query am trying to execute .

CREATE TABLE `gsraisin`.`member_to_topics` (
`member_id` VARCHAR(50) NOT NULL,
`topic_id` VARCHAR(50) NOT NULL,
`created_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`member_id`, `topic_id`),
CONSTRAINT `FK_member_to_topics_memberid` FOREIGN KEY `FK_member_to_topics_memberid`   
(`member_id`)
REFERENCES `member` (`id`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `FK_member_to_topics_topicid` FOREIGN KEY `FK_member_to_topics_topicid` 
(`topic_id`)
REFERENCES `topics` (`id`)
ON DELETE CASCADE
ON UPDATE NO ACTION
)
ENGINE = InnoDB;

But getting the following error while executing - MYSQL Error Number 1005 can't Create table member_to_topics (errno:121)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-12-30 12:01:39

我在本地机器上尝试过,效果很好。

mysql> show create table member;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                       |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member | CREATE TABLE `member` (
  `id` varchar(50) NOT NULL DEFAULT '',
  `name` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


mysql> show create table topics;
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                  |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| topics | CREATE TABLE `topics` (
  `id` varchar(50) NOT NULL DEFAULT '',
  `topic_type` varchar(50) DEFAULT NULL,
  `created_by` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table member_to_topics;
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member_to_topics | CREATE TABLE `member_to_topics` (
  `member_id` varchar(50) NOT NULL,
  `topic_id` varchar(50) NOT NULL,
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`member_id`,`topic_id`),
  KEY `FK_member_to_topics_topicid` (`topic_id`),
  CONSTRAINT `FK_member_to_topics_memberid` FOREIGN KEY (`member_id`) REFERENCES `member` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `FK_member_to_topics_topicid` FOREIGN KEY (`topic_id`) REFERENCES `topics` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

错误代码 121 表示密钥重复。

[matthewh@kookaburra ~]$ perror 121
OS error code 121:  Remote I/O error
MySQL error code 121: Duplicate key on write or update

我怀疑您可能有重复的约束名称?

I tried this on my local machine and it worked fine.

mysql> show create table member;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                       |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member | CREATE TABLE `member` (
  `id` varchar(50) NOT NULL DEFAULT '',
  `name` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


mysql> show create table topics;
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                  |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| topics | CREATE TABLE `topics` (
  `id` varchar(50) NOT NULL DEFAULT '',
  `topic_type` varchar(50) DEFAULT NULL,
  `created_by` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table member_to_topics;
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member_to_topics | CREATE TABLE `member_to_topics` (
  `member_id` varchar(50) NOT NULL,
  `topic_id` varchar(50) NOT NULL,
  `created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`member_id`,`topic_id`),
  KEY `FK_member_to_topics_topicid` (`topic_id`),
  CONSTRAINT `FK_member_to_topics_memberid` FOREIGN KEY (`member_id`) REFERENCES `member` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  CONSTRAINT `FK_member_to_topics_topicid` FOREIGN KEY (`topic_id`) REFERENCES `topics` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Error code 121 is for Duplicate Key.

[matthewh@kookaburra ~]$ perror 121
OS error code 121:  Remote I/O error
MySQL error code 121: Duplicate key on write or update

I suspect you may have a duplicated constraint name perhaps?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文