数据库——建立ER模型及关系模型转换
2023-12-31 23:41:18
?
【实验内容及要求】
- 使用画图工具或MySQL Workbench等建模工具设计出相应的ER图,将局部ER图合并为一个整体ER模型,在ER模型中填加多样性约束,建立显示主键的ER模型,标识实体的属性,确认主键、外键。
- 将上述ER图转化为关系模式,要求标出每个关系的主码和外码
- 依据创建的模型,生成SQL语句,在MySQL里创建数据库和表
【实验平台】
Windows MySQL + MySQL Workbench
MySQL Workbench: MySQL
Workbench是为MySQL设计的ER/数据库建模工具。是著名的数据库设计工具DBDesigner4的继任者。具有设计和创建新的数据库图示,建立数据库文档,以及进行复杂的MySQL
迁移的作用
【实验步骤】
-
新建模型
-
为表添加字段和完整性约束
-
导出SQL语句
-
执行SQL语句,产生数据库和表
【实验结果】
- ER模型
- SQL文件
?
-- MySQL Script generated by MySQL Workbench
-- Mon May 23 15:15:50 2022
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Customer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Customer` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Customer` (
`customerNo` INT NOT NULL,
`customerName` VARCHAR(45) NULL,
`customerStreet` VARCHAR(45) NULL,
`customerCity` VARCHAR(45) NULL,
`customerState` VARCHAR(45) NULL,
`customerZipCode` VARCHAR(45) NULL,
`custTelNo` VARCHAR(45) NULL,
`custFaxNo` VARCHAR(45) NULL,
`DOB` VARCHAR(45) NULL,
`maritalStatus` VARCHAR(45) NULL,
`creditRating` VARCHAR(45) NULL,
PRIMARY KEY (`customerNo`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Employee` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Employee` (
`employeeNo` INT NOT NULL,
`title` VARCHAR(45) NULL,
`firstName` VARCHAR(45) NULL,
`middleName` VARCHAR(45) NULL,
`lastName` VARCHAR(45) NULL,
`address` VARCHAR(45) NULL,
`workTelExt` VARCHAR(45) NULL,
`homeTelNo` VARCHAR(45) NULL,
`empEmailAddress` VARCHAR(45) NULL,
`socialSecurityNumber` VARCHAR(45) NULL,
`DOB` VARCHAR(45) NULL,
`position` VARCHAR(45) NULL,
`sex` VARCHAR(45) NULL,
`salary` VARCHAR(45) NULL,
`dateStarted` VARCHAR(45) NULL,
PRIMARY KEY (`employeeNo`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`PaymentMethod`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`PaymentMethod` ;
CREATE TABLE IF NOT EXISTS `mydb`.`PaymentMethod` (
`pMethodNo` INT NOT NULL,
`paymentMethod` VARCHAR(45) NULL,
PRIMARY KEY (`pMethodNo`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Order`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Order` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Order` (
`orderNo` INT NOT NULL,
`orderDate` VARCHAR(45) NULL,
`billingStreet` VARCHAR(45) NULL,
`billingCity` VARCHAR(45) NULL,
`billingState` VARCHAR(45) NULL,
`billingZipCode` VARCHAR(45) NULL,
`promisedDate` VARCHAR(45) NULL,
`status` VARCHAR(45) NULL,
`customerNo` VARCHAR(45) NULL,
`employeeNo` VARCHAR(45) NULL,
`Invoice_InvoiceNo` INT NOT NULL,
`Invoice_PaymentMethod_pMethodNo` INT NOT NULL,
`Customer_customerNo` INT NOT NULL,
`Employee_employeeNo` INT NOT NULL,
PRIMARY KEY (`orderNo`, `Invoice_InvoiceNo`, `Invoice_PaymentMethod_pMethodNo`, `Customer_customerNo`),
INDEX `fk_Order_Customer1_idx` (`Customer_customerNo` ASC) VISIBLE,
INDEX `fk_Order_Employee1_idx` (`Employee_employeeNo` ASC) VISIBLE,
CONSTRAINT `fk_Order_Invoice1`
FOREIGN KEY ()
REFERENCES `mydb`.`Invoice` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Order_Customer1`
FOREIGN KEY (`Customer_customerNo`)
REFERENCES `mydb`.`Customer` (`customerNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Order_Employee1`
FOREIGN KEY (`Employee_employeeNo`)
REFERENCES `mydb`.`Employee` (`employeeNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Invoice`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Invoice` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Invoice` (
`InvoiceNo` INT NOT NULL,
`dateRaised` VARCHAR(45) NULL,
`datePaid` VARCHAR(45) NULL,
`creditCardNo` VARCHAR(45) NULL,
`hodlersName` VARCHAR(45) NULL,
`expiryDate` VARCHAR(45) NULL,
`orderNo` INT NULL,
`pMethodNo` VARCHAR(45) NULL,
`PaymentMethod_pMethodNo` INT NOT NULL,
PRIMARY KEY (`InvoiceNo`, `PaymentMethod_pMethodNo`),
INDEX `fk_Invoice_PaymentMethod_idx` (`PaymentMethod_pMethodNo` ASC) VISIBLE,
CONSTRAINT `fk_Invoice_PaymentMethod`
FOREIGN KEY (`PaymentMethod_pMethodNo`)
REFERENCES `mydb`.`PaymentMethod` (`pMethodNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `orderNo`
FOREIGN KEY ()
REFERENCES `mydb`.`Order` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Product`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Product` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Product` (
`productNo` INT NOT NULL,
`productName` VARCHAR(45) NULL,
`serialNo` VARCHAR(45) NULL,
`unitPrice` VARCHAR(45) NULL,
`quantityOnHand` VARCHAR(45) NULL,
`reorderLevel` VARCHAR(45) NULL,
`reorderQuantity` VARCHAR(45) NULL,
`reorderLeadTime` VARCHAR(45) NULL,
PRIMARY KEY (`productNo`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`OrderDetail`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`OrderDetail` ;
CREATE TABLE IF NOT EXISTS `mydb`.`OrderDetail` (
`orderNo` INT NOT NULL,
`productNo` VARCHAR(45) NULL,
`quantityOrdered` VARCHAR(45) NULL,
`Product_productNo` INT NOT NULL,
`Order_orderNo` INT NOT NULL,
`Order_Invoice_InvoiceNo` INT NOT NULL,
`Order_Invoice_PaymentMethod_pMethodNo` INT NOT NULL,
`Order_Customer_customerNo` INT NOT NULL,
PRIMARY KEY (`orderNo`, `Product_productNo`, `Order_orderNo`, `Order_Invoice_InvoiceNo`, `Order_Invoice_PaymentMethod_pMethodNo`, `Order_Customer_customerNo`),
INDEX `fk_OrderDetail_Product1_idx` (`Product_productNo` ASC) VISIBLE,
INDEX `fk_OrderDetail_Order1_idx` (`Order_orderNo` ASC, `Order_Invoice_InvoiceNo` ASC, `Order_Invoice_PaymentMethod_pMethodNo` ASC, `Order_Customer_customerNo` ASC) VISIBLE,
CONSTRAINT `fk_OrderDetail_Product1`
FOREIGN KEY (`Product_productNo`)
REFERENCES `mydb`.`Product` (`productNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_OrderDetail_Order1`
FOREIGN KEY (`Order_orderNo` , `Order_Invoice_InvoiceNo` , `Order_Invoice_PaymentMethod_pMethodNo` , `Order_Customer_customerNo`)
REFERENCES `mydb`.`Order` (`orderNo` , `Invoice_InvoiceNo` , `Invoice_PaymentMethod_pMethodNo` , `Customer_customerNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`ShipmentMethod`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`ShipmentMethod` ;
CREATE TABLE IF NOT EXISTS `mydb`.`ShipmentMethod` (
`sMethodNo` INT NOT NULL,
`shipmentMethod` VARCHAR(45) NULL,
PRIMARY KEY (`sMethodNo`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Shipment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Shipment` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Shipment` (
`shipmentNo` INT NOT NULL,
`quantity` VARCHAR(45) NULL,
`shipmentDate` VARCHAR(45) NULL,
`completeStatus` VARCHAR(45) NULL,
`orderNo` INT NULL,
`productNo` VARCHAR(45) NULL,
`employeeNo` INT NULL,
`sMethodNo` INT NULL,
`OrderDetail_orderNo` INT NOT NULL,
`OrderDetail_Product_productNo` INT NOT NULL,
`OrderDetail_Order_orderNo` INT NOT NULL,
`OrderDetail_Order_Invoice_InvoiceNo` INT NOT NULL,
`OrderDetail_Order_Invoice_PaymentMethod_pMethodNo` INT NOT NULL,
`OrderDetail_Order_Customer_customerNo` INT NOT NULL,
`ShipmentMethod_sMethodNo` INT NOT NULL,
`Employee_employeeNo` INT NOT NULL,
PRIMARY KEY (`shipmentNo`, `OrderDetail_orderNo`, `OrderDetail_Product_productNo`, `OrderDetail_Order_orderNo`, `OrderDetail_Order_Invoice_InvoiceNo`, `OrderDetail_Order_Invoice_PaymentMethod_pMethodNo`, `OrderDetail_Order_Customer_customerNo`, `ShipmentMethod_sMethodNo`),
INDEX `fk_Shipment_OrderDetail1_idx` (`OrderDetail_orderNo` ASC, `OrderDetail_Product_productNo` ASC, `OrderDetail_Order_orderNo` ASC, `OrderDetail_Order_Invoice_InvoiceNo` ASC, `OrderDetail_Order_Invoice_PaymentMethod_pMethodNo` ASC, `OrderDetail_Order_Customer_customerNo` ASC, `orderNo` ASC) VISIBLE,
INDEX `fk_Shipment_ShipmentMethod1_idx` (`ShipmentMethod_sMethodNo` ASC, `sMethodNo` ASC, `OrderDetail_orderNo` ASC, `OrderDetail_Product_productNo` ASC, `OrderDetail_Order_orderNo` ASC, `OrderDetail_Order_Invoice_InvoiceNo` ASC, `OrderDetail_Order_Invoice_PaymentMethod_pMethodNo` ASC, `OrderDetail_Order_Customer_customerNo` ASC) VISIBLE,
INDEX `fk_Shipment_Employee1_idx` (`Employee_employeeNo` ASC) VISIBLE,
INDEX `orderNo_idx` (`orderNo` ASC) VISIBLE,
CONSTRAINT `orderNo`
FOREIGN KEY (`OrderDetail_orderNo` , `OrderDetail_Product_productNo` , `OrderDetail_Order_orderNo` , `OrderDetail_Order_Invoice_InvoiceNo` , `OrderDetail_Order_Invoice_PaymentMethod_pMethodNo` , `OrderDetail_Order_Customer_customerNo` , `orderNo`)
REFERENCES `mydb`.`OrderDetail` (`orderNo` , `orderNo` , `orderNo` , `orderNo` , `orderNo` , `orderNo` , `orderNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `sMethodNo`
FOREIGN KEY (`ShipmentMethod_sMethodNo` , `sMethodNo` , `OrderDetail_orderNo` , `OrderDetail_Product_productNo` , `OrderDetail_Order_orderNo` , `OrderDetail_Order_Invoice_InvoiceNo` , `OrderDetail_Order_Invoice_PaymentMethod_pMethodNo` , `OrderDetail_Order_Customer_customerNo`)
REFERENCES `mydb`.`ShipmentMethod` (`sMethodNo` , `sMethodNo` , `sMethodNo` , `sMethodNo` , `sMethodNo` , `sMethodNo` , `sMethodNo` , `sMethodNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `employeeNo`
FOREIGN KEY (`Employee_employeeNo`)
REFERENCES `mydb`.`Employee` (`employeeNo`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `orderNo`
FOREIGN KEY (`orderNo`)
REFERENCES `mydb`.`OrderDetail` (`orderNo`)
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;
文章来源:https://blog.csdn.net/weixin_49816179/article/details/135320340
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!