简体   繁体   中英

What is the difference between many-to-many cross table and one-to-one cross table

Is there anything different between the two? All I see is the additional index which I don't believe fundamentally changes anything.

I would expect the first allows each t1_1 entity to be joined to multiple t1_2 entities, and the opposite as well.

For the second, I would expect each t2_1 entity to be joined to a maximum of one t2_2 entity.

But the resulting schema's generated by MySQL Workbench appear to be basically the same.

PS. Why I am doing this? Learning about super/sub tables, and went off on a tangent.

在此处输入图片说明

-- MySQL Script generated by MySQL Workbench
-- 08/05/15 08:12:21
-- Model: New Model    Version: 1.0
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

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

-- -----------------------------------------------------
-- Table `mydb`.`t1_1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1_1` (
  `id` INT NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`t1_2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1_2` (
  `id` INT NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`t1_1_has_t1_2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1_1_has_t1_2` (
  `t1_1_id` INT NOT NULL,
  `t1_2_id` INT NOT NULL,
  PRIMARY KEY (`t1_1_id`, `t1_2_id`),
  INDEX `fk_t1_1_has_t1_2_t1_21_idx` (`t1_2_id` ASC),
  INDEX `fk_t1_1_has_t1_2_t1_1_idx` (`t1_1_id` ASC),
  CONSTRAINT `fk_t1_1_has_t1_2_t1_1`
    FOREIGN KEY (`t1_1_id`)
    REFERENCES `mydb`.`t1_1` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_t1_1_has_t1_2_t1_21`
    FOREIGN KEY (`t1_2_id`)
    REFERENCES `mydb`.`t1_2` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`t2_1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t2_1` (
  `id` INT NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`t2_2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t2_2` (
  `id` INT NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`t2_1_hs_t2_2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t2_1_hs_t2_2` (
  `t2_1_id` INT NOT NULL,
  `t2_2_id` INT NOT NULL,
  PRIMARY KEY (`t2_1_id`, `t2_2_id`),
  INDEX `fk_t2_1_hs_t2_2_t2_21_idx` (`t2_2_id` ASC),
  CONSTRAINT `fk_t2_1_hs_t2_2_t2_11`
    FOREIGN KEY (`t2_1_id`)
    REFERENCES `mydb`.`t2_1` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_t2_1_hs_t2_2_t2_21`
    FOREIGN KEY (`t2_2_id`)
    REFERENCES `mydb`.`t2_2` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

IMO, talking in perspective of database design (not mysql workbench)
When using 1-n relation, we mean a not null foreign key inside cross table.
When using 1-1 relation, we mean a not null foreign key inside cross table that is the or part of a unique key of cross table.

Consider tables having these records:
T1_1(A1, B1, C1)
T2_1(A2, B2, C2)
In 1-n relation, we can have (A1, A2) multiple times in T1_has_T2 table
In 1-1 relation, we can not have (A1, A2) multiple times in T1_has_T2 table.

So Achieving 1-1 could be possible by putting foreign keys inside primary key or defining a unique key(index) on combination the foreign key columns.

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