0

I have an Account table and a Customer table. An account has many customers. The name of the customer is unique across the account it references. Meaning, two customers belonging to the same account should have a unique name. How do I apply this constraint? following is the mysql table structure.

Thanks.

CREATE TABLE IF NOT EXISTS `sample`.`Account` (
  `id` BIGINT NOT NULL AUTO_INCREMENT,
  `email` VARCHAR(45) NOT NULL,
  `password` VARCHAR(45) NULL,
  `created_date` DATE NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `email_UNIQUE` (`email` ASC))
ENGINE = InnoDB
AUTO_INCREMENT = 0;

CREATE TABLE IF NOT EXISTS `sample`.`Customer` (
 `id` BIGINT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`),
  INDEX `account_fk_id_idx` (`account_id` ASC),
  CONSTRAINT `account_fk_id`
    FOREIGN KEY (`account_id`)
    REFERENCES `sample`.`Account` (`id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 0;
1
  • Define named account groups and use the group id in the customer table as one of foreign keys. Jun 28, 2014 at 21:19

1 Answer 1

0

I think you want:

unique (account_id, name)

In the Customer table.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.