简体   繁体   English

如何 ER 图 MYSQL

[英]How to ER Diagram MYSQL

I'm trying to create simple library database in mysql.我正在尝试在 mysql 中创建简单的库数据库。 I have 5 tables students, entry, book, typebook, author.我有 5 桌学生,条目,书籍,打字本,作者。 When I was trying to make er diagram with mysql reverse engineer my tables doesn't have any relationships on er diagram.当我尝试使用 mysql 逆向工程制作 er 图时,我的表在 er 图上没有任何关系。 But almost every databases have relationships on er diagram in the internet.但是几乎每个数据库都在互联网上的er图上有关系。 What am i doing wrong and how to fix it?我做错了什么以及如何解决?

CREATE TABLE student(
`stuNo` INT NOT NULL,
`stuname` VARCHAR(45) NULL,
`stusurname` VARCHAR(45) NULL,
`class` INT NULL,
`age` INT NULL,
PRIMARY KEY (`stuNo`));

 CREATE TABLE entry(
`stuNo` INT NOT NULL,
`entryno` INT NOT NULL,
`bookno` INT NOT NULL,
`borrowdate` DATE NULL,
`returndate` DATE NULL,
PRIMARY KEY (`bookno`));

 CREATE TABLE book(
`bookno` INT NOT NULL,
`bookname` VARCHAR(45) NULL,
 `authorno` INT NOT NULL,
`typeno` INT NOT NULL,
PRIMARY KEY (authorno , typeno));

 CREATE TABLE  typebook (
`typeno` INT NOT NULL,
`typename` VARCHAR(45) NULL,
PRIMARY KEY (`typeno`));

 CREATE TABLE author(
`authorno` INT NOT NULL,
`authorname` VARCHAR(45) NULL,
`autorname` VARCHAR(45) NULL,
PRIMARY KEY (`authorno`));

在此处输入图像描述

I had to switch the order of the CREATE TABLE我不得不切换 CREATE TABLE 的顺序

What changes is the order you have to nsert data.改变的是您必须插入数据的顺序。 For example例如

If you waant to INSERT a book, you first have to insert the typebook and authoir, that ocrresponds to the book and so on.如果您想插入一本书,您首先必须插入对应于该书的 typebook 和 authoir,依此类推。

CREATE TABLE student(
`stuNo` INT NOT NULL,
`stuname` VARCHAR(45) NULL,
`stusurname` VARCHAR(45) NULL,
`class` INT NULL,
`age` INT NULL,
PRIMARY KEY (`stuNo`));

 CREATE TABLE  typebook (
`typeno` INT NOT NULL,
`typename` VARCHAR(45) NULL,
PRIMARY KEY (`typeno`));

 CREATE TABLE author(
`authorno` INT NOT NULL,
`authorname` VARCHAR(45) NULL,
`autorname` VARCHAR(45) NULL,
PRIMARY KEY (`authorno`));

CREATE TABLE book(
`bookno` INT NOT NULL,
`bookname` VARCHAR(45) NULL,
 `authorno` INT NOT NULL,
`typeno` INT NOT NULL,
PRIMARY KEY (authorno , typeno),
INDEX(bookno),
FOREIGN KEY (typeno)
        REFERENCES typebook(typeno),
FOREIGN KEY (authorno)
        REFERENCES author(authorno));

 CREATE TABLE entry(
`stuNo` INT NOT NULL,
`entryno` INT NOT NULL,
`bookno` INT NOT NULL,
`borrowdate` DATE NULL,
`returndate` DATE NULL,
PRIMARY KEY (`bookno`),
FOREIGN KEY (stuNo)
        REFERENCES student(stuNo),
FOREIGN KEY (bookno)
        REFERENCES book(bookno)
        );

Results in结果是在此处输入图像描述

The problem is that your tables do not have any FOREIGN KEYS set.问题是您的表没有设置任何FOREIGN KEYS MySQL has no idea that the authorno column from one table should be linked to the authorno column from another table. MySQL 不知道一个表中的authorno列应该链接到另一个表中的authorno列。 So no relationship links are generated.因此不会生成任何关系链接。

To generate the relationship links to your ER diagram, you have to use FOREIGN KEY entries in your CREATE TABLE query to specify which column from one table is referencing a column from a different table:要生成到 ER 图的关系链接,您必须在CREATE TABLE查询中使用FOREIGN KEY条目来指定一个表中的哪一列正在引用另一表中的一列:

CREATE TABLE author(
    `authorno` INT NOT NULL,
    `authorname` VARCHAR(45) NULL,
    `autorname` VARCHAR(45) NULL,
    PRIMARY KEY (`authorno`)
);

CREATE TABLE book(
    `bookno` INT NOT NULL,
    `bookname` VARCHAR(45) NULL,
    `authorno` INT NOT NULL,
    `typeno` INT NOT NULL,
    PRIMARY KEY (bookno),
    FOREIGN KEY (authorno) REFERENCES author(authorno)
);

With the added FOREIGN KEY entries MySQL now knows that the authorno column in book must reference a value from the authorno column of the author table.通过添加FOREIGN KEY条目 MySQL 现在知道book中的authorno列必须引用author表的authorno列中的值。 When generating the ER diagram you should get the lines between the tables to see the relationship.生成 ER 图时,您应该获取表之间的线以查看关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM