所以像这样的东西?这是 mysql,如果您还没有使用它,请下载 mysql 工作台并对其进行修改。它让您深入了解 SQL 的工作原理。我过去常常手动编写这些东西,但可视化它真的很有帮助。
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';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Sandwiches`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Sandwiches` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Sandwiches` (
`SandwichName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`SandwichName`),
UNIQUE INDEX `SandwichName_UNIQUE` (`SandwichName` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Chefs`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Chefs` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Chefs` (
`ChefID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NOT NULL,
`Age` INT UNSIGNED NOT NULL,
`SignatureSandwich` VARCHAR(45) NOT NULL,
PRIMARY KEY (`ChefID`, `SignatureSandwich`),
INDEX `fk_Chefs_Sandwiches_idx` (`SignatureSandwich` ASC),
CONSTRAINT `fk_SignatureSandwich`
FOREIGN KEY (`SignatureSandwich`)
REFERENCES `mydb`.`Sandwiches` (`SandwichName`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`SandwichLikes`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`SandwichLikes` ;
CREATE TABLE IF NOT EXISTS `mydb`.`SandwichLikes` (
`SandwichThatIsLiked` VARCHAR(45) NOT NULL,
`ChefThatLikes` INT UNSIGNED NOT NULL,
PRIMARY KEY (`SandwichThatIsLiked`, `ChefThatLikes`),
INDEX `fk_Sandwiches_has_Chefs_Chefs1_idx` (`ChefThatLikes` ASC),
INDEX `fk_Sandwiches_has_Chefs_Sandwiches1_idx` (`SandwichThatIsLiked` ASC),
CONSTRAINT `fk_SandwichThatIsLiked`
FOREIGN KEY (`SandwichThatIsLiked`)
REFERENCES `mydb`.`Sandwiches` (`SandwichName`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ChefThatLikes`
FOREIGN KEY (`ChefThatLikes`)
REFERENCES `mydb`.`Chefs` (`ChefID`)
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;