Relationship of Tables - how to do

Asked

Viewed 156 times

1

I’m having trouble figuring out how to relate to tables. My scenario is below:

A parceiro may have several usuários, however a usuário may only be attached to one parceiro. A usuário can register numerous pedidos, and these pedidos will be hitched usuário who registered and parceiro user. Example: pedido to is attached to the usuário cesar in which this usuário is attached to the parceiro partner x.

To make a new pedido the code (id) of the parceiro and the details of the new application, such as: name, rg, Cpf and email

In this pedido there’s a chave de vpn in which it is unique and being formed by partner-Cpf. In a table vpnCode has the chave de vpn, bytes received, sent, server ip, real ip.

One VPN has chave de vpn, the parceiro that generated it, the cpf of pedido, the rg of pedido.

What I can’t understand (I’ve been trying for over a week to figure out how to do it) is how to make these relationships. Please, I need help.

  • The USER x PARTNER ratio is 1 to 1 or 1 for many?

  • Each USER may have only one PARTNER, but each PARTNER may have several USERS... That is, 1 to 1

  • I think you need a table of USERS, one of PARTNERS, one of CHAVES_VPN, one of USUARIO_PARCEIRO, one of PARCEIROS_USUARIOS and CHAVES_PARCEIROS.

1 answer

2


I think this architecture should solve your problem. I didn’t put all the VPN fields but you can do it yourself.

inserir a descrição da imagem aqui

If you need the code that generates these tables, see below:

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`PARCEIRO`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`PARCEIRO` (
  `pk_parceiro` INT NOT NULL AUTO_INCREMENT,
  `nome` VARCHAR(45) NULL,
  `cpf` VARCHAR(45) NULL,
  `RG` VARCHAR(45) NULL,
  `E-mail` VARCHAR(45) NULL,
  `outrosCampos` VARCHAR(45) NULL,
  PRIMARY KEY (`pk_parceiro`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`USER`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`USER` (
  `pk_user` INT NOT NULL AUTO_INCREMENT,
  `pk_father` INT NULL,
  `Name` VARCHAR(45) NULL,
  `outrosCampos` VARCHAR(45) NULL,
  PRIMARY KEY (`pk_user`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`USER_PARCEIROS`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`USER_PARCEIROS` (
  `fk_user` INT NOT NULL,
  `fk_parceiro` INT NOT NULL,
  `outrosCampos` VARCHAR(45) NULL,
  PRIMARY KEY (`fk_user`, `fk_parceiro`),
  INDEX `fkp_idx` (`fk_parceiro` ASC),
  CONSTRAINT `fku`
    FOREIGN KEY (`fk_user`)
    REFERENCES `mydb`.`USER` (`pk_user`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fkp`
    FOREIGN KEY (`fk_parceiro`)
    REFERENCES `mydb`.`PARCEIRO` (`pk_parceiro`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`vpnCode_Parc`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`vpnCode_Parc` (
  `pk_vpnCOde` INT NOT NULL AUTO_INCREMENT,
  `vpnCode` VARCHAR(450) NOT NULL,
  `fkParceiro` INT NOT NULL,
  `outrosCampos` VARCHAR(45) NULL,
  PRIMARY KEY (`pk_vpnCOde`),
  INDEX `fkkpar_idx` (`fkParceiro` ASC),
  CONSTRAINT `fkkpar`
    FOREIGN KEY (`fkParceiro`)
    REFERENCES `mydb`.`PARCEIRO` (`pk_parceiro`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

If a user can only have one partner and this can be associated with multiple users then the model looks like this:

inserir a descrição da imagem aqui

  • Zwitterion was confused about user_partner and user

  • If I understand correctly, 1 user can have at least 1 partner and 1 partner must have at least 1 correct user? So it’s a many-to-many relationship with an intermediate table. Making a composite Primary key ensures that there will be only one user and partner input.

  • Note: the user pk_father column is optional. With it you can perform a hierarchy analysis.

  • zwitterion, according to the OP in the comments, each user can have only 1 partner, but each partner can have several. The relationship would be, in case, 1:n. @Cesara.Fonseca, if so, edit the question and add this information so that the answers are in agreement as well.

  • For example: Usuários: a, b, c || Parceiro: x, y || Usuário a só pode estar em x ou y neste exemplo, e parceiro x ou y pode conter a, b e c

  • I edited the question to get better to understand

  • It is still a bit confusing. Is there a possibility that a user does not have a partner? I made the edition based on the set where all users necessarily have a partner.

  • Zwi my doubt has been solved, thank you man. Could you give me a single example of how I would use Join to query a Partner and show all its users?

  • It depends on how you use the partner. By pk_partner or by name. The most common would be to use a selectBox, so vc will retrieve the partner pk via jquery and send to the following sqlquery: select U.Name, P.nome,P.cpf,P.RG,P.E-mail from user as U
inner join PARCEIRO as P on (U.fk_Parceiro=P.pk_parceiro)
where U.fk_Parceiro = jsFkParceiro; Two suggestions: Change this table npme that I used. Pass to user. And change the name of the Email field to E_mail.

  • Would zwitterion be anywhere else I could talk to you? I wanted to ask some questions, and if I continue to run this place or create another topic for a similar question will pollute a lot...

  • 1

    @Cesara.Fonseca the answer was very specific, it does not cost to give +1 (in addition to mark as solution), so you help those who helped you ;)

  • send it to my email. [email protected]. I reply when I have free time.

  • 1

    For me to score as +1 I need to have more reputation than I have... @zwitterion thanks man!

Show 8 more comments

Browser other questions tagged

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