mysql反向工程不显示关系

d7v8vwbk  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(286)

我在做这个项目。虽然我的表定义了主键和外键,但当我尝试反向工程时,它不会在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
gijlo24d

gijlo24d1#

在数据库中使用此查询,然后再试一次反向工程

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用于在两个表中删除约束

相关问题