简体   繁体   中英

I can't add a foreign key constraint? Mysql

I cannot create the second table because Mysql prints out the message with Error Code 12 15, but i do not understand what is the problem in the script..

There are my two tables:

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;

i found the error, you want to get a foreign key 'CustomerAccountServer' varchar(50) and you can only have a foreign key referencing a unique field. Modify your customer_accounts table so that the customeraccountServer field is unique.

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;

I run this Script:

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

then i can get:

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

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM