Guest User

Untitled

a guest
Sep 27th, 2020
22
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 03. Many-To-Many Relationship
  2. CREATE DATABASE students;
  3. USE students;
  4.  
  5. CREATE TABLE exams
  6. (
  7. exam_id INT UNSIGNED PRIMARY KEY NOT NULL UNIQUE,
  8. name VARCHAR(30) NOT NULL UNIQUE
  9. );
  10.  
  11. INSERT INTO exams
  12. VALUES (101, 'Spring MVC'),
  13. (102, 'Neo4j'),
  14. (103, 'Oracle 11g');
  15.  
  16. CREATE TABLE students
  17. (
  18. student_id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT UNIQUE,
  19. name VARCHAR(30) NOT NULL
  20. );
  21.  
  22. INSERT INTO students (name)
  23. VALUES ('Mila'),
  24. ('Toni'),
  25. ('Ron');
  26.  
  27.  
  28. CREATE TABLE students_exams
  29. (
  30. student_id INT UNSIGNED NOT NULL,
  31. exam_id INT UNSIGNED NOT NULL,
  32.  
  33. CONSTRAINT pk_studentId_examId
  34. PRIMARY KEY (student_id, exam_id),
  35. CONSTRAINT fk_studentsExams_students
  36. FOREIGN KEY (student_id) REFERENCES students (student_id),
  37. CONSTRAINT fk_studentsExams_exams
  38. FOREIGN KEY (exam_id) REFERENCES exams (exam_id)
  39. );
  40. INSERT INTO students_exams
  41. VALUES (1, 101),
  42. (1, 102),
  43. (2, 101),
  44. (3, 103),
  45. (2, 102),
  46. (2, 103);
RAW Paste Data