我能';是否不添加外键约束?Mysql

我能';是否不添加外键约束?Mysql,mysql,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)

我无法创建第二个表,因为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;

我发现了错误,您希望获取外键“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;
我运行以下脚本:

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

没有索引、约束和外键?如果您还没有任何表,请先运行“为客户帐户创建表”,然后运行bugs\u elt脚本。如果您的表已经运行ALTER TABLE bugs\u elt ADD CONSTRAINT
fk\u bugs\u etl\u customer\u accounts
外键(
customerAccountServer
)引用
tsmdb\u集中式
customerAccountServer
)@DiegoParraIt打印:“错误代码:1822。未能添加外键约束。引用表“customer_accounts”中缺少约束“fk_bugs_etl_customer_accounts_idx”的索引相同的错误:(…是否有任何问题,因为我使用的第一个表有两个主键,而我只想使用一个?不,没有问题,您可以将脚本和错误放在打印屏幕上吗?@DiegoParra