首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能添加外键约束吗?Mysql

我不能添加外键约束吗?Mysql
EN

Stack Overflow用户
提问于 2015-03-12 14:57:43
回答 2查看 54关注 0票数 0

我无法创建第二个表,因为Mysql输出了错误代码12 15的消息,但我不明白脚本中的问题是什么。

这是我的两张桌子:

代码语言:javascript
复制
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`customer_accounts` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `customerAccountName` VARCHAR(50) NOT NULL,
  `customerAccountUser` VARCHAR(50) NOT NULL,
  `customerAccountServer` VARCHAR(45) NOT NULL,
  `password` VARCHAR(20) NOT NULL,
  `status` TINYINT(50) NOT NULL,
  PRIMARY KEY (`id`, `customerAccountServer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


-- -----------------------------------------------------
-- Table `tsmdb_centralized`.`bugs_etl`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`bugs_etl` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `bug_title` VARCHAR(45) NOT NULL,
  `bug_description` VARCHAR(500) NULL,
  `customerAccountServer` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `fk_bugs_etl_customer_accounts_idx` (`customerAccountServer` ASC),
  CONSTRAINT `fk_bugs_etl_customer_accounts`
    FOREIGN KEY (`customerAccountServer`)
    REFERENCES `tsmdb_centralized`.`customer_accounts` (`customerAccountServer`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-12 15:04:43

我发现了错误,您希望获得一个外键'CustomerAccountServer‘varchar(50),并且只能有一个引用唯一字段的外键。修改customer_accounts表,使customeraccountServer字段是唯一的。

代码语言:javascript
复制
CREATE TABLE IF NOT EXISTS `tsmdb_centralized`.`customer_accounts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`customerAccountName` VARCHAR(50) NOT NULL,
`customerAccountUser` VARCHAR(50) NOT NULL,
`customerAccountServer` VARCHAR(45) NOT NULL,
`password` VARCHAR(20) NOT NULL,
`status` TINYINT(50) NOT NULL,
PRIMARY KEY (`id`, `customerAccountServer`),
UNIQUE KEY `customerAccountServer_UNIQUE` (`customerAccountServer`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
票数 0
EN

Stack Overflow用户

发布于 2015-03-12 18:28:06

我运行这个脚本:

代码语言:javascript
复制
CREATE TABLE customer_accounts (
  id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  customerAccountName VARCHAR(50) NOT NULL,
  customerAccountUser VARCHAR(50) NOT NULL,
  customerAccountServer VARCHAR(45) NOT NULL,
  password VARCHAR(20) NOT NULL,
  status TINYINT(50) NOT NULL,
  UNIQUE KEY Cat_customerAccountServer (customerAccountServer)
)
  ENGINE = InnoDB
  DEFAULT CHARACTER SET = utf8;


CREATE TABLE bugs_etl (
   id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
   bug_title VARCHAR(45) NOT NULL,
   bug_description VARCHAR(500) NULL,
   customerAccountServer VARCHAR(45) NOT NULL,
   INDEX fk_bugs_etl_customer_accounts_idx(customerAccountServer ASC),
   FOREIGN KEY fk_cat(customerAccountServer)
   REFERENCES customer_accounts(customerAccountServer)
   ON UPDATE NO ACTION
   ON DELETE NO ACTION
)
  ENGINE = InnoDB
  DEFAULT CHARACTER SET = utf8;


insert into customer_accounts (customerAccountName,customerAccountUser,
                              customerAccountServer,password,status)
values('nuevo','nuevo','nuevo','1234',1);

insert into customer_accounts (customerAccountName,customerAccountUser,
                              customerAccountServer,password,status)
values('nuevo','nuevo','nuevo2','1234',1);

insert into bugs_etl (bug_title,bug_description,
                              customerAccountServer)
values('nuevo','nuevo','nuevo2');

然后我就能得到:

代码语言:javascript
复制
select * from customer_accounts
join bugs_etl 
on customer_accounts.customerAccountServer = bugs_etl.customerAccountServer

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29013142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档