【问题标题】:MySQL Reverse Engineer not showing relationshipMySQL逆向工程师未显示关系
【发布时间】:2020-07-10 22:00:56
【问题描述】:

我正在做这个项目。 虽然我的表具有定义了外键的主键,但当我尝试逆向工程时,它在 MySQL Workbench 中没有显示任何关系!

这是代码

-- drop the database is one exists by the same name
DROP DATABASE IF EXISTS hospital;
-- create the database
CREATE DATABASE hospital;
-- use the database
USE hospital;

-- Table 1
-- create the table patient_information
CREATE TABLE patient_information
(
    patient_id      INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,                    -- identification number of patient
    last_name       VARCHAR(30) NOT NULL,                                       -- last name of patient
    first_name      VARCHAR(20) NOT NULL,                                       -- first name of patient
    phone_number    CHAR(13) NOT NULL,                                          -- phone number of patient
    gender          ENUM('Male','Female','M','F','Not disclosed')NOT NULL,      -- gender of patient
    birth_date      DATE NOT NULL                                               -- birth date of patient
);

-- Insert data into the table
INSERT INTO patient_information (last_name,first_name,phone_number,gender,birth_date)
VALUES

('Sharma','Ajay',7053415122,'Male','1986-09-05'),
('Shah','Akshay',7051234567,'Male','1988-04-28'),
('Jain','Bharat',6471226547,'Male','1969-05-02'),
('Patel','Manisha',6475471230,'Female','1999-06-09'),
('Smith','Jack',8994563210,'Male','2000-10-02'),
('John','Julie',9873344502,'Not disclosed ','1998-11-15'),
('Prajapati','Aksh',5456674596,'Male','1998-12-16'),
('Johnson','Angel',9294545621,'Not disclosed','1987-09-26'),
('Wayne','Ava', 9294543329,'Female','1979-02-28'),
('Addams','Mia',9004545621,'Female','2010-01-01'),
('Mia','Molkava',9009898621,'Female','2010-06-01'),
('Ann','Lisa',9234545621,'Female','2010-11-21'),
('Brown','Linda',9004125621,'Female','2010-10-01'),
('Daniel','Jack',9008989621,'Male','2010-07-10'),
('Perry','Eva',8783435652,'Female','1985-12-18');

-- describe the table
DESC patient_information;

-- select data from table
SELECT * FROM patient_information;


-- Table 2
-- create the table visit_information
CREATE TABLE visit_information
(
    patient_id                  INT UNSIGNED PRIMARY KEY,
    doctor_consulted            VARCHAR(30) NOT NULL,
    checkup_room                CHAR(4) NOT NULL,
    visit_date                  DATE NOT NULL,
    health_issue                VARCHAR(100) NOT NULL,
    
    CONSTRAINT patient_id_fk FOREIGN KEY (patient_id) REFERENCES patient_information(patient_id)
);
-- patient_id
-- doctor visited
-- check up room
-- visit date
-- health issue
-- weight

INSERT INTO visit_information(patient_id, doctor_consulted, checkup_room, visit_date, health_issue) VALUES
(1, 'Jack Wayne', 4023, '2020-06-20', 'Asthama'),
-- (1, 'Gurpreet Singh', 4017, '2020-06-28', 'Corona'),
(2, 'John Brown', 3023, '2020-02-11', 'Headache'),
(3, 'Jessica Jhonson', 2022, '2020-04-15', 'Dengue'),
(4, 'John Brown', 3023, '2020-05-11', 'Headache'),
(5, 'Gurpreet Singh', 4012, '2020-04-11', 'Corona'),
(6, 'Thomas Jacob', 4012, '2020-05-11', 'Corona'),
(7, 'Victor Zack', 5002, '2020-05-28', 'Viral Fever'),
(8, 'Alex Ryan', 4013, '2020-04-16', 'Corona'),
(9, 'Thomas Jacob', 4013, '2020-06-01', 'Corona'),
(10, 'Gurpreet Singh', 4014, '2020-03-25', 'Corona'),
(11, 'Alex Ryan', 4014, '2020-05-26', 'Corona'),
(12, 'Alex Johnson', 4015, '2020-05-27', 'Corona'),
(13, 'Alex Singh', 4015, '2020-05-28', 'Corona'),
(14, 'Alex Singh', 4016, '2020-05-29', 'Corona'),
(15, 'Alex Singh', 4016, '2020-05-30', 'Corona')
;

-- describe the table
DESC visit_information;

-- select data from table
SELECT * FROM visit_information;

-- the vales have one to one relationship

【问题讨论】:

  • 您的设计只允许患者访问一次。我怀疑这就是你想要的。

标签: mysql sql mysql-workbench


【解决方案1】:

在您的数据库中使用此查询并再试一次逆向工程

Use hospital;
SET FOREIGN_KEY_CHEKS=0;

ALTER TABLE patient_information 
ADD CONSTRAINT patient_id_fk 
FOREIGN KEY (patient_id) REFERENCES visit_information (patient_id)
ON DELETE CASCADE;

ON DELETE CASCADE 用于在两个表中删除约束

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,然后我将引擎从 MyISAM 切换到 InnoDB,它运行良好。

    如果您使用的是 Hibernate - SpringBoot。在 y 中添加这一行

    【讨论】:

    • 您的答案似乎被截断了,并且您没有在答案中添加任何代码行
    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2011-07-24
    • 2023-03-24
    • 2021-09-29
    相关资源
    最近更新 更多