Problems with Table Modeling with Two Foreign Keys

Asked

Viewed 65 times

0

I need to finish a modeling of a database and I’m at an impasse.

Simply put, I have 3 tables as follows:

PROMISSORY NOTES

CREATE TABLE IF NOT EXISTS `picinin`.`promissorias` (
  `id_promissoria` INT NOT NULL AUTO_INCREMENT,
  `emissao` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `vencimento` DATETIME NOT NULL,
  `numeracao` INT NOT NULL,
  `valor` DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (`id_promissoria`))
ENGINE = InnoDB

CHECKS

CREATE TABLE IF NOT EXISTS `picinin`.`cheques` (
  `id_cheque` INT NOT NULL AUTO_INCREMENT,
  `banco` INT NULL,
  `agencia` INT NULL,
  `conta` INT NULL,
  `numero` INT NULL,
  `valor` DECIMAL(10,2) NULL,
  `emissao` DATETIME NULL,
  `vencimento` DATETIME NULL,
  `status` CHAR(1) NULL DEFAULT 'e',
  PRIMARY KEY (`id_cheque`))
ENGINE = InnoDB

MOVEMENTS

CREATE TABLE IF NOT EXISTS `picinin`.`movimentacoes` (
  `id_movimentacao` INT NOT NULL AUTO_INCREMENT,
  `data` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `valor` DECIMAL(10,2) NOT NULL,
  `id_promissoria` INT NOT NULL,
  `id_cheque` INT NOT NULL,
  PRIMARY KEY (`id_movimentacao`),
  INDEX `fk_movimentacoes_promissorias1_idx` (`id_promissoria` ASC),
  INDEX `fk_movimentacoes_cheques1_idx` (`id_cheque` ASC),
  CONSTRAINT `fk_movimentacoes_promissorias1`
    FOREIGN KEY (`id_promissoria`)
    REFERENCES `picinin`.`promissorias` (`id_promissoria`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_movimentacoes_cheques1`
    FOREIGN KEY (`id_cheque`)
    REFERENCES `picinin`.`cheques` (`id_cheque`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

In these tables are inserted the open payments of a customer. If the sale is made for payment in 10 promissory notes, enter 10 records in the table PROMISSORY NOTE. If checks are chosen as a form of payment, we register the 10 checks issued by the customer in the table CHECKS.

In the third table MOVEMENTS, we control the payments, be they of promissory notes or checks, but I’m having trouble creating the relationships of keys to ensure integrity, IE, will only be downloaded a title in the table MOVEMENTS if there is a relationship in the table PROMISSORY NOTE or in the CHECKS.

Visually the modeling is as follows:

inserir a descrição da imagem aqui

We know that in practice this does not work, because if I go to discharge a promissory note, the system will ask for the information of a cheque and vice versa.

How do I fix it?

  • 1

    Give a study on partitioning. Partition the entity movimentacoes in cheque and promissory movements and make the appropriate relationship of each of them.

  • You talk to create a table that will keep the checks payments and another to keep the promissory notes payments?

  • 1

    Movements----<movements>----checks because a cheque can pay one or more promises , and a promising one can be paid for more than one cheque. But I’m not sure I understand the problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.