mysql反向工程不显示关系

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

我在做这个项目。虽然我的表定义了主键和外键,但当我尝试反向工程时,它不会在mysql workbench中显示任何关系!!
这是密码

  1. -- drop the database is one exists by the same name
  2. DROP DATABASE IF EXISTS hospital;
  3. -- create the database
  4. CREATE DATABASE hospital;
  5. -- use the database
  6. USE hospital;
  7. -- Table 1
  8. -- create the table patient_information
  9. CREATE TABLE patient_information
  10. (
  11. patient_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, -- identification number of patient
  12. last_name VARCHAR(30) NOT NULL, -- last name of patient
  13. first_name VARCHAR(20) NOT NULL, -- first name of patient
  14. phone_number CHAR(13) NOT NULL, -- phone number of patient
  15. gender ENUM('Male','Female','M','F','Not disclosed')NOT NULL, -- gender of patient
  16. birth_date DATE NOT NULL -- birth date of patient
  17. );
  18. -- Insert data into the table
  19. INSERT INTO patient_information (last_name,first_name,phone_number,gender,birth_date)
  20. VALUES
  21. ('Sharma','Ajay',7053415122,'Male','1986-09-05'),
  22. ('Shah','Akshay',7051234567,'Male','1988-04-28'),
  23. ('Jain','Bharat',6471226547,'Male','1969-05-02'),
  24. ('Patel','Manisha',6475471230,'Female','1999-06-09'),
  25. ('Smith','Jack',8994563210,'Male','2000-10-02'),
  26. ('John','Julie',9873344502,'Not disclosed ','1998-11-15'),
  27. ('Prajapati','Aksh',5456674596,'Male','1998-12-16'),
  28. ('Johnson','Angel',9294545621,'Not disclosed','1987-09-26'),
  29. ('Wayne','Ava', 9294543329,'Female','1979-02-28'),
  30. ('Addams','Mia',9004545621,'Female','2010-01-01'),
  31. ('Mia','Molkava',9009898621,'Female','2010-06-01'),
  32. ('Ann','Lisa',9234545621,'Female','2010-11-21'),
  33. ('Brown','Linda',9004125621,'Female','2010-10-01'),
  34. ('Daniel','Jack',9008989621,'Male','2010-07-10'),
  35. ('Perry','Eva',8783435652,'Female','1985-12-18');
  36. -- describe the table
  37. DESC patient_information;
  38. -- select data from table
  39. SELECT * FROM patient_information;
  40. -- Table 2
  41. -- create the table visit_information
  42. CREATE TABLE visit_information
  43. (
  44. patient_id INT UNSIGNED PRIMARY KEY,
  45. doctor_consulted VARCHAR(30) NOT NULL,
  46. checkup_room CHAR(4) NOT NULL,
  47. visit_date DATE NOT NULL,
  48. health_issue VARCHAR(100) NOT NULL,
  49. CONSTRAINT patient_id_fk FOREIGN KEY (patient_id) REFERENCES patient_information(patient_id)
  50. );
  51. -- patient_id
  52. -- doctor visited
  53. -- check up room
  54. -- visit date
  55. -- health issue
  56. -- weight
  57. INSERT INTO visit_information(patient_id, doctor_consulted, checkup_room, visit_date, health_issue) VALUES
  58. (1, 'Jack Wayne', 4023, '2020-06-20', 'Asthama'),
  59. -- (1, 'Gurpreet Singh', 4017, '2020-06-28', 'Corona'),
  60. (2, 'John Brown', 3023, '2020-02-11', 'Headache'),
  61. (3, 'Jessica Jhonson', 2022, '2020-04-15', 'Dengue'),
  62. (4, 'John Brown', 3023, '2020-05-11', 'Headache'),
  63. (5, 'Gurpreet Singh', 4012, '2020-04-11', 'Corona'),
  64. (6, 'Thomas Jacob', 4012, '2020-05-11', 'Corona'),
  65. (7, 'Victor Zack', 5002, '2020-05-28', 'Viral Fever'),
  66. (8, 'Alex Ryan', 4013, '2020-04-16', 'Corona'),
  67. (9, 'Thomas Jacob', 4013, '2020-06-01', 'Corona'),
  68. (10, 'Gurpreet Singh', 4014, '2020-03-25', 'Corona'),
  69. (11, 'Alex Ryan', 4014, '2020-05-26', 'Corona'),
  70. (12, 'Alex Johnson', 4015, '2020-05-27', 'Corona'),
  71. (13, 'Alex Singh', 4015, '2020-05-28', 'Corona'),
  72. (14, 'Alex Singh', 4016, '2020-05-29', 'Corona'),
  73. (15, 'Alex Singh', 4016, '2020-05-30', 'Corona')
  74. ;
  75. -- describe the table
  76. DESC visit_information;
  77. -- select data from table
  78. SELECT * FROM visit_information;
  79. -- the vales have one to one relationship
gijlo24d

gijlo24d1#

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

  1. Use hospital;
  2. SET FOREIGN_KEY_CHEKS=0;
  3. ALTER TABLE patient_information
  4. ADD CONSTRAINT patient_id_fk
  5. FOREIGN KEY (patient_id) REFERENCES visit_information (patient_id)
  6. ON DELETE CASCADE;

on delete cascade用于在两个表中删除约束

相关问题