1

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)

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

thanx for your reply matthew, changing the constraint name worked for me. Constraint name can be anyname or a specific column name kind off?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.