我不能添加外键约束? Mysql的

时间:2015-03-12 14:57:43

标签: mysql

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

有两张桌子:

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;

2 个答案:

答案 0 :(得分:0)

我发现错误,你想获得一个外键' CustomerAccountServer' varchar(50),您只能使用引用唯一字段的外键。修改customer_accounts表,以便customeraccountServer字段是唯一的。

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;

答案 1 :(得分:0)

我运行此脚本:

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');

然后我可以得到:

select * from customer_accounts
join bugs_etl 
on customer_accounts.customerAccountServer = bugs_etl.customerAccountServer

result