Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.99 KB | None | 0 0
  1. CREATE TABLE students(
  2. student_id INT(11) AUTO_INCREMENT NOT NULL,
  3. name VARCHAR(50)
  4. );
  5.  
  6. CREATE TABLE exams(
  7. exam_id INT(11) NOT NULL,
  8. name VARCHAR(50)
  9. );
  10.  
  11.  
  12. ALTER TABLE stutends
  13. ADD CONSTRAINT pk_student_id PRIMARY KEY(student_id);
  14.  
  15. ALTER TABLE exams
  16. ADD CONSTRAINT pk_exam_id PRIMARY KEY (exam_id);
  17.  
  18. CREATE TABLE studetns_exams(
  19. sutdent_id INT NOT NULL,
  20. exam_id INT NOT NULL
  21. );
  22.  
  23. ALTER TABLE students_exams
  24. ADD CONSTRAINT pk_students_exams PRIMARY KEY (student_id, exam_id);
  25.  
  26. ALTER TABLE students_exams
  27. ADD CONSTRAINT fk_student_exams_student_id FOREIGN KEY(student_id) REFERENCE students(student_id);
  28.  
  29. ALTER TABLE students_exams
  30. ADD CONSTRAINT fk_student_exam_exam_id FOREIGN KEY(exam_id) REFERENCE exams(exam_id);
  31.  
  32. INSERT INTO students VALUES
  33. (1, 'Mila'),
  34. (2, 'Toni'),
  35. (3, 'Ron');
  36.  
  37. INSERT INTO exams VALUES
  38. (101, 'Spring MVC'),
  39. (102, 'Neo4j'),
  40. (103, 'Oracle 11g');
  41.  
  42. INSERT INTO students_exams VALUES
  43. (1, 101),
  44. (1, 102),
  45. (2, 101),
  46. (2, 102),
  47. (3, 101),
  48. (3, 102);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement